Advertisement
pleasedontcode

"Timer Trigger" rev_07

Jun 15th, 2024
399
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: "Timer Trigger"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-15 11:02:26
  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 buttonPressedTwoSeconds();
  41. void buttonISR();
  42. void resetTimer();
  43.  
  44. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  45. const uint8_t sensoreInfrarossi_PushButton_PIN_D2 = 2;
  46.  
  47. /***** DEFINITION OF I2C PINS *****/
  48. const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  49. const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  50. const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Typically 0x27 for most I2C LCDs
  51.  
  52. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  53. LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize with address, columns, and rows
  54. EasyButton button(sensoreInfrarossi_PushButton_PIN_D2); // Initialize EasyButton with the button pin
  55.  
  56. /****** TIMER VARIABLES *****/
  57. enum TimerState { STOPPED, RUNNING };
  58. TimerState timerState = STOPPED;
  59. unsigned long startTime = 0;
  60. unsigned long elapsedTime = 0;
  61.  
  62. void buttonPressedTwoSeconds() {
  63.   Serial.println("Button pressed for two seconds");
  64. }
  65.  
  66. void buttonISR() {
  67.   button.read();
  68. }
  69.  
  70. void resetTimer() {
  71.   timerState = STOPPED;
  72.   elapsedTime = 0;
  73.   lcd.setCursor(0, 1);
  74.   lcd.print("Timer: 0s      ");
  75. }
  76.  
  77. void setup(void)
  78. {
  79.     // put your setup code here, to run once:
  80.     Serial.begin(115200); // Initialize serial communication
  81.     Serial.println();
  82.     Serial.println(">>> EasyButton onPressedFor interrupt example <<<");
  83.  
  84.     pinMode(sensoreInfrarossi_PushButton_PIN_D2, INPUT_PULLUP);
  85.  
  86.     lcd.init();       // Initialize the LCD
  87.     lcd.backlight();  // Turn on the backlight
  88.  
  89.     // Display initial messages
  90.     lcd.setCursor(3, 0);
  91.     lcd.print("Hello, world!");
  92.     lcd.setCursor(2, 1);
  93.     lcd.print("Ywrobot Arduino!");
  94.     lcd.setCursor(0, 2);
  95.     lcd.print("Arduino LCM IIC 1602");
  96.     lcd.setCursor(2, 3);
  97.     lcd.print("Power By Ec-yuan!");
  98.  
  99.     button.begin(); // Initialize the button
  100.     button.onPressedFor(2000, buttonPressedTwoSeconds); // Set up the onPressedFor event
  101.  
  102.     if (button.supportsInterrupt()) {
  103.         button.enableInterrupt(buttonISR); // Enable interrupt if supported
  104.         Serial.println("Button will be used through interrupts");
  105.     }
  106. }
  107.  
  108. void loop(void)
  109. {
  110.     // put your main code here, to run repeatedly:
  111.     button.update(); // Update the button state
  112.  
  113.     if (digitalRead(sensoreInfrarossi_PushButton_PIN_D2) == LOW) {
  114.         if (timerState == STOPPED) {
  115.             timerState = RUNNING;
  116.             startTime = millis();
  117.         }
  118.     } else {
  119.         if (timerState == RUNNING) {
  120.             timerState = STOPPED;
  121.             elapsedTime = millis() - startTime;
  122.         }
  123.     }
  124.  
  125.     if (timerState == RUNNING) {
  126.         unsigned long currentTime = millis();
  127.         unsigned long displayTime = (currentTime - startTime) / 1000;
  128.         lcd.setCursor(0, 1);
  129.         lcd.print("Timer: ");
  130.         lcd.print(displayTime);
  131.         lcd.print("s      "); // Clear any extra characters
  132.     } else {
  133.         lcd.setCursor(0, 1);
  134.         lcd.print("Timer: ");
  135.         lcd.print(elapsedTime / 1000);
  136.         lcd.print("s      "); // Clear any extra characters
  137.     }
  138. }
  139.  
  140. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement