#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
RTC_DS3231 rtc;
#define BLACK 0x0000
#define WHITE 0xFFFF
void setup() {
Serial.begin(9600);
Serial.println("Initializing...");
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
tft.init(TFT_WIDTH, TFT_HEIGHT);
tft.setRotation(1);
// Clear the screen
tft.fillScreen(BLACK);
Serial.println("Display initialized.");
}
void loop() {
DateTime now = rtc.now();
// Clear the area where the digital clock will be displayed
tft.fillRect(0, 0, TFT_WIDTH, TFT_HEIGHT, BLACK);
// Display digital time on TFT
tft.setCursor(10, 10);
tft.setTextSize(4);
tft.setTextColor(WHITE);
if (now.hour() < 10)
// Add leading zero
tft.print('0');
tft.print(now.hour());
tft.print(':');
// Add leading zero
if (now.minute() < 10)
tft.print('0');
tft.print(now.minute());
tft.print(':');
// Add leading zero
if (now.second() < 10)
tft.print('0');
tft.print(now.second());
// Update every second
delay(1000);
}