#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the I2C address if necessary
#define BLUE_BUTTON_PIN 3
#define YELLOW_BUTTON_PIN 4
#define BUZZER_PIN 5
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;
unsigned long gameTimer = 0;
unsigned long countdownTimer = 0;
unsigned long gameStartTime = 0;
unsigned long countdownStartTime = 0;
bool gameStarted = false;
bool countdownStarted = false;
bool gameEnded = false;
int gameTimeOptions[] = {1, 5, 20, 30, 45, 60}; // Minutes
int countdownOptions[] = {1, 5, 10, 15}; // Minutes
int gameTimeSelection = 0;
int countdownSelection = 0;
unsigned long resetPressStartTime = 0;
bool resetTimerStarted = false;
void displayLabels() {
lcd.setCursor(0, 0);
lcd.print("M");
lcd.setCursor(8, 0);
lcd.print("S");
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(BLUE_BUTTON_PIN, INPUT_PULLUP);
pinMode(YELLOW_BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
setupGame();
}
void setupGame() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Game Time:");
lcd.setCursor(0, 1);
lcd.print(gameTimeOptions[gameTimeSelection]);
lcd.print(" min");
while (!countdownStarted) {
if (digitalRead(YELLOW_BUTTON_PIN) == LOW) {
delay(200);
gameTimeSelection = (gameTimeSelection + 1) % 4;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous text
lcd.setCursor(0, 1);
lcd.print(gameTimeOptions[gameTimeSelection]);
lcd.print(" min");
}
if (digitalRead(BLUE_BUTTON_PIN) == LOW) {
delay(200);
gameTimer = gameTimeOptions[gameTimeSelection] * 60000;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Countdown:");
lcd.setCursor(0, 1);
lcd.print(countdownOptions[countdownSelection]);
lcd.print(" min");
while (!countdownStarted) {
if (digitalRead(YELLOW_BUTTON_PIN) == LOW) {
delay(200);
countdownSelection = (countdownSelection + 1) % 3;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear previous text
lcd.setCursor(0, 1);
lcd.print(countdownOptions[countdownSelection]);
lcd.print(" min");
}
if (digitalRead(BLUE_BUTTON_PIN) == LOW) {
delay(200);
countdownTimer = countdownOptions[countdownSelection] * 60000;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Click Start");
while (!gameStarted) {
if (digitalRead(BLUE_BUTTON_PIN) == LOW) {
delay(200);
countdownStarted = true;
countdownStartTime = millis();
lcd.clear();
break;
}
}
}
}
}
}
}
void loop() {
unsigned long currentMillis = millis();
if (digitalRead(BLUE_BUTTON_PIN) == LOW && digitalRead(YELLOW_BUTTON_PIN) == LOW) {
if (!resetTimerStarted) {
resetPressStartTime = currentMillis;
resetTimerStarted = true;
} else if (currentMillis - resetPressStartTime >= 10000) {
resetDevice();
}
} else {
resetTimerStarted = false;
}
if (gameEnded) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("GAME END");
if (digitalRead(YELLOW_BUTTON_PIN) == LOW) {
delay(200);
gameEnded = false;
gameStarted = false;
countdownStarted = false;
setupGame();
}
return;
}
if (countdownStarted && !gameStarted) {
unsigned long elapsed = currentMillis - countdownStartTime;
if (elapsed >= countdownTimer) {
gameStarted = true;
gameStartTime = millis();
countdownStarted = false;
lcd.clear();
displayLabels();
} else {
lcd.setCursor(0, 0);
lcd.print("Countdown");
lcd.setCursor(0, 1);
lcd.print(formatTime(countdownTimer - elapsed));
}
}
if (gameStarted && !gameEnded) {
unsigned long elapsed = currentMillis - gameStartTime;
if (elapsed >= gameTimer) {
gameEnded = true;
} else {
lcd.setCursor(11, 1);
lcd.print(formatTime(gameTimer - elapsed));
}
}
if (!countdownStarted && !gameStarted) {
return;
}
if (ObjectiveHeldBlue) {
blueObjectiveHoldingTime = currentMillis - blueObjectiveStartTime + blueTotalTime;
}
if (ObjectiveHeldYellow) {
yellowObjectiveHoldingTime = currentMillis - yellowObjectiveStartTime + yellowTotalTime;
}
// Update only the dynamic content
lcd.setCursor(2, 0);
lcd.print(formatTime(blueObjectiveHoldingTime));
lcd.setCursor(10, 0);
lcd.print(formatTime(yellowObjectiveHoldingTime));
if (!countdownStarted && gameStarted && !gameEnded) {
lcd.setCursor(0, 1);
if (ObjectiveHeldYellow) {
lcd.print("SARI TAKIM ");
} else if (ObjectiveHeldBlue) {
lcd.print("MAVI TAKIM ");
} else {
lcd.print("TARAFSIZ ");
}
}
handleButtonPresses();
}
String formatTime(unsigned long timeMillis) {
unsigned long seconds = (timeMillis / 1000) % 60;
unsigned long totalMinutes = timeMillis / 60000;
char buf[10];
sprintf(buf, "%lu:%02lu", totalMinutes, seconds);
return String(buf);
}
void refreshLCD() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("M");
lcd.setCursor(8, 0);
lcd.print("S");
}
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.setCursor(0, 1);
lcd.print(" ");
blueButtonPressStartTime = millis();
while (digitalRead(BLUE_BUTTON_PIN) == LOW) {
elapsed = millis() - blueButtonPressStartTime;
lcd.setCursor(0, 1);
lcd.print("ALINIYOR");
if (elapsed / 1000 >= lastSecond + 1 && lastSecond < 5) {
lcd.setCursor(8 + lastSecond * 2, 1);
lcd.print(" >");
tone(BUZZER_PIN, 440, 250);
lastSecond++;
}
if (elapsed >= 5000) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("MAVI ALDI!");
tone(BUZZER_PIN, 880, 750);
delay(2000);
lcd.clear();
ObjectiveHeldBlue = true;
ObjectiveHeldYellow = false;
blueSavedTime = blueObjectiveHoldingTime;
blueTotalTime = blueSavedTime;
blueObjectiveStartTime = millis();
refreshLCD();
break;
}
}
}
// Process yellow button press for neutral to yellow transition
if (!ObjectiveHeldBlue && !ObjectiveHeldYellow && digitalRead(YELLOW_BUTTON_PIN) == LOW) {
lcd.clear();
yellowButtonPressStartTime = millis();
while (digitalRead(YELLOW_BUTTON_PIN) == LOW) {
elapsed = millis() - yellowButtonPressStartTime;
lcd.setCursor(0, 1);
lcd.print("ALINIYOR");
if (elapsed / 1000 >= lastSecond + 1 && lastSecond < 5) {
lcd.setCursor(8 + lastSecond * 2, 1);
lcd.print(" >");
tone(BUZZER_PIN, 440, 250);
lastSecond++;
}
if (elapsed >= 5000) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("SARI ALDI!");
tone(BUZZER_PIN, 880, 750);
delay(2000);
lcd.clear();
ObjectiveHeldBlue = false;
ObjectiveHeldYellow = true;
yellowSavedTime = yellowObjectiveHoldingTime;
yellowTotalTime = yellowSavedTime;
yellowObjectiveStartTime = millis();
refreshLCD();
break;
}
}
}
// Handle transitions from blue or yellow to neutral
if ((ObjectiveHeldBlue || ObjectiveHeldYellow) && (digitalRead(BLUE_BUTTON_PIN) == LOW || digitalRead(YELLOW_BUTTON_PIN) == LOW)) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("KURTARILIYOR");
unsigned long buttonPressStartTime = millis();
while (digitalRead(BLUE_BUTTON_PIN) == LOW || digitalRead(YELLOW_BUTTON_PIN) == LOW) {
elapsed = millis() - buttonPressStartTime;
if (elapsed / 1000 >= lastSecond + 1 && lastSecond < 5) {
lcd.setCursor(12 + lastSecond, 1);
lcd.print(">");
tone(BUZZER_PIN, 440, 250);
lastSecond++;
}
if (elapsed >= 5000) {
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("KURTARILDI!");
tone(BUZZER_PIN, 880, 750);
delay(2000);
refreshLCD();
ObjectiveHeldBlue = false;
ObjectiveHeldYellow = false;
break;
}
}
}
}
void resetDevice() {
lcd.clear();
resetTimerStarted = false;
gameStarted = false;
countdownStarted = false;
gameEnded = false;
setupGame();
}