/********* Pleasedontcode.com ********** Pleasedontcode thanks you for automatic code generation! Enjoy your code! - Terms and Conditions: You have a non-exclusive, revocable, worldwide, royalty-free license for personal and commercial use. Attribution is optional; modifications are allowed, but you're responsible for code maintenance. We're not liable for any loss or damage. For full terms, please visit pleasedontcode.com/termsandconditions. - Project: LCD Timer - Source Code compiled for: Arduino Mega - Source Code created on: 2024-04-04 15:33:53 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* create a 5 minutes timer that starts when i push */ /* the button and stops when i push it again */ /****** END SYSTEM REQUIREMENTS *****/ /****** DEFINITION OF LIBRARIES *****/ #include #include // https://github.com/marcoschwartz/LiquidCrystal_I2C #include // https://github.com/evert-arias/EasyButton /****** FUNCTION PROTOTYPES *****/ void setup(); void loop(); /***** DEFINITION OF DIGITAL INPUT PINS *****/ const uint8_t BUTTON_PIN = 2; /***** DEFINITION OF I2C PINS *****/ const uint8_t LCD_I2C_PIN_SDA = 20; const uint8_t LCD_I2C_PIN_SCL = 21; const uint8_t LCD_I2C_SLAVE_ADDRESS = 39; /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/ LiquidCrystal_I2C lcd(LCD_I2C_SLAVE_ADDRESS, 20, 4); // Initialize the LiquidCrystal_I2C class instance EasyButton pushButton(BUTTON_PIN); // Initialize the EasyButton with the button pin bool timerRunning = false; unsigned long timerStartTime; const unsigned long TIMER_DURATION = 5 * 60 * 1000; // 5 minutes in milliseconds void onPressedForDuration() { if (timerRunning) { // Stop the timer unsigned long elapsed = millis() - timerStartTime; timerRunning = false; // Display the elapsed time on the LCD lcd.setCursor(0, 0); lcd.print("Elapsed Time: "); lcd.setCursor(0, 1); lcd.print(elapsed / 1000); lcd.print(" seconds"); } else { // Start the timer timerStartTime = millis(); timerRunning = true; // Clear the LCD lcd.clear(); } } void setup() { // put your setup code here, to run once: pushButton.begin(); // Initialize the EasyButton pushButton.onPressedFor(2000, onPressedForDuration); // Add the callback function to be called when the button is pressed for at least the given time. lcd.begin(20, 4); // Initialize the LCD } void loop() { // put your main code here, to run repeatedly: pushButton.read(); // Continuously read the status of the button. // Check if the timer has reached the desired duration if (timerRunning && millis() - timerStartTime >= TIMER_DURATION) { // Stop the timer timerRunning = false; // Display the timer finished message on the LCD lcd.setCursor(0, 0); lcd.print("Timer Finished!"); } }