Advertisement
pleasedontcode

"Infrared Timer" rev_04

Feb 20th, 2024
76
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: "Infrared Timer"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-02-20 11:41:57
  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. /****** SYSTEM REQUIREMENTS *****/
  38. /****** SYSTEM REQUIREMENT 1 *****/
  39.   /* The project must control a timer that starts when */
  40.   /* the infrared sensor is in a low active state and */
  41.   /* stops when the sensor returns to a low active */
  42.   /* state. The timer must be displayed on the LCD */
  43.   /* screen. */
  44. /****** SYSTEM REQUIREMENT 2 *****/
  45.   /* The timer state should be handled using a state */
  46.   /* machine. Furthermore, an explicit function called */
  47.   /* 'resetTimer' needs to be implemented to reset the */
  48.   /* counter and state of the state machine. */
  49. /****** END SYSTEM REQUIREMENTS *****/
  50.  
  51. /****** FUNCTION PROTOTYPES *****/
  52. void setup(void);
  53. void loop(void);
  54. void resetTimer(void);
  55.  
  56. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  57. const uint8_t infraredSensor_PIN_D2 = 2;
  58.  
  59. /***** DEFINITION OF I2C PINS *****/
  60. const uint8_t lcd_I2C_PIN_SDA_A4 = A4;
  61. const uint8_t lcd_I2C_PIN_SCL_A5 = A5;
  62. const uint8_t lcd_I2C_SLAVE_ADDRESS = 39;
  63.  
  64. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  65. LiquidCrystal_I2C lcd(lcd_I2C_SLAVE_ADDRESS, 16, 2); // Initialize the LiquidCrystal_I2C object
  66. EasyButton button(infraredSensor_PIN_D2); // Initialize the EasyButton object
  67.  
  68. // Timer variables
  69. unsigned long startTime = 0;
  70. unsigned long elapsedTime = 0;
  71. bool timerRunning = false;
  72.  
  73. enum TimerState
  74. {
  75.   IDLE,
  76.   RUNNING
  77. };
  78.  
  79. TimerState timerState = IDLE;
  80.  
  81. void resetTimer()
  82. {
  83.   startTime = 0;
  84.   elapsedTime = 0;
  85.   timerRunning = false;
  86.   timerState = IDLE;
  87. }
  88.  
  89. void setup(void)
  90. {
  91.   // put your setup code here, to run once:
  92.  
  93.   pinMode(infraredSensor_PIN_D2, INPUT_PULLUP);
  94.  
  95.   lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
  96.   lcd.backlight(); // Turn on the backlight
  97.  
  98.   lcd.print("Hello, Arduino!"); // Print a message on the LCD
  99.  
  100.   // Initialize the EasyButton object
  101.   button.begin();
  102.  
  103.   // Attach button press event handler
  104.   button.onPressed(buttonPressed);
  105. }
  106.  
  107. void loop(void)
  108. {
  109.   // put your main code here, to run repeatedly:
  110.  
  111.   // Check if the button is pressed
  112.   button.read();
  113.  
  114.   // Check if the infrared sensor is in a low active state
  115.   if (digitalRead(infraredSensor_PIN_D2) == LOW)
  116.   {
  117.     // Check if the timer is not already running
  118.     if (!timerRunning)
  119.     {
  120.       // Start the timer
  121.       startTime = millis();
  122.       timerRunning = true;
  123.       timerState = RUNNING;
  124.     }
  125.   }
  126.   else
  127.   {
  128.     // Check if the timer is running
  129.     if (timerRunning)
  130.     {
  131.       // Stop the timer and calculate the elapsed time
  132.       elapsedTime = millis() - startTime;
  133.       timerRunning = false;
  134.       timerState = IDLE;
  135.     }
  136.   }
  137.  
  138.   // Display the timer on the LCD
  139.   lcd.clear();
  140.   lcd.setCursor(0, 0);
  141.   lcd.print("Timer: ");
  142.   lcd.print(elapsedTime);
  143.  
  144.   delay(100); // Delay for stability
  145. }
  146.  
  147. void buttonPressed()
  148. {
  149.   // Reset the timer when the button is pressed
  150.   resetTimer();
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement