#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <time.h>
// Constants
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define WIDTH 320
#define HEIGHT 240
#define CENTER_X WIDTH / 2
#define CENTER_Y HEIGHT / 2
#define RADIUS (CENTER_X < CENTER_Y ? CENTER_X : CENTER_Y) - 10
// Create the display
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
void setup() {
tft.begin();
tft.setRotation(3); // Landscape mode
// Draw clock face and hands once
drawClockFace();
}
void loop() {
// Get current time
time_t now = time(NULL);
struct tm *current_time = localtime(&now);
int seconds = current_time->tm_sec;
int minutes = current_time->tm_min;
int hours = current_time->tm_hour % 12;
// Clear the screen
tft.fillScreen(ILI9341_BLACK);
// Draw clock face and hands
drawClockFace();
drawClockHands(seconds, minutes, hours);
// Cap the frame rate
delay(1000);
}
void drawClockFace() {
tft.fillCircle(CENTER_X, CENTER_Y, RADIUS, ILI9341_WHITE);
for (int i = 0; i < 12; ++i) {
float angle = i * 2 * M_PI / 12;
int x1 = CENTER_X + cos(angle) * (RADIUS - 5);
int y1 = CENTER_Y + sin(angle) * (RADIUS - 5);
int x2 = CENTER_X + cos(angle) * (RADIUS - 15);
int y2 = CENTER_Y + sin(angle) * (RADIUS - 15);
tft.drawLine(x1, y1, x2, y2, ILI9341_WHITE);
}
}
void drawClockHands(int seconds, int minutes, int hours) {
drawHand(seconds, 60, 2, ILI9341_RED);
drawHand(minutes, 60, 4, ILI9341_GREEN);
drawHand(hours * 5 + minutes / 12, 60, 6, ILI9341_BLUE);
}
void drawHand(int value, int range, int length, uint16_t color) {
float angle = (value * 360.0f / range - 90) * M_PI / 180;
int x2 = CENTER_X + cos(angle) * (RADIUS - length);
int y2 = CENTER_Y + sin(angle) * (RADIUS - length);
tft.drawLine(CENTER_X, CENTER_Y, x2, y2, color);
}