#include <Wire.h>
#include "RTClib.h"
#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);
RTC_DS1307 rtc;
bool zoomIn = true;
void setup() {
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED allocation failed"));
while (true);
}
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (true);
}
if (!rtc.isrunning()) {
Serial.println("RTC not running, setting time...");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
display.clearDisplay();
display.display();
}
void loop() {
DateTime now = rtc.now();
// Format: HH:MM:SS (8 characters + null terminator)
char timeStr[9];
sprintf(timeStr, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
// Toggle zoom: size 1 or 2 only
int textSize = zoomIn ? 2 : 1;
zoomIn = !zoomIn;
display.clearDisplay();
display.setTextSize(textSize);
display.setTextColor(SSD1306_WHITE);
// Calculate bounding box to center text
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(timeStr, 0, 0, &x1, &y1, &w, &h);
int x = (SCREEN_WIDTH - w) / 2;
int y = (SCREEN_HEIGHT - h) / 2;
display.setCursor(x, y);
display.print(timeStr);
display.display();
delay(1000);
}