#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// Pin definitions for the ILI9341
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
// Create an instance of the ILI9341 display driver
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
unsigned long previousMillis = 0;
const long interval = 1000; // interval at which to update the clock (milliseconds)
int seconds = 0;
int minutes = 0;
int hours = 0;
const int clockRadius = 100;
const int centerX = 120;
const int centerY = 120;
const int secondHandLength = clockRadius - 10;
const int minuteHandLength = clockRadius - 25;
const int hourHandLength = clockRadius - 40;
const int handThickness = 9; // Increase thickness for the hour and minute hands
void setup() {
// Initialize the display
tft.begin();
// Set the rotation of the display for vertical orientation
tft.setRotation(0); // Adjust if necessary
// Draw the static clock display
drawClock();
hours = 03; // example starting time: 03:00:00
minutes = 0;
seconds = 0;
tft.fillScreen(ILI9341_BLACK);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
if (minutes >= 60) {
minutes = 0;
hours++;
if (hours >= 24) {
hours = 0;
}
}
}
// Redraw the screen with the new time
drawClock();
}
}
void drawClock() {
int centerCircleRadius = 4; // Radius of the center circle, adjust as needed
tft.fillCircle(centerX, centerY, centerCircleRadius, ILI9341_WHITE);
// Draw the clock face with a blue circle
tft.drawCircle(centerX, centerY, clockRadius, ILI9341_BLUE);
tft.drawCircle(centerX, centerY, clockRadius + 1, ILI9341_BLUE);
tft.drawCircle(centerX, centerY, clockRadius + 2, ILI9341_BLUE);
tft.drawCircle(centerX, centerY, clockRadius + 3, ILI9341_BLUE);
// Draw the hour markers
for (int i = 0; i < 12; i++) {
float angle = i * 30;
float sx = cos(angle * DEG_TO_RAD) * (clockRadius - 10);
float sy = sin(angle * DEG_TO_RAD) * (clockRadius - 10);
float ex = cos(angle * DEG_TO_RAD) * clockRadius;
float ey = sin(angle * DEG_TO_RAD) * clockRadius;
tft.drawLine(centerX + sx, centerY - sy, centerX + ex, centerY - ey, ILI9341_WHITE);
}
// Draw the 12, 3, 6, and 9 numbers
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(2);
tft.setCursor(centerX - 10, centerY - clockRadius + 10); tft.print("12");
tft.setCursor(centerX + clockRadius - 20, centerY - 8); tft.print("3");
tft.setCursor(centerX - 10, centerY + clockRadius - 30); tft.print("6");
tft.setCursor(centerX - clockRadius + 10, centerY - 8); tft.print("9");
// Clear previous hands by drawing over them in black
static float prevHrAngle = 0, prevMinAngle = 0, prevSecAngle = 0;
drawTaperedHand(centerX, centerY, prevHrAngle, hourHandLength, ILI9341_BLACK, 6);
drawTaperedHand(centerX, centerY, prevMinAngle, minuteHandLength, ILI9341_BLACK, 5);
drawTaperedHand(centerX, centerY, prevSecAngle, secondHandLength, ILI9341_BLACK, 2);
// Draw new hands
int hrAngle = map((hours % 12) * 5 + minutes / 12, 0, 60, 0, 360) - 90;
int minAngle = map(minutes, 0, 60, 0, 360) - 90;
int secAngle = map(seconds, 0, 60, 0, 360) - 90;
// Draw new hands with the specified thickness
drawTaperedHand(centerX, centerY, hrAngle, hourHandLength, ILI9341_YELLOW, 6);
drawTaperedHand(centerX, centerY, minAngle, minuteHandLength, ILI9341_BLUE, 5);
drawTaperedHand(centerX, centerY, secAngle, secondHandLength, ILI9341_RED, 2);
// Update previous hand positions
prevHrAngle = hrAngle;
prevMinAngle = minAngle;
prevSecAngle = secAngle;
// Draw the digital time
int timeTextLength = 13 *25; // Approximate width of the text (6 characters, each character approx. 12 pixels wide)
int timeTextHeight = 18; // Approximate height of the text (18 pixels high)
int timeX = 60; // X position of the time text
int timeY = 260; // Y position of the time text
// Clear the previous time by drawing a rectangle filled with the background color
tft.fillRect(timeX, timeY, timeTextLength, timeTextHeight, ILI9341_BLACK);
// Set the cursor position to the starting point of the time
tft.setCursor(timeX, timeY);
// Set the text color and size for the digital time
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
// Print the time with leading zeros for hours, minutes, and seconds
tft.print(hours < 10 ? "0" : "");
tft.print(hours);
tft.print(':');
tft.print(minutes < 10 ? "0" : "");
tft.print(minutes);
tft.print(':');
tft.print(seconds < 10 ? "0" : "");
tft.print(seconds);
}
void drawTaperedHand(int x, int y, float angle, int length, uint16_t color, int baseWidth) {
angle = radians(angle); // Convert angle to radians for trigonometric functions
// Calculate the end point of the hand
int endX = x + length * cos(angle);
int endY = y + length * sin(angle);
// Calculate the points for the base of the hand
int baseHalfWidth = baseWidth / 2;
int baseLeftX = x - baseHalfWidth * sin(angle);
int baseLeftY = y + baseHalfWidth * cos(angle);
int baseRightX = x + baseHalfWidth * sin(angle);
int baseRightY = y - baseHalfWidth * cos(angle);
// Draw the hand as a filled shape (a tapered line)
tft.fillTriangle(baseLeftX, baseLeftY, baseRightX, baseRightY, endX, endY, color);
}