Advertisement
pleasedontcode

LCD Button rev_05

May 3rd, 2024
888
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 Button
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-05-03 14:37:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project must control a timer that starts when */
  21.     /* the infrared sensor is in a low active state and */
  22.     /* stops when the sensor returns to a low active */
  23.     /* state. The timer must be displayed on the LCD */
  24.     /* screen. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* The timer state should be handled using a state */
  27.     /* machine. Furthermore, an explicit function called */
  28.     /* 'resetTimer' needs to be implemented to reset the */
  29.     /* counter and state of the state machine. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /****** DEFINITION OF LIBRARIES *****/
  33. #include <Wire.h>
  34. #include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
  35. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void resetTimer(void);
  41.  
  42. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  43. const uint8_t sensorInfrared_PushButton_PIN_D2 = 2;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  47. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  48. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
  49.  
  50. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  51. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize the LCD object with the I2C address, columns, and rows
  52. EasyButton button(sensorInfrared_PushButton_PIN_D2); // Initialize the EasyButton object with the button pin
  53.  
  54. // Timer variables
  55. unsigned long startTime = 0;
  56. unsigned long elapsedTime = 0;
  57. bool timerRunning = false;
  58.  
  59. void setup(void)
  60. {
  61.     // put your setup code here, to run once:
  62.     pinMode(sensorInfrared_PushButton_PIN_D2, INPUT_PULLUP);
  63.  
  64.     lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
  65.     lcd.backlight(); // Turn on the backlight
  66.  
  67.     lcd.setCursor(0, 0);
  68.     lcd.print("Hello, world!");
  69.  
  70.     // Add the EasyButton setup code here
  71.     button.begin();
  72.     button.onPressedFor(2000, onPressedForDuration);
  73. }
  74.  
  75. void loop(void)
  76. {
  77.     // put your main code here, to run repeatedly:
  78.     button.read();
  79.  
  80.     // Check the infrared sensor state
  81.     if (digitalRead(sensorInfrared_PushButton_PIN_D2) == LOW) {
  82.         if (!timerRunning) {
  83.             startTime = millis();
  84.             timerRunning = true;
  85.         }
  86.         elapsedTime = millis() - startTime;
  87.         lcd.setCursor(0, 1);
  88.         lcd.print("Timer: ");
  89.         lcd.print(elapsedTime / 1000); // Display elapsed time in seconds
  90.     }
  91.     else {
  92.         resetTimer();
  93.     }
  94. }
  95.  
  96. // Callback function to be called when the button is pressed.
  97. void onPressedForDuration()
  98. {
  99.     // Code to be executed when the button is pressed for the given duration.
  100. }
  101.  
  102. // Function to reset the timer and LCD display
  103. void resetTimer()
  104. {
  105.     startTime = 0;
  106.     elapsedTime = 0;
  107.     timerRunning = false;
  108.     lcd.setCursor(0, 1);
  109.     lcd.print("Timer: 0     "); // Clear the timer display
  110. }
  111.  
  112. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement