#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#define TFT_CS 15
#define TFT_DC 4
#define TFT_RST -1
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
int hours = 0, minutes = 0, seconds = 0;
bool running = false;
bool buttonPressed = false;
const int startStopPin = 34;
const int resetPin = 35;
unsigned long previousMillis = 0;
const long interval = 1000; // Update the display every 1 second
void setup() {
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_BLACK); // Set the background color to black
// Create separate round rectangles for hours, minutes, and seconds
tft.fillRoundRect(20, 50, 80, 100, 10, ILI9341_DARKGREY); // Hours background (left)
tft.fillRoundRect(120, 50, 80, 100, 10, ILI9341_DARKGREY); // Minutes background (center)
tft.fillRoundRect(220, 50, 80, 100, 10, ILI9341_DARKGREY); // Seconds background (right)
// Label
tft.setCursor(80, 10);
tft.setTextColor(ILI9341_WHITE); // White text
tft.setTextSize(3);
tft.print("Stopwatch");
// Start/Stop Button
tft.fillRoundRect(60, 170, 100, 40, 8, running ? ILI9341_RED : ILI9341_GREEN); // Red for Stop, Green for Start
tft.setCursor(85, 178);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print(running ? "Stop" : "Start");
// Reset Button
tft.fillRoundRect(180, 170, 100, 40, 8, ILI9341_BLUE); // Blue for Reset
tft.setCursor(215, 178);
tft.setTextColor(ILI9341_WHITE); // White text
tft.print("Reset");
// Initial display
updateDisplay();
}
void updateDisplay() {
tft.setTextColor(ILI9341_WHITE); // White text
tft.setTextSize(4);
// Display hours in the left rectangle
tft.setCursor(35, 70);
tft.print(hours < 10 ? "0" : "");
tft.print(hours);
// Display minutes in the center rectangle
tft.setCursor(135, 70);
tft.print(minutes < 10 ? "0" : "");
tft.print(minutes);
// Display seconds in the right rectangle
tft.setCursor(235, 70);
tft.print(seconds < 10 ? "0" : "");
tft.print(seconds);
}
void loop() {
int startStopState = digitalRead(startStopPin);
int resetState = digitalRead(resetPin);
unsigned long currentMillis = millis();
if (startStopState == HIGH) {
if (!buttonPressed) {
buttonPressed = true;
running = !running;
tft.fillRoundRect(60, 170, 100, 40, 8, running ? ILI9341_RED : ILI9341_GREEN);
tft.setCursor(85, 178);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(2);
tft.print(running ? "Stop" : "Start");
}
} else {
buttonPressed = false;
}
if (resetState == HIGH) {
if (!running) {
hours = 0, minutes = 0, seconds = 0;
updateDisplay();
}
}
if (running && currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
if (hours == 24) {
hours = 0;
}
}
}
updateDisplay();
}
}