#include <WiFi.h>
#include <time.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Pin definitions
const int BUTTON_PIN = 26;
const int RED_PIN = 13;
const int GREEN_PIN = 12;
const int BLUE_PIN = 14;
// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// NTP Server
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 3600;
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Variables
enum Color {GREEN, YELLOW, RED, BLUE};
Color currentColor = GREEN;
unsigned long stopwatchStart = 0;
bool isStopwatchRunning = false;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
lcd.init();
lcd.backlight();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
setColor(currentColor);
updateDisplay();
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
delay(50); // Simple debounce
if (digitalRead(BUTTON_PIN) == LOW) {
currentColor = static_cast<Color>((currentColor + 1) % 4);
setColor(currentColor);
stopwatchStart = millis();
isStopwatchRunning = true;
updateDisplay();
while (digitalRead(BUTTON_PIN) == LOW); // Wait for button release
}
}
if (isStopwatchRunning && (millis() - stopwatchStart) % 1000 == 0) {
updateDisplay();
}
}
void setColor(Color color) {
digitalWrite(RED_PIN, color == RED || color == YELLOW ? HIGH : LOW);
digitalWrite(GREEN_PIN, color == GREEN || color == YELLOW ? HIGH : LOW);
digitalWrite(BLUE_PIN, color == BLUE ? HIGH : LOW);
}
void updateDisplay() {
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
return;
}
char currentTime[9];
strftime(currentTime, sizeof(currentTime), "%H:%M:%S", &timeinfo);
unsigned long stopwatchTime = (millis() - stopwatchStart) / 1000;
int minutes = stopwatchTime / 60;
int seconds = stopwatchTime % 60;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(currentTime);
lcd.setCursor(0, 1);
const char* colorNames[] = {"GREEN", "YELLOW", "RED", "BLUE"};
lcd.printf("%s - %02d:%02d", colorNames[currentColor], minutes, seconds);
}