#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#define TFT_CS 15 // Chip select control pin
#define TFT_RST 4 // Reset pin
#define TFT_DC 2 // Data/command control pin
#define TFT_SCK 18 // Serial Clock pin
#define TFT_MOSI 23 // Master Out Slave In pin
#define TFT_WIDTH 240
#define TFT_HEIGHT 240
const char* ssid = "Airtel_arvi_6035"; // Replace with your WiFi SSID
const char* password = "air26289"; // Replace with your WiFi password
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org", 19800, 60000); // Offset for IST (UTC+5:30) and update interval
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
#define BLUE 0x001F
#define YELLOW 0xFFE0
int clockCenterX = TFT_WIDTH / 2;
int clockCenterY = TFT_HEIGHT / 2;
void setup() {
Serial.begin(115200);
Serial.println("Initializing display...");
// Set up SPI
SPI.begin(TFT_SCK, -1, TFT_MOSI, -1);
tft.init(TFT_WIDTH, TFT_HEIGHT);
tft.setRotation(1);
tft.fillScreen(BLACK); // Clear the screen
Serial.println("Display initialized.");
drawClockFace();
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
// Initialize the NTP client
timeClient.begin();
timeClient.update();
}
void loop() {
timeClient.update();
drawClockHands();
delay(1000); // Update every second
}
void drawClockFace() {
tft.fillScreen(BLACK); // Clear the screen
tft.drawCircle(clockCenterX, clockCenterY, 100, WHITE);
// Draw the clock numbers
tft.setTextSize(2);
tft.setTextColor(WHITE);
for (int i = 1; i <= 12; i++) {
float angle = i * 30 * PI / 180;
int x = clockCenterX + 90 * cos(angle - PI / 2);
int y = clockCenterY + 90 * sin(angle - PI / 2);
tft.setCursor(x - 5, y - 5);
tft.print(i);
}
}
void drawClockHands() {
static int prevSecond = -1;
static int prevMinute = -1;
static int prevHour = -1;
int hours = timeClient.getHours() % 12;
int minutes = timeClient.getMinutes();
int seconds = timeClient.getSeconds();
float hourAngle = (hours + minutes / 60.0) * 30 * PI / 180;
float minuteAngle = (minutes + seconds / 60.0) * 6 * PI / 180;
float secondAngle = seconds * 6 * PI / 180;
int hx = clockCenterX + 50 * cos(hourAngle - PI / 2);
int hy = clockCenterY + 50 * sin(hourAngle - PI / 2);
int mx = clockCenterX + 70 * cos(minuteAngle - PI / 2);
int my = clockCenterY + 70 * sin(minuteAngle - PI / 2);
int sx = clockCenterX + 90 * cos(secondAngle - PI / 2);
int sy = clockCenterY + 90 * sin(secondAngle - PI / 2);
if (prevSecond != seconds) {
// Clear the previous hands
if (prevHour != -1 && prevMinute != -1 && prevSecond != -1) {
float prevHourAngle = (prevHour + prevMinute / 60.0) * 30 * PI / 180;
float prevMinuteAngle = (prevMinute + prevSecond / 60.0) * 6 * PI / 180;
float prevSecondAngle = prevSecond * 6 * PI / 180;
int prevHx = clockCenterX + 50 * cos(prevHourAngle - PI / 2);
int prevHy = clockCenterY + 50 * sin(prevHourAngle - PI / 2);
int prevMx = clockCenterX + 70 * cos(prevMinuteAngle - PI / 2);
int prevMy = clockCenterY + 70 * sin(prevMinuteAngle - PI / 2);
int prevSx = clockCenterX + 90 * cos(prevSecondAngle - PI / 2);
int prevSy = clockCenterY + 90 * sin(prevSecondAngle - PI / 2);
tft.drawLine(clockCenterX, clockCenterY, prevHx, prevHy, BLACK);
tft.drawLine(clockCenterX, clockCenterY, prevMx, prevMy, BLACK);
tft.drawLine(clockCenterX, clockCenterY, prevSx, prevSy, BLACK);
}
// Draw the new hands
tft.drawLine(clockCenterX, clockCenterY, hx, hy, RED);
tft.drawLine(clockCenterX, clockCenterY, mx, my, GREEN);
tft.drawLine(clockCenterX, clockCenterY, sx, sy, YELLOW);
// Update previous values
prevHour = hours;
prevMinute = minutes;
prevSecond = seconds;
}
}