#include <LiquidCrystal_I2C.h>
const int LCD_ADDRESS = 0x27;
const int LCD_COLUMNS = 16;
const int LCD_ROWS = 2;
const int RED_BUTTON_PIN = 4; // Pin connected to the red push button
const int YELLOW_BUTTON_PIN = 16; // Pin connected to the yellow push button
const int GREEN_BUTTON_PIN = 0; // Pin connected to the green push button
const int RED_LED_PIN = 18; // Pin connected to the red LED
const int YELLOW_LED_PIN = 5; // Pin connected to the yellow LED
const int GREEN_LED_PIN = 19; // Pin connected to the green LED
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
String staticMessage = "";
unsigned long startTime = 0;
bool isTimerStarted = false; // Renamed to avoid naming conflict
int lastPressedButton = -1; // -1 indicates no button was pressed initially
void scrollText(int row, String message, int delayTime, int lcdColumns) {
message = String(message.length() + lcdColumns, ' ') + message + String(lcdColumns, ' ');
for (int pos = 0; pos < message.length() - lcdColumns + 1; pos++) {
lcd.setCursor(0, row);
lcd.print(message.substring(pos, pos + lcdColumns));
delay(delayTime);
}
}
void displayMessage(String message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
}
void displayTimer(int row) {
unsigned long elapsedSeconds = (millis() - startTime) / 1000;
unsigned long hours = elapsedSeconds / 3600;
unsigned long minutes = (elapsedSeconds % 3600) / 60;
unsigned long seconds = elapsedSeconds % 60;
lcd.setCursor(0, row);
lcd.print("Timer: ");
lcd.print(hours < 10 ? "0" : "");
lcd.print(hours);
lcd.print(":");
lcd.print(minutes < 10 ? "0" : "");
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds < 10 ? "0" : "");
lcd.print(seconds);
}
void setup() {
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
pinMode(YELLOW_BUTTON_PIN, INPUT_PULLUP);
pinMode(GREEN_BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
if (digitalRead(RED_BUTTON_PIN) == LOW && lastPressedButton != RED_BUTTON_PIN) {
staticMessage = "STOPPED";
startTime = millis();
isTimerStarted = true;
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
displayMessage(staticMessage);
lastPressedButton = RED_BUTTON_PIN;
} else if (digitalRead(YELLOW_BUTTON_PIN) == LOW && lastPressedButton != YELLOW_BUTTON_PIN) {
staticMessage = "BREAKDOWN";
startTime = millis();
isTimerStarted = true;
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
displayMessage(staticMessage);
lastPressedButton = YELLOW_BUTTON_PIN;
} else if (digitalRead(GREEN_BUTTON_PIN) == LOW && lastPressedButton != GREEN_BUTTON_PIN) {
staticMessage = "RUNNING";
startTime = millis();
isTimerStarted = true;
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
displayMessage(staticMessage);
lastPressedButton = GREEN_BUTTON_PIN;
}
if (isTimerStarted) {
displayTimer(1);
}
delay(1000);
}