#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.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() {
tft.begin();
tft.setRotation(3);
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__)));
}
Serial.begin(9600);
}
void drawClock() {
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
// Clear previous clock hands
tft.fillCircle(160, 120, 90, ILI9341_BLACK);
// Draw clock face
tft.fillCircle(160, 120, 100, ILI9341_CASET);
tft.fillCircle(160, 120, 5, ILI9341_RED);
// Draw hour hand
int hourHandLength = 50;
int hourHandAngle = map(hour % 12, 0, 12, 0, 360) - 90;
drawHand(160, 120, hourHandLength, hourHandAngle, ILI9341_PWCTR1);
// Draw minute hand
int minuteHandLength = 70;
int minuteHandAngle = map(minute, 0, 60, 0, 360) - 90;
drawHand(160, 120, minuteHandLength, minuteHandAngle, ILI9341_RED);
// Draw second hand
int secondHandLength = 90;
int secondHandAngle = map(second, 0, 60, 0, 360) - 90;
drawHand(160, 120, secondHandLength, secondHandAngle, ILI9341_YELLOW);
}
void drawHand(int x, int y, int length, int angle, uint16_t color) {
int x2 = x + length * cos(angle * PI / 180);
int y2 = y + length * sin(angle * PI / 180);
tft.drawLine(x, y, x2, y2, color);
}
void loop() {
drawClock();
delay(5000); // Update every second
}
led chasing on tft code by arvind.
अरविन्द पाटील 21/11/23 .