CCCPaste

LCD Button rev_05

/********* 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: LCD Button
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-05-03 14:37:09

********* 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

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t sensorInfrared_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 = 39;

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

// Timer variables
unsigned long startTime = 0;
unsigned long elapsedTime = 0;
bool timerRunning = false;

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(sensorInfrared_PushButton_PIN_D2, INPUT_PULLUP);

	lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
	lcd.backlight(); // Turn on the backlight

	lcd.setCursor(0, 0);
	lcd.print("Hello, world!");

	// Add the EasyButton setup code here
	button.begin();
	button.onPressedFor(2000, onPressedForDuration);
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	button.read();

	// Check the infrared sensor state
	if (digitalRead(sensorInfrared_PushButton_PIN_D2) == LOW) {
		if (!timerRunning) {
			startTime = millis();
			timerRunning = true;
		}
		elapsedTime = millis() - startTime;
		lcd.setCursor(0, 1);
		lcd.print("Timer: ");
		lcd.print(elapsedTime / 1000); // Display elapsed time in seconds
	}
	else {
		resetTimer();
	}
}

// Callback function to be called when the button is pressed.
void onPressedForDuration()
{
	// Code to be executed when the button is pressed for the given duration.
}

// Function to reset the timer and LCD display
void resetTimer()
{
	startTime = 0;
	elapsedTime = 0;
	timerRunning = false;
	lcd.setCursor(0, 1);
	lcd.print("Timer: 0     "); // Clear the timer display
}

/* END CODE */