#include <Arduino.h>
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4);
const int buttonPin = 2;
const int redPin = 12;
const int greenPin = 14;
const int bluePin = 27;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
unsigned long lastUpdateTime = 0;
bool running = false;
int colorIndex = 0;
const int numColors = 4;
const int colors[numColors][3] = {
{0, 255, 0}, // Green
{255, 255, 0}, // Yellow
{255, 0, 0}, // Red
{0, 0, 255} // Blue
};
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Press to Start");
pinMode(buttonPin, INPUT_PULLUP);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonInterrupt, FALLING);
setColor(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2]);
// WiFi setup (for future use)
// WiFi.begin("SSID", "PASSWORD");
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
// Serial.println("Connecting to WiFi...");
// }
// Serial.println("Connected to WiFi");
}
void loop() {
unsigned long currentTime = millis();
if (running && (currentTime - lastUpdateTime >= 100)) { // Update every 100ms
elapsedTime = currentTime - startTime;
updateDisplay();
lastUpdateTime = currentTime;
}
}
void buttonInterrupt() {
if (!running) {
startTime = millis() - elapsedTime; // Resume from last stopped time
running = true;
lcd.clear();
} else {
running = false;
}
// Change LED color
colorIndex = (colorIndex + 1) % numColors;
setColor(colors[colorIndex][0], colors[colorIndex][1], colors[colorIndex][2]);
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time:");
lcd.setCursor(0, 1);
lcd.print(formatTime(elapsedTime));
lcd.setCursor(0, 2);
lcd.print(running ? "Running" : "Paused");
}
String formatTime(unsigned long timeInMillis) {
unsigned long totalSeconds = timeInMillis / 1000;
unsigned long hours = totalSeconds / 3600;
unsigned long minutes = (totalSeconds % 3600) / 60;
unsigned long seconds = totalSeconds % 60;
unsigned long centiseconds = (timeInMillis % 1000) / 10;
char buffer[13];
snprintf(buffer, 13, "%02lu:%02lu:%02lu.%02lu", hours, minutes, seconds, centiseconds);
return String(buffer);
}
void setColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}