#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust the I2C address if necessary
#define BLUE_BUTTON_PIN 3
#define RED_BUTTON_PIN 4
#define BUZZER_PIN 5
#define BLUE_LED_PIN 6
#define RED_LED_PIN 7
#define NAT_LED_PIN 8
unsigned long blueButtonPressStartTime = 0;
bool ObjectiveHeldBlue = false; // Boolean to check the state of Objective FOR BLUE
unsigned long yellowButtonPressStartTime = 0;
bool ObjectiveHeldYellow = false;
unsigned long blueObjectiveStartTime = 0;
unsigned long yellowObjectiveStartTime = 0;
unsigned long blueObjectiveHoldingTime = 0;
unsigned long yellowObjectiveHoldingTime = 0;
unsigned long blueSavedTime = 0;
unsigned long yellowSavedTime = 0;
unsigned long blueTotalTime = 0;
unsigned long yellowTotalTime = 0;
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP);
pinMode(RED_BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(NAT_LED_PIN, OUTPUT);
// Initial static display setup
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MAVI:");
lcd.setCursor(0, 1);
lcd.print("SARI:");
}
void hardwareCheck(){
if(digitalRead(BLUE_BUTTON_PIN) == LOW){
Serial.print("Blue button pressed");
}
if(digitalRead(RED_BUTTON_PIN) == LOW){
Serial.print("YELLOW button pressed");
}
}
void loop() {
unsigned long currentMillis = millis();
digitalWrite(BLUE_LED_PIN, ObjectiveHeldBlue ? HIGH : LOW);
digitalWrite(RED_LED_PIN, ObjectiveHeldYellow ? HIGH : LOW);
digitalWrite(NAT_LED_PIN, (!ObjectiveHeldBlue && !ObjectiveHeldYellow) ? HIGH : LOW);
if (ObjectiveHeldBlue) {
blueObjectiveHoldingTime = currentMillis - blueObjectiveStartTime;
}
if (ObjectiveHeldYellow) {
yellowObjectiveHoldingTime = currentMillis - yellowObjectiveStartTime;
}
// Update only the dynamic content
lcd.setCursor(7, 0); // Set cursor past the label
lcd.print(formatTime(blueObjectiveHoldingTime) + " "); // Extra spaces to clear old digits
lcd.setCursor(7, 1);
lcd.print(formatTime(yellowObjectiveHoldingTime) + " "); // Extra spaces to clear old digits
lcd.setCursor(15, 0);
lcd.print(formatTime(blueSavedTime)); // Extra spaces to clear old digits
lcd.setCursor(15, 1);
lcd.print(formatTime(yellowSavedTime) + " "); // Extra spaces to clear old digits
lcd.setCursor(0,2);
lcd.print(" ");
lcd.setCursor(6,3);
if (ObjectiveHeldYellow) {
lcd.print("SARILAR") ;}
if (ObjectiveHeldBlue) {
lcd.print("MAVILER") ;}
if (!ObjectiveHeldYellow & !ObjectiveHeldBlue) {
lcd.print("TARAFSIZ") ;}
handleButtonPresses();
}
String formatTime(unsigned long timeMillis) {
unsigned long seconds = (timeMillis / 1000) % 60;
unsigned long totalMinutes = timeMillis / 60000; // Calculate total minutes directly
char buf[10];
sprintf(buf, "%lu:%02lu", totalMinutes, seconds); // Use %lu for totalMinutes since it's a long unsigned int
return String(buf);
}
void refreshLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MAVI:");
lcd.setCursor(0, 1);
lcd.print("SARI:");
}
void handleButtonPresses() {
unsigned long elapsed;
unsigned long lastSecond = 0;
// Process blue button press for neutral to blue transition
if (!ObjectiveHeldBlue && !ObjectiveHeldYellow && digitalRead(BLUE_BUTTON_PIN) == LOW) {
lcd.clear();
blueButtonPressStartTime = millis();
while (digitalRead(BLUE_BUTTON_PIN) == LOW) {
elapsed = millis() - blueButtonPressStartTime;
lcd.setCursor(0, 2);
lcd.print("MAVILER ALIYOR...");
if (elapsed / 1000 >= lastSecond + 1 && lastSecond < 5) {
lcd.setCursor(2 + lastSecond * 5, 3);
lcd.print("*");
tone(BUZZER_PIN, 440, 250);
lastSecond++;
}
if (elapsed >= 5000) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MAVI:");
lcd.setCursor(0, 1);
lcd.print("SARI:");
lcd.setCursor(0, 2);
lcd.print("MAVILER ALDI!");
tone(BUZZER_PIN, 880, 750);
delay(2000);
lcd.clear();
ObjectiveHeldBlue = true;
ObjectiveHeldYellow = false;
blueSavedTime += blueObjectiveHoldingTime;
blueObjectiveStartTime = millis();
refreshLCD();
break;
}
}
}
// Process red button press for neutral to yellow transition
if (!ObjectiveHeldBlue && !ObjectiveHeldYellow && digitalRead(RED_BUTTON_PIN) == LOW) {
lcd.clear();
yellowButtonPressStartTime = millis();
while (digitalRead(RED_BUTTON_PIN) == LOW) {
elapsed = millis() - yellowButtonPressStartTime;
lcd.setCursor(0, 2);
lcd.print("SARILAR ALIYOR...");
if (elapsed / 1000 >= lastSecond + 1 && lastSecond < 5) {
lcd.setCursor(2 + lastSecond * 5, 3);
lcd.print("*");
tone(BUZZER_PIN, 440, 250);
lastSecond++;
}
if (elapsed >= 5000) {
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("SARILAR ALDI!");
tone(BUZZER_PIN, 880, 750);
delay(2000);
lcd.clear();
ObjectiveHeldBlue = false;
ObjectiveHeldYellow = true;
yellowSavedTime += yellowObjectiveHoldingTime;
yellowObjectiveStartTime = millis();
refreshLCD();
break;
}
}
}
// Handle transitions from blue or yellow to neutral
if ((ObjectiveHeldBlue || ObjectiveHeldYellow) && (digitalRead(BLUE_BUTTON_PIN) == LOW || digitalRead(RED_BUTTON_PIN) == LOW)) {
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("KURTARILIYOR...");
unsigned long buttonPressStartTime = millis();
while (digitalRead(BLUE_BUTTON_PIN) == LOW || digitalRead(RED_BUTTON_PIN) == LOW) {
elapsed = millis() - buttonPressStartTime;
if (elapsed / 1000 >= lastSecond + 1 && lastSecond < 5) {
lcd.setCursor(2 + lastSecond * 5, 3);
lcd.print("*");
tone(BUZZER_PIN, 440, 250);
lastSecond++;
}
if (elapsed >= 5000) {
refreshLCD();
lcd.setCursor(6, 2);
lcd.print("KURTARILDI!");
tone(BUZZER_PIN, 880, 750);
delay(2000);
refreshLCD();
ObjectiveHeldBlue = false;
ObjectiveHeldYellow = false;
break;
}
}
}
}