#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);
// Paddle
float paddleX = 60;
const int paddleY = 60;
const int paddleWidth = 20;
const int paddleHeight = 3;
// Ball
float ballX = 64;
float ballY = 32;
float ballSpeedX = 1;
float ballSpeedY = 1;
// Potentiometer
const int potPin = A0;
float smoothedPot = 0.0;
void setup() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;); // Display not found
}
display.clearDisplay();
display.display();
}
void loop() {
// Read potentiometer
int potRaw = analogRead(potPin);
// Smooth the input using low-pass filter
smoothedPot = smoothedPot * 0.9 + potRaw * 0.1;
// Map to paddle position
paddleX = map(smoothedPot, 0, 1023, 0, SCREEN_WIDTH - paddleWidth);
// Move ball
ballX += ballSpeedX;
ballY += ballSpeedY;
// Bounce ball off walls
if (ballX <= 0 || ballX >= SCREEN_WIDTH - 1) ballSpeedX *= -1;
if (ballY <= 0) ballSpeedY *= -1;
// Bounce off paddle
if (ballY >= paddleY - 2 && ballX >= paddleX && ballX <= paddleX + paddleWidth) {
ballSpeedY *= -1;
ballY = paddleY - 2; // Prevent sticking
}
// Missed paddle
if (ballY > SCREEN_HEIGHT) {
ballX = 64;
ballY = 32;
ballSpeedY = 1;
ballSpeedX = (random(0, 2) == 0) ? 1 : -1;
}
// Draw game
display.clearDisplay();
// Paddle
display.fillRect((int)paddleX, paddleY, paddleWidth, paddleHeight, SSD1306_WHITE);
// Ball
display.fillCircle((int)ballX, (int)ballY, 2, SSD1306_WHITE);
display.display();
delay(10); // small delay for timing
}