#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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);
// Joystick pins
const int VRxPin = A1; // Joystick X-axis (analog)
const int VRyPin = A0; // Joystick Y-axis (analog)
const int buttonPin = 2; // Joystick button (digital)
int ballX = SCREEN_WIDTH / 2; // Starting X position of the ball
int ballY = SCREEN_HEIGHT / 2; // Starting Y position of the ball
const int ballSize = 5; // Radius of the ball
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Joystick button
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay(); // Clear display
display.display(); // Show empty display
}
void loop() {
// Read joystick values
int VRxValue = analogRead(VRxPin); // Read X-axis
int VRyValue = analogRead(VRyPin); // Read Y-axis
// Map the joystick values to move the ball
int newBallX = map(VRxValue, 1023, 0, 0, SCREEN_WIDTH - ballSize);
int newBallY = map(VRyValue, 1023, 0, 0, SCREEN_HEIGHT - ballSize);
// Update ball position
ballX = newBallX;
ballY = newBallY;
// Clear the display
display.clearDisplay();
// Draw the ball at the new position
display.fillCircle(ballX, ballY, ballSize, SSD1306_WHITE);
// Update the display with new ball position
display.display();
// Small delay for smooth movement
delay(10);
}