#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width in pixels
#define SCREEN_HEIGHT 64 // OLED height in pixels
// Create display object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Ball animation variables
int ballX = 0, ballY = 0;
int ballSpeedX = 1, ballSpeedY = 1;
void setup() {
Serial.begin(115200);
// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("ESP32 OLED DEMO");
display.println("Initializing...");
display.display();
delay(2000);
}
void loop() {
display.clearDisplay();
// Display fake sensor readings
float temp = random(200, 300) / 10.0; // 20.0 to 30.0
float humidity = random(400, 700) / 10.0; // 40.0 to 70.0
display.setCursor(0, 0);
display.setTextSize(1);
display.print("Temp: ");
display.print(temp);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
// Draw animated ball
display.fillCircle(ballX, ballY + 25, 3, SSD1306_WHITE);
// Update ball position
ballX += ballSpeedX;
ballY += ballSpeedY;
// Bounce ball off edges
if (ballX <= 0 || ballX >= SCREEN_WIDTH - 1) ballSpeedX = -ballSpeedX;
if (ballY <= 0 || ballY >= SCREEN_HEIGHT - 1) ballSpeedY = -ballSpeedY;
// Show everything
display.display();
delay(50);
}