/********* 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: Button Blink
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-04-07 11:22:41

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* When the EasyButton connected to pin D2 is */
	/* pressed, the green LED connected to pin D4 shall */
	/* blink precisely 10 times and then stop blinking, */
	/* waiting for the EasyButton to be pressed again. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* Αφού η πράσινη λυχνία LED στον ακροδέκτη D4 */
	/* τελειώσει να αναβοσβήνει, η κόκκινη λυχνία LED */
	/* στον ακροδέκτη D3 θα πρέπει να παραμείνει αναμμένη */
	/* για διάστημα 10 δευτερολέπτων και στη συνέχεια να */
	/* σβήσει. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <EasyButton.h> // https://github.com/evert-arias/EasyButton

/****** FUNCTION PROTOTYPES *****/
void setup();
void loop();
void blinkGreenLED();
void turnOffRedLED();

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t RED_LED_PIN = 3;
const uint8_t GREEN_LED_PIN = 4;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(BUTTON_PIN); // Instance of the EasyButton class

// Flag to track if the button is pressed
bool buttonPressed = false;

void setup()
{
  // Initialize the digital input and output pins
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(RED_LED_PIN, OUTPUT);
  pinMode(GREEN_LED_PIN, OUTPUT);

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

  // Attach a callback function to be called when the button is pressed
  button.onPressed(blinkGreenLED);
}

void loop()
{
  // Continuously read the status of the button
  button.read();

  // Check if the button is pressed
  if (button.isPressed())
  {
    // Check if the button is not already pressed
    if (!buttonPressed)
    {
      // Set the flag to indicate that the button is pressed
      buttonPressed = true;

      // Blink the green LED precisely 10 times
      for (int i = 0; i < 10; i++)
      {
        digitalWrite(GREEN_LED_PIN, HIGH);
        delay(250);
        digitalWrite(GREEN_LED_PIN, LOW);
        delay(250);
      }

      // Stop blinking the green LED
      digitalWrite(GREEN_LED_PIN, LOW);

      // Turn on the red LED for 10 seconds
      digitalWrite(RED_LED_PIN, HIGH);
      delay(10000);
      digitalWrite(RED_LED_PIN, LOW);

      // Reset the flag
      buttonPressed = false;
    }
  }
}

void blinkGreenLED()
{
  // Blink the green LED once when the button is pressed
  digitalWrite(GREEN_LED_PIN, HIGH);
  delay(250);
  digitalWrite(GREEN_LED_PIN, LOW);
}

void turnOffRedLED()
{
  // Turn off the red LED
  digitalWrite(RED_LED_PIN, LOW);
}
