/********* 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: "Infrared Timer" - Source Code compiled for: Arduino Uno - Source Code created on: 2024-02-20 11:41:57 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* The project must control a timer that starts when */ /* the infrared sensor is in a low active state and */ /* stops when the sensor returns to a low active */ /* state. The timer must be displayed on the LCD */ /* screen. */ /****** SYSTEM REQUIREMENT 2 *****/ /* The timer state should be handled using a state */ /* machine. Furthermore, an explicit function called */ /* 'resetTimer' needs to be implemented to reset the */ /* counter and state of the state machine. */ /****** END SYSTEM REQUIREMENTS *****/ /****** DEFINITION OF LIBRARIES *****/ #include #include //https://github.com/marcoschwartz/LiquidCrystal_I2C #include //https://github.com/evert-arias/EasyButton /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* The project must control a timer that starts when */ /* the infrared sensor is in a low active state and */ /* stops when the sensor returns to a low active */ /* state. The timer must be displayed on the LCD */ /* screen. */ /****** SYSTEM REQUIREMENT 2 *****/ /* The timer state should be handled using a state */ /* machine. Furthermore, an explicit function called */ /* 'resetTimer' needs to be implemented to reset the */ /* counter and state of the state machine. */ /****** END SYSTEM REQUIREMENTS *****/ /****** FUNCTION PROTOTYPES *****/ void setup(void); void loop(void); void resetTimer(void); /***** DEFINITION OF DIGITAL INPUT PINS *****/ const uint8_t infraredSensor_PIN_D2 = 2; /***** DEFINITION OF I2C PINS *****/ const uint8_t lcd_I2C_PIN_SDA_A4 = A4; const uint8_t lcd_I2C_PIN_SCL_A5 = A5; const uint8_t lcd_I2C_SLAVE_ADDRESS = 39; /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/ LiquidCrystal_I2C lcd(lcd_I2C_SLAVE_ADDRESS, 16, 2); // Initialize the LiquidCrystal_I2C object EasyButton button(infraredSensor_PIN_D2); // Initialize the EasyButton object // Timer variables unsigned long startTime = 0; unsigned long elapsedTime = 0; bool timerRunning = false; enum TimerState { IDLE, RUNNING }; TimerState timerState = IDLE; void resetTimer() { startTime = 0; elapsedTime = 0; timerRunning = false; timerState = IDLE; } void setup(void) { // put your setup code here, to run once: pinMode(infraredSensor_PIN_D2, INPUT_PULLUP); lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows lcd.backlight(); // Turn on the backlight lcd.print("Hello, Arduino!"); // Print a message on the LCD // Initialize the EasyButton object button.begin(); // Attach button press event handler button.onPressed(buttonPressed); } void loop(void) { // put your main code here, to run repeatedly: // Check if the button is pressed button.read(); // Check if the infrared sensor is in a low active state if (digitalRead(infraredSensor_PIN_D2) == LOW) { // Check if the timer is not already running if (!timerRunning) { // Start the timer startTime = millis(); timerRunning = true; timerState = RUNNING; } } else { // Check if the timer is running if (timerRunning) { // Stop the timer and calculate the elapsed time elapsedTime = millis() - startTime; timerRunning = false; timerState = IDLE; } } // Display the timer on the LCD lcd.clear(); lcd.setCursor(0, 0); lcd.print("Timer: "); lcd.print(elapsedTime); delay(100); // Delay for stability } void buttonPressed() { // Reset the timer when the button is pressed resetTimer(); }