Advertisement
pleasedontcode

LCD Timer rev_01

Apr 4th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: LCD Timer
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2024-04-04 15:33:53
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* create a 5 minutes timer that starts when i push */
  21.     /* the button and stops when i push it again */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
  27. #include <EasyButton.h>  // https://github.com/evert-arias/EasyButton
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup();
  31. void loop();
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t BUTTON_PIN = 2;
  35.  
  36. /***** DEFINITION OF I2C PINS *****/
  37. const uint8_t LCD_I2C_PIN_SDA = 20;
  38. const uint8_t LCD_I2C_PIN_SCL = 21;
  39. const uint8_t LCD_I2C_SLAVE_ADDRESS = 39;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. LiquidCrystal_I2C lcd(LCD_I2C_SLAVE_ADDRESS, 20, 4); // Initialize the LiquidCrystal_I2C class instance
  43.  
  44. EasyButton pushButton(BUTTON_PIN); // Initialize the EasyButton with the button pin
  45.  
  46. bool timerRunning = false;
  47. unsigned long timerStartTime;
  48. const unsigned long TIMER_DURATION = 5 * 60 * 1000;  // 5 minutes in milliseconds
  49.  
  50. void onPressedForDuration()
  51. {
  52.   if (timerRunning) {
  53.     // Stop the timer
  54.     unsigned long elapsed = millis() - timerStartTime;
  55.     timerRunning = false;
  56.  
  57.     // Display the elapsed time on the LCD
  58.     lcd.setCursor(0, 0);
  59.     lcd.print("Elapsed Time: ");
  60.     lcd.setCursor(0, 1);
  61.     lcd.print(elapsed / 1000);
  62.     lcd.print(" seconds");
  63.   } else {
  64.     // Start the timer
  65.     timerStartTime = millis();
  66.     timerRunning = true;
  67.  
  68.     // Clear the LCD
  69.     lcd.clear();
  70.   }
  71. }
  72.  
  73. void setup()
  74. {
  75.   // put your setup code here, to run once:
  76.  
  77.   pushButton.begin(); // Initialize the EasyButton
  78.  
  79.   pushButton.onPressedFor(2000, onPressedForDuration);  // Add the callback function to be called when the button is pressed for at least the given time.
  80.  
  81.   lcd.begin(20, 4); // Initialize the LCD
  82.  
  83. }
  84.  
  85. void loop()
  86. {
  87.   // put your main code here, to run repeatedly:
  88.  
  89.   pushButton.read();  // Continuously read the status of the button.
  90.  
  91.   // Check if the timer has reached the desired duration
  92.   if (timerRunning && millis() - timerStartTime >= TIMER_DURATION) {
  93.     // Stop the timer
  94.     timerRunning = false;
  95.  
  96.     // Display the timer finished message on the LCD
  97.     lcd.setCursor(0, 0);
  98.     lcd.print("Timer Finished!");
  99.   }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement