#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <RTClib.h>
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
RTC_DS3231 rtc;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
tft.begin();
tft.setRotation(3); // Adjust rotation if needed
}
void drawClock() {
tft.fillScreen(ILI9341_BLACK);
DateTime now = rtc.now();
int hours = now.hour();
int minutes = now.minute();
int seconds = now.second();
int centerX = tft.width() / 2;
int centerY = tft.height() / 2;
int clockRadius = tft.width() / 3;
// Draw clock face
tft.fillCircle(centerX, centerY, clockRadius, ILI9341_WHITE);
tft.drawCircle(centerX, centerY, clockRadius, ILI9341_YELLOW);
// Draw clock hands
int secondHandLength = clockRadius - 10;
int minuteHandLength = clockRadius - 20;
int hourHandLength = clockRadius - 40;
int secondX = centerX + secondHandLength * cos((seconds - 15) * 6 * PI / 180);
int secondY = centerY + secondHandLength * sin((seconds - 15) * 6 * PI / 180);
tft.drawLine(centerX, centerY, secondX, secondY, ILI9341_RED);
int minuteX = centerX + minuteHandLength * cos((minutes - 15) * 6 * PI / 180);
int minuteY = centerY + minuteHandLength * sin((minutes - 15) * 6 * PI / 180);
tft.drawLine(centerX, centerY, minuteX, minuteY, ILI9341_BLUE);
int hourX = centerX + hourHandLength * cos((hours % 12 - 3) * 30 * PI / 180);
int hourY = centerY + hourHandLength * sin((hours % 12 - 3) * 30 * PI / 180);
tft.drawLine(centerX, centerY, hourX, hourY, ILI9341_GREEN);
}
void loop() {
drawClock();
// delay(1000);
}