#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <math.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Demo control
unsigned long lastSwitch = 0;
int demoIndex = 0;
const int demoDuration = 5000; // milliseconds per demo
// For scrolling text
int scrollX = SCREEN_WIDTH;
// For bouncing ball
int ballX = 0, ballY = 0;
int dx = 2, dy = 2;
// ------------------ Setup ------------------
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
}
// ------------------ Main Loop ------------------
void loop() {
unsigned long now = millis();
if (now - lastSwitch > demoDuration) {
demoIndex = (demoIndex + 1) % 5;
lastSwitch = now;
scrollX = SCREEN_WIDTH; // reset scroll
ballX = 0; ballY = 0; dx = 2; dy = 2; // reset ball
display.clearDisplay();
}
switch (demoIndex) {
case 0: showStaticText(); break;
case 1: showScrollingText(); break;
case 2: showShapes(); break;
case 3: showBouncingBall(); break;
case 4: showFractalTree(); break;
}
delay(30);
}
// ------------------ Demo 1: Static Text ------------------
void showStaticText() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.println(" I Am ARVIND");
display.display();
}
// ------------------ Demo 2: Scrolling Text ------------------
void showScrollingText() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(scrollX, 30);
display.println("Scrolling Text");
display.display();
scrollX -= 2;
if (scrollX < -100) scrollX = SCREEN_WIDTH;
}
// ------------------ Demo 3: Geometric Shapes ------------------
void showShapes() {
display.clearDisplay();
display.drawRect(10, 10, 50, 30, SSD1306_WHITE);
display.fillCircle(100, 32, 10, SSD1306_WHITE);
display.drawLine(0, 0, 127, 63, SSD1306_WHITE);
display.display();
}
// ------------------ Demo 4: Bouncing Ball ------------------
void showBouncingBall() {
display.clearDisplay();
display.fillCircle(ballX, ballY, 5, SSD1306_WHITE);
display.display();
ballX += dx;
ballY += dy;
if (ballX <= 0 || ballX >= SCREEN_WIDTH - 5) dx = -dx;
if (ballY <= 0 || ballY >= SCREEN_HEIGHT - 5) dy = -dy;
}
// ------------------ Demo 5: Fractal Tree ------------------
void drawBranch(int x, int y, int len, float angle, int depth) {
if (depth == 0) return;
int x2 = x + len * cos(angle);
int y2 = y - len * sin(angle);
display.drawLine(x, y, x2, y2, SSD1306_WHITE);
drawBranch(x2, y2, len * 0.7, angle + 0.5, depth - 1);
drawBranch(x2, y2, len * 0.7, angle - 0.5, depth - 1);
}
void showFractalTree() {
display.clearDisplay();
drawBranch(64, 63, 20, 1.57, 5); // Start from bottom center
display.display();
}