#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address
#define Start 4 // start stop button
#define AddMinutesButton 5 // button to add 1 minute
int minutes = 0;
int seconds = 0;
int initialMinutes = 0; // Variable to store the initially set minutes
boolean timeState = false;
void setup() {
pinMode(Start, INPUT_PULLUP);
pinMode(AddMinutesButton, INPUT_PULLUP);
Serial.begin(9600); // output
lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" SETEAZA TIMP");
lcd.setCursor(0, 1);
lcd.print(" SI APASA START");
}
void loop() {
// Check if the add minutes button is pressed
if (digitalRead(AddMinutesButton) == LOW) {
minutes += 1; // Add 1 minute
if (minutes >= 60) { // Limit to 59 minutes
minutes = 59;
}
initialMinutes = minutes; // Save the initial value of minutes
updateDisplay();
delay(1000); // Debounce delay
}
// Check if the start button is pressed
if (digitalRead(Start) == LOW) {
timeState = true; // Set the timeState to true
}
// If timeState is true, start the timer automatically
if (timeState) {
timer();
}
}
void timer() {
while (timeState) {
if (seconds == 0 && minutes > 0) {
seconds = 60;
minutes -= 1;
}
if (seconds == 0 && minutes == 0) { // Countdown alarm
alarma(); // Trigger the alarm for 30 seconds
resetTimer(); // Call the function to reset the timer to the previous value
continue; // Restart the timer
}
// Sound alarm when less than 1 minute remains
if (minutes == 0 && seconds <= 60) {
if (seconds % 10 == 0 && seconds > 10) {
tone(11, 1000, 200); // Beep every 10 seconds
} else if (seconds <= 10) {
tone(11, 1500, 200); // Beep every second in the last 10 seconds
}
}
delay(992); // Delay for keeping time master setting
seconds -= 1;
updateDisplay();
// Allow stopping the timer with the start button
if (digitalRead(Start) == LOW) {
delay(1000);
timeState = false;
break;
}
}
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TIMP DE RESPAWN");
lcd.setCursor(0, 1);
lcd.print("ASTEAPTA: ");
if (minutes <= 9) {
lcd.print("0");
}
lcd.print(minutes);
lcd.print(":");
if (seconds <= 9) {
lcd.print("0");
}
lcd.print(seconds);
}
void alarma() {
unsigned long startTime = millis(); // Get the current time in milliseconds
while (millis() - startTime < 30000) { // Run the alarm for 30 seconds (30000 milliseconds)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" POTI IESI DIN");
lcd.setCursor(0, 1);
lcd.print(" RESPAWN !");
tone(11, 600, 250); // First beep
delay(250);
tone(11, 800, 250); // Second beep
delay(250);
}
}
void resetTimer() {
// Reset the minutes to the value initially set by the user
minutes = initialMinutes; // Use the value saved in initialMinutes
seconds = 0; // Reset seconds to 0
updateDisplay(); // Update the LCD display with the new time
}