#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int VRx = A0;
int VRy = A1;
int SW = 2;
void update(float x, float y, bool pressed);
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 initialization failed");
while (1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.drawRoundRect(92, 10, 11, 15, 2, WHITE); // Up
display.drawTriangle(88, 10, 106, 10, 97, 1, WHITE);
display.drawRoundRect(76, 26, 15, 11, 2, WHITE); // Left
display.drawTriangle(76, 22, 76, 40, 67, 31, WHITE);
display.drawRoundRect(92, 38, 11, 15, 2, WHITE); // Down
display.drawTriangle(88, 52, 106, 52, 97, 61, WHITE);
display.drawRoundRect(104, 26, 15, 11, 2, WHITE); // Right
display.drawTriangle(118, 22, 118, 40, 127, 31, WHITE);
display.drawCircle(97, 31, 5, WHITE);
display.display();
delay(2000);
pinMode(SW, INPUT_PULLUP);
}
void loop() {
int rawX = analogRead(VRx);
int rawY = analogRead(VRy);
bool pressed = (digitalRead(SW) == LOW);
float x = -(rawX - 512.0) / 512.0;
float y = (rawY - 512.0) / 512.0;
x = constrain(x, -1.0, 1.0);
y = constrain(y, -1.0, 1.0);
// Redraw UI each frame
display.clearDisplay();
display.drawRoundRect(92, 10, 11, 15, 2, WHITE); // Up
display.drawTriangle(88, 10, 106, 10, 97, 1, WHITE);
display.drawRoundRect(76, 26, 15, 11, 2, WHITE); // Left
display.drawTriangle(76, 22, 76, 40, 67, 31, WHITE);
display.drawRoundRect(92, 38, 11, 15, 2, WHITE); // Down
display.drawTriangle(88, 52, 106, 52, 97, 61, WHITE);
display.drawRoundRect(104, 26, 15, 11, 2, WHITE); // Right
display.drawTriangle(118, 22, 118, 40, 127, 31, WHITE);
display.drawCircle(97, 31, 5, WHITE); // Center
update(x, y, pressed);
display.display();
delay(100);
}
void update(float x, float y, bool pressed){
if (pressed) {
// Blink center and show message
display.fillCircle(64, 31, 5, WHITE);
display.setCursor(40, 54);
display.setTextSize(1);
display.print("Pressed!");
return;
}
if (x == 0 && y == 0) {
display.fillCircle(97, 31, 5, WHITE); // Center
} else if (x > 0) {
display.fillRoundRect(104, 26, 15, 11, 2, WHITE);
display.fillTriangle(118, 22, 118, 40, 127, 31, WHITE); // Right
} else if (x < 0) {
display.fillRoundRect(76, 26, 15, 11, 2, WHITE);
display.fillTriangle(76, 22, 76, 40, 67, 31, WHITE); // Left
}
if (y > 0) {
display.fillRoundRect(92, 10, 11, 15, 2, WHITE);
display.fillTriangle(88, 10, 106, 10, 97, 1, WHITE); // Up
} else if (y < 0) {
display.fillRoundRect(92, 38, 11, 15, 2, WHITE);
display.fillTriangle(88, 52, 106, 52, 97, 61, WHITE); // Down
}
}