#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);
// Animation control
unsigned long lastSwitch = 0;
int animIndex = 0;
const int animDuration = 5000; // milliseconds per animation
// Animation variables
int scrollX = SCREEN_WIDTH;
int ballX = 0, ballY = 0, dx = 2, dy = 2;
int wavePhase = 0;
int starY = SCREEN_HEIGHT;
int pulseSize = 0;
bool pulseGrow = true;
int sweepPos = 0;
int squareSize = 0;
int sparkleCount = 0;
float angle = 0;
int heartSize = 5;
bool heartGrow = true;
void setup() {
Wire.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
}
void loop() {
unsigned long now = millis();
if (now - lastSwitch > animDuration) {
animIndex = (animIndex + 1) % 10; // now 10 animations
lastSwitch = now;
// reset variables
scrollX = SCREEN_WIDTH;
ballX = 0; ballY = 0; dx = 2; dy = 2;
wavePhase = 0;
starY = SCREEN_HEIGHT;
pulseSize = 0; pulseGrow = true;
sweepPos = 0;
squareSize = 0;
sparkleCount = 0;
angle = 0;
heartSize = 5; heartGrow = true;
display.clearDisplay();
}
switch (animIndex) {
case 0: animateScrollingText(); break;
case 1: animateBouncingBall(); break;
case 2: animateWave(); break;
case 3: animateFallingStar(); break;
case 4: animatePulseCircle(); break;
case 5: animateDiagonalSweep(); break;
case 6: animateExpandingSquare(); break;
case 7: animateSparkles(); break;
case 8: animateRotatingCross(); break;
case 9: animateHeartbeat(); break;
}
delay(30);
}
// ------------------ Animation 1: Scrolling Text ------------------
void animateScrollingText() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(scrollX, 30);
display.println("Enper Education");
display.display();
scrollX -= 2;
if (scrollX < -100) scrollX = SCREEN_WIDTH;
}
// ------------------ Animation 2: Bouncing Ball ------------------
void animateBouncingBall() {
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;
}
// ------------------ Animation 3: Sine Wave ------------------
void animateWave() {
display.clearDisplay();
for (int x = 0; x < SCREEN_WIDTH; x++) {
int y = 32 + 20 * sin((x + wavePhase) * 0.1);
display.drawPixel(x, y, SSD1306_WHITE);
}
wavePhase += 2;
display.display();
}
// ------------------ Animation 4: Falling Star ------------------
void animateFallingStar() {
display.clearDisplay();
display.fillTriangle(64, starY, 60, starY + 10, 68, starY + 10, SSD1306_WHITE);
display.display();
starY -= 2;
if (starY < -10) starY = SCREEN_HEIGHT;
}
// ------------------ Animation 5: Pulsing Circle ------------------
void animatePulseCircle() {
display.clearDisplay();
display.fillCircle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, pulseSize, SSD1306_WHITE);
display.display();
if (pulseGrow) {
pulseSize++;
if (pulseSize > 20) pulseGrow = false;
} else {
pulseSize--;
if (pulseSize < 2) pulseGrow = true;
}
}
// ------------------ Animation 6: Diagonal Sweep ------------------
void animateDiagonalSweep() {
display.clearDisplay();
display.drawLine(0, sweepPos, sweepPos, 0, SSD1306_WHITE);
display.display();
sweepPos += 2;
if (sweepPos > SCREEN_WIDTH) sweepPos = 0;
}
// ------------------ Animation 7: Expanding Square ------------------
void animateExpandingSquare() {
display.clearDisplay();
int cx = SCREEN_WIDTH / 2;
int cy = SCREEN_HEIGHT / 2;
display.drawRect(cx - squareSize/2, cy - squareSize/2, squareSize, squareSize, SSD1306_WHITE);
display.display();
squareSize += 2;
if (squareSize > 60) squareSize = 0;
}
// ------------------ Animation 8: Sparkles ------------------
void animateSparkles() {
display.clearDisplay();
for (int i = 0; i < 20; i++) {
int x = random(SCREEN_WIDTH);
int y = random(SCREEN_HEIGHT);
display.drawPixel(x, y, SSD1306_WHITE);
}
display.display();
}
// ------------------ Animation 9: Rotating Cross ------------------
void animateRotatingCross() {
display.clearDisplay();
int cx = SCREEN_WIDTH / 2;
int cy = SCREEN_HEIGHT / 2;
int len = 20;
int x1 = cx + len * cos(angle);
int y1 = cy + len * sin(angle);
int x2 = cx - len * cos(angle);
int y2 = cy - len * sin(angle);
display.drawLine(x1, y1, x2, y2, SSD1306_WHITE);
display.drawLine(cx + len * cos(angle + M_PI/2), cy + len * sin(angle + M_PI/2),
cx - len * cos(angle + M_PI/2), cy - len * sin(angle + M_PI/2), SSD1306_WHITE);
display.display();
angle += 0.1;
}
// ------------------ Animation 10: Heartbeat ------------------
void animateHeartbeat() {
display.clearDisplay();
int cx = SCREEN_WIDTH / 2;
int cy = SCREEN_HEIGHT / 2;
// simple heart shape using circles + triangle
display.fillCircle(cx - heartSize/2, cy - heartSize/2, heartSize/2, SSD1306_WHITE);
display.fillCircle(cx + heartSize/2, cy - heartSize/2, heartSize/2, SSD1306_WHITE);
display.fillTriangle(cx - heartSize, cy - heartSize/2,
cx + heartSize, cy - heartSize/2,
cx, cy + heartSize, SSD1306_WHITE);
display.display();
if (heartGrow) {
heartSize++;
if (heartSize > 20) heartGrow = false;
} else {
heartSize--;
if (heartSize < 5) heartGrow = true;
}
}
https://wokwi.com/projects/465181710585385985