#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Define the OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Create an instance of the display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 0x3C);
// Joystick pins
const int joystickXPin = A0; // X-axis
const int joystickYPin = A1; // Y-axis
const int buttonPin = 2; // Button pin
// UI variables
int cursorX = 0;
int cursorY = 0;
void setup() {
// Initialize the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
// Initialize joystick button pin
pinMode(buttonPin, INPUT_PULLUP);
// Display initial UI
drawUI();
}
void loop() {
// Read joystick values
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
// Map joystick values to cursor movement (inverted)
if (xValue < 100) { // Left
cursorX++; // Move right
} else if (xValue > 900) { // Right
cursorX--; // Move left
}
if (yValue < 100) { // Up
cursorY++; // Move down
} else if (yValue > 900) { // Down
cursorY--; // Move up
}
// Keep cursor within bounds
cursorX = constrain(cursorX, 0, 127);
cursorY = constrain(cursorY, 0, 63);
// Check button press
if (digitalRead(buttonPin) == LOW) {
// Perform action on button press
// For example, toggle a setting or select an item
}
// Update the display
drawUI();
delay(100); // Simple debounce
}
void drawUI() {
display.clearDisplay();
// Draw a simple cursor
display.fillRect(cursorX, cursorY, 5, 5, WHITE);
// Add more UI elements as needed
display.setCursor(0, 0);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println("Use joystick to move");
display.display();
}