/********* 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: "Timer Trigger"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-15 11:02:26

********* 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 <Wire.h>
#include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
#include <EasyButton.h>         // https://github.com/evert-arias/EasyButton

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void buttonPressedTwoSeconds();
void buttonISR();
void resetTimer();

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t sensoreInfrarossi_PushButton_PIN_D2 = 2;

/***** DEFINITION OF I2C PINS *****/
const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27; // Typically 0x27 for most I2C LCDs

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize with address, columns, and rows
EasyButton button(sensoreInfrarossi_PushButton_PIN_D2); // Initialize EasyButton with the button pin

/****** TIMER VARIABLES *****/
enum TimerState { STOPPED, RUNNING };
TimerState timerState = STOPPED;
unsigned long startTime = 0;
unsigned long elapsedTime = 0;

void buttonPressedTwoSeconds() {
  Serial.println("Button pressed for two seconds");
}

void buttonISR() {
  button.read();
}

void resetTimer() {
  timerState = STOPPED;
  elapsedTime = 0;
  lcd.setCursor(0, 1);
  lcd.print("Timer: 0s      ");
}

void setup(void)
{
    // put your setup code here, to run once:
    Serial.begin(115200); // Initialize serial communication
    Serial.println();
    Serial.println(">>> EasyButton onPressedFor interrupt example <<<");

    pinMode(sensoreInfrarossi_PushButton_PIN_D2, INPUT_PULLUP);

    lcd.init();       // Initialize the LCD
    lcd.backlight();  // Turn on the backlight

    // Display initial messages
    lcd.setCursor(3, 0);
    lcd.print("Hello, world!");
    lcd.setCursor(2, 1);
    lcd.print("Ywrobot Arduino!");
    lcd.setCursor(0, 2);
    lcd.print("Arduino LCM IIC 1602");
    lcd.setCursor(2, 3);
    lcd.print("Power By Ec-yuan!");

    button.begin(); // Initialize the button
    button.onPressedFor(2000, buttonPressedTwoSeconds); // Set up the onPressedFor event

    if (button.supportsInterrupt()) {
        button.enableInterrupt(buttonISR); // Enable interrupt if supported
        Serial.println("Button will be used through interrupts");
    }
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    button.update(); // Update the button state

    if (digitalRead(sensoreInfrarossi_PushButton_PIN_D2) == LOW) {
        if (timerState == STOPPED) {
            timerState = RUNNING;
            startTime = millis();
        }
    } else {
        if (timerState == RUNNING) {
            timerState = STOPPED;
            elapsedTime = millis() - startTime;
        }
    }

    if (timerState == RUNNING) {
        unsigned long currentTime = millis();
        unsigned long displayTime = (currentTime - startTime) / 1000;
        lcd.setCursor(0, 1);
        lcd.print("Timer: ");
        lcd.print(displayTime);
        lcd.print("s      "); // Clear any extra characters
    } else {
        lcd.setCursor(0, 1);
        lcd.print("Timer: ");
        lcd.print(elapsedTime / 1000);
        lcd.print("s      "); // Clear any extra characters
    }
}

/* END CODE */