diff --git a/clock.ino b/clock.ino new file mode 100644 index 0000000..1071926 --- /dev/null +++ b/clock.ino @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +RTC_DS1307 rtc; + +#define TIME_X 5 +#define TIME_Y 70 +#define TIME_SIZE 10 + +void setup() { + TFT_BL_ON; + Tft.TFTinit(); + Wire.begin(); + rtc.begin(); + + // Uncomment to set RTC to compile time (do once, then comment out) + // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); + + // Clear entire screen once + Tft.fillScreen(0, 320, 0, 240, BLACK); +} + +void loop() { + static unsigned long lastUpdate = 0; + static char lastTime[16] = ""; + + if (millis() - lastUpdate >= 1000) { + lastUpdate = millis(); + + DateTime now = rtc.now(); + + // Convert to 12-hour format + int hour12 = now.hour(); + if (hour12 == 0) { + hour12 = 12; // Midnight is 12 AM + } else if (hour12 > 12) { + hour12 -= 12; // Convert PM hours + } + + char timebuf[16]; + sprintf(timebuf, "%02d:%02d", hour12, now.minute()); + + // Update time if changed + if (strcmp(timebuf, lastTime) != 0) { + // Erase old time by drawing in BLACK + Tft.drawString(lastTime, TIME_X, TIME_Y, TIME_SIZE, BLACK, LANDSCAPE); + + // Draw new time in WHITE + Tft.drawString(timebuf, TIME_X, TIME_Y, TIME_SIZE, WHITE, LANDSCAPE); + + strcpy(lastTime, timebuf); + } + } +} \ No newline at end of file