#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h>
#include "RTClib.h"
// OLED setup (ESP32-C3 Mini I2C pins: SDA=8, SCL=9)
U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 9, /* data=*/ 8);
// RTC object (works with DS3231 or DS1307)
RTC_DS3231 rtc;
int animIndex = 0;
unsigned long lastSwitch = 0;
void setup() {
u8g2.begin();
u8g2.setContrast(255);
if (!rtc.begin()) {
while (1); // halt if RTC not found
}
// DS3231 doesn’t need isrunning() check
}
// --- Animation functions ---
void animClock() {
DateTime now = rtc.now();
char buf[20];
sprintf(buf, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
u8g2.setFont(u8g2_font_8x13B_mn);
u8g2.drawStr(20, 64, buf);
char dateBuf[20];
sprintf(dateBuf, "%02d/%02d/%04d", now.day(), now.month(), now.year());
//u8g2.setFont(u8g2_font_6x10_tf);
u8g2.setFont(u8g2_font_ncenB14_tr); // serif bold
u8g2.drawStr(20, 80, dateBuf);
}
void animLines() {
for (int i=0; i<128; i+=8) {
u8g2.drawLine(0, i, 127, 127-i);
}
}
void animBoxes() {
for (int i=0; i<60; i+=10) {
u8g2.drawFrame(10+i, 10+i, 108-2*i, 108-2*i);
}
}
void animFilledBox() {
u8g2.drawBox(30, 30, 60, 60);
}
void animCircle() {
u8g2.drawCircle(64, 64, 30, U8G2_DRAW_ALL);
}
void animDisc() {
u8g2.drawDisc(64, 64, 30);
}
void animTriangle() {
u8g2.drawTriangle(20, 100, 100, 100, 60, 20);
}
void animText() {
u8g2.setFont(u8g2_font_8x13B_mn); // valid font
u8g2.drawStr(10, 64, "Graphics Demo");
}
void animBranding() {
// u8g2.setFont(u8g2_font_5x8_mf);
// u8g2.drawStr(20, 120, "AI Centre Nandurbar");
u8g2.setFont(u8g2_font_ncenB14_tr); // serif bold
u8g2.drawStr(10, 64, "ARVID ");
}
void animCheckerboard() {
for (int y=0; y<128; y+=16) {
for (int x=0; x<128; x+=16) {
if ((x+y)%32==0) u8g2.drawBox(x,y,16,16);
}
}
}
// --- Draw current animation ---
void drawAnimation(int idx) {
switch(idx) {
case 0: animClock(); break;
case 1: animLines(); break;
case 2: animBoxes(); break;
case 3: animFilledBox(); break;
case 4: animCircle(); break;
case 5: animDisc(); break;
case 6: animTriangle(); break;
case 7: animText(); break;
case 8: animBranding(); break;
case 9: animCheckerboard(); break;
}
}
void loop() {
// Switch animation every 3 seconds
if (millis() - lastSwitch > 3000) {
animIndex++;
if (animIndex > 9) animIndex = 0;
lastSwitch = millis();
}
u8g2.firstPage();
do {
drawAnimation(animIndex);
} while (u8g2.nextPage());
}
https://wokwi.com/projects/466622604391530497