CCCPaste

BlinkingLED rev_01

/********* 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: BlinkingLED
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-09-16 17:21:01
	- Source Code generated by: Alessandro

********* Pleasedontcode.com **********/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 

/****** SYSTEM REQUIREMENT 1 *****/
	/* If push button is pressed two times in 1 second, */
	/* so blink the LED for 5 seconds with a period of */
	/* 500 milliseconds. */

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void buttonPressed(void);
void blinkLED(void);

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t myLED_LED_PIN_D3 = 3;

// Button object
EasyButton myButton(myPushButton_PushButton_PIN_D2);

// LED state
bool ledState = false;

// Blink interval
const uint32_t blinkInterval = 500;

// Blink duration
const uint32_t blinkDuration = 5000;

// Button press counter
uint8_t buttonPressCount = 0;

// Last button press time
uint32_t lastButtonPressTime = 0;

void setup(void)
{
	// Set the push button pin as input with pull-up resistor enabled
	pinMode(myPushButton_PushButton_PIN_D2, INPUT_PULLUP);

	// Set the LED pin as output
	pinMode(myLED_LED_PIN_D3, OUTPUT);

	// Initialize the button object
	myButton.begin();

	// Attach the buttonPressed function to the button's pressed event
	myButton.onPressed(buttonPressed);
}

void loop(void)
{
	// Update the button object
	myButton.update();
}

void buttonPressed(void)
{
	// Get the current time
	uint32_t currentTime = millis();

	// Check if it's a new button press within the time limit
	if (currentTime - lastButtonPressTime > 1000)
	{
		// Reset the button press count
		buttonPressCount = 0;
	}

	// Increment the button press count
	buttonPressCount++;

	// Update the last button press time
	lastButtonPressTime = currentTime;

	// Check if the button press count is 2
	if (buttonPressCount == 2)
	{
		// Enable the LED blinking
		blinkLED();
	}
}

void blinkLED(void)
{
	// Get the start time
	uint32_t startTime = millis();

	// Blink the LED for the specified duration
	while (millis() - startTime < blinkDuration)
	{
		// Toggle the LED state
		ledState = !ledState;

		// Set the LED state
		digitalWrite(myLED_LED_PIN_D3, ledState);

		// Delay for the blink interval
		delay(blinkInterval);
	}

	// Turn off the LED
	digitalWrite(myLED_LED_PIN_D3, LOW);
}