#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SH1107 display = Adafruit_SH1107(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
const int joyXPin = A0; // Joystick x-axis (HO)
const int joyYPin = A1; // Joystick y-axis (VE)
const int buttonPin = 2; // Joystick button (SE)
int posX = SCREEN_WIDTH / 2, posY = SCREEN_HEIGHT / 2; // Initial position
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Joystick button input
if (!display.begin(0x3C, true)) { // Address 0x3C for 128x64
Serial.println(F("SH1107 allocation failed"));
for(;;);
}
display.clearDisplay();
display.display();
}
void loop() {
int joyX = analogRead(joyXPin);
int joyY = analogRead(joyYPin);
int buttonState = digitalRead(buttonPin);
// Map joystick values to screen coordinates
posX = map(joyX, 0, 1023, 0, SCREEN_WIDTH - 1);
posY = map(joyY, 0, 1023, 0, SCREEN_HEIGHT - 1);
// Clear the previous position
display.clearDisplay();
// Draw at the new position
if (buttonState == LOW) {
display.fillCircle(posX, posY, 3, SH110X_WHITE); // Larger circle when button pressed
} else {
display.drawCircle(posX, posY, 3, SH110X_WHITE); // Regular circle when moving joystick
}
// Update the display
display.display();
delay(50); // Short delay for responsiveness
}