#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// #include <Adafruit_SH1106.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// ----------------------------------------------------
int potX = A0;
int potY = A1;
int potZ = A2;
float angleX, angleY, angleZ;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
display.clearDisplay(); // vymazání displeje
display.setTextSize(1);
display.setTextColor(WHITE);
display.display(); // aktualizace displeje
}
void loop() {
// Read potentiometer values
int valX = analogRead(potX);
int valY = analogRead(potY);
int valZ = analogRead(potZ);
// Map potentiometer values to angles (0 to 360 degrees)
angleX = map(valX, 0, 1023, 0, 360);
angleY = map(valY, 0, 1023, 0, 360);
angleZ = map(valZ, 0, 1023, 0, 360);
// Clear the display
display.clearDisplay();
// Calculate coordinates of the center of the square
float centerX = SCREEN_WIDTH / 2.0;
float centerY = SCREEN_HEIGHT / 2.0;
// Calculate coordinates of the vertices of the square after rotation
float vertices[4][2];
vertices[0][0] = centerX + 10 * (cos(angleZ) * cos(angleY) - sin(angleZ) * sin(angleX) * sin(angleY));
vertices[0][1] = centerY + 10 * (sin(angleZ) * cos(angleY) + cos(angleZ) * sin(angleX) * sin(angleY));
vertices[1][0] = centerX + 10 * (-cos(angleZ) * sin(angleY) - sin(angleZ) * sin(angleX) * cos(angleY));
vertices[1][1] = centerY + 10 * (-sin(angleZ) * sin(angleY) + cos(angleZ) * sin(angleX) * cos(angleY));
vertices[2][0] = centerX + 10 * (sin(angleZ) * cos(angleX));
vertices[2][1] = centerY + 10 * (-cos(angleZ) * cos(angleX));
vertices[3][0] = centerX + 10 * (cos(angleZ) * sin(angleY) + sin(angleZ) * sin(angleX) * cos(angleY));
vertices[3][1] = centerY + 10 * (sin(angleZ) * sin(angleY) - cos(angleZ) * sin(angleX) * cos(angleY));
// Draw the square on the OLED display
for (int i = 0; i < 3; i++) {
display.drawLine(vertices[i][0], vertices[i][1], vertices[i + 1][0], vertices[i + 1][1], WHITE);
}
display.drawLine(vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1], WHITE);
// Display the result
display.display();
delay(50); // You can adjust the delay based on your preference and the responsiveness of your potentiometers
}