#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int JOY_X = 26; // HORZ
const int JOY_Y = 27; // VERT
const int JOY_SW = 16; // SEL
void setup() {
Wire.setSDA(0);
Wire.setSCL(1);
Wire.begin();
pinMode(JOY_SW, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
}
void loop() {
int rawX = analogRead(JOY_X);
int rawY = analogRead(JOY_Y);
// Map 12-bit ADC (0-4095) to OLED coordinates
int x = map(rawX, 0, 4095, 0, 127);
int y = map(rawY, 0, 4095, 63, 0); // invert Y if desired
display.clearDisplay();
// Draw a 4x4 square as the cursor
display.fillRect(x, y, 4, 4, SSD1306_WHITE);
// If joystick button is pressed, draw a border
if (digitalRead(JOY_SW) == LOW) {
display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
}
display.display();
delay(20);
}