#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <SPI.h>

#define TFT_CS     7
#define TFT_RST    4
#define TFT_DC     9
#define TFT_SCK    18
#define TFT_MOSI   19
#define RTC_SDA    2
#define RTC_SCL    3

RTC_DS1307 rtc;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);

void setup() {
  // Initialize SPI for TFT
  SPI.begin(TFT_SCK, -1, TFT_MOSI, -1);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_BLACK);

  // Initialize I2C for RTC
  Wire.begin(RTC_SDA, RTC_SCL);
  if (!rtc.begin()) {
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(2);
    tft.setCursor(10, 10);
    tft.println("Couldn't find RTC");
    while (1);
  }
  
  if (!rtc.isrunning()) {
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(2);
    tft.setCursor(10, 40);
    tft.println("RTC is NOT running!");
    // Uncomment to set the time:
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  
  drawClockFace();
}

void loop() {
  drawClockHands();
  delay(1000); // Update every second
}

void drawClockFace() {
  int centerX = tft.width() / 2;
  int centerY = tft.height() / 2;
  int radius = 100;
  
  tft.fillScreen(ILI9341_BLACK);
  tft.drawCircle(centerX, centerY, radius, ILI9341_WHITE);

  for (int i = 0; i < 12; i++) {
    float angle = i * 30 * PI / 180;
    int x1 = centerX + (radius - 10) * cos(angle);
    int y1 = centerY + (radius - 10) * sin(angle);
    int x2 = centerX + radius * cos(angle);
    int y2 = centerY + radius * sin(angle);
    tft.drawLine(x1, y1, x2, y2, ILI9341_WHITE);
  }
}

void drawClockHands() {
  int centerX = tft.width() / 2;
  int centerY = tft.height() / 2;

  DateTime now = rtc.now();
  int hours = now.hour();
  int minutes = now.minute();
  int seconds = now.second();

  // Clear previous hands
  drawClockFace();

  // Draw hour hand
  float hourAngle = ((hours % 12) + minutes / 60.0) * 30 * PI / 180;
  int hourX = centerX + 50 * cos(hourAngle - PI / 2);
  int hourY = centerY + 50 * sin(hourAngle - PI / 2);
  tft.drawLine(centerX, centerY, hourX, hourY, ILI9341_WHITE);

  // Draw minute hand
  float minuteAngle = (minutes + seconds / 60.0) * 6 * PI / 180;
  int minuteX = centerX + 80 * cos(minuteAngle - PI / 2);
  int minuteY = centerY + 80 * sin(minuteAngle - PI / 2);
  tft.drawLine(centerX, centerY, minuteX, minuteY, ILI9341_WHITE);

  // Draw second hand
  float secondAngle = seconds * 6 * PI / 180;
  int secondX = centerX + 90 * cos(secondAngle - PI / 2);
  int secondY = centerY + 90 * sin(secondAngle - PI / 2);
  tft.drawLine(centerX, centerY, secondX, secondY, ILI9341_RED);
}
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:18
esp:19
esp:GND.1
esp:3V3.1
esp:3V3.2
esp:GND.2
esp:RST
esp:GND.3
esp:GND.4
esp:5V.1
esp:5V.2
esp:GND.5
esp:GND.6
esp:GND.7
esp:GND.8
esp:GND.9
esp:RX
esp:TX
esp:GND.10
lcd1:VCC
lcd1:GND
lcd1:CS
lcd1:RST
lcd1:D/C
lcd1:MOSI
lcd1:SCK
lcd1:LED
lcd1:MISO
CODE LINK https://wokwi.com/projects/405392386172420097
अरविन्द पाटील 05/08/24.
Define TFT_CS 7, TFT_RST 4, TFT_DC 9, TFT_SCK 18,TFT_MOSI 19.
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW