CCCPaste

Button Blink rev_10

/********* 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:46:40

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


/********* User code review feedback **********
#### Feedback 1 ####
- ok. i want the green led to blink 100ms
********* User code review feedback **********/

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

/****** SYSTEM REQUIREMENTS *****/
const uint8_t BUTTON_PIN = 2;
const uint8_t GREEN_LED_PIN = 4;
const uint8_t RED_LED_PIN = 3;
const int BLINK_COUNT = 10;
const int BLINK_DURATION = 100;

// Instance of the button.
EasyButton button(BUTTON_PIN);

// Variables to track the state of the system requirements.
bool isGreenLedBlinking = false;
bool isRedLedOn = false;
int blinkCount = 0;

void setup()
{
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(GREEN_LED_PIN, OUTPUT);
  pinMode(RED_LED_PIN, OUTPUT);

  // Initialize the button.
  button.begin();
  button.onPressed([]() {
    if (!isGreenLedBlinking)
    {
      blinkCount = 0;
      isGreenLedBlinking = true;
      digitalWrite(GREEN_LED_PIN, HIGH);
    }
    else
    {
      blinkCount++;
      if (blinkCount >= BLINK_COUNT)
      {
        isGreenLedBlinking = false;
        digitalWrite(GREEN_LED_PIN, LOW);
      }
      else
      {
        digitalWrite(GREEN_LED_PIN, !digitalRead(GREEN_LED_PIN));
      }
    }
  });
}

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

  if (isGreenLedBlinking)
  {
    delay(BLINK_DURATION);
  }
  else if (isRedLedOn)
  {
    delay(10000);
    isRedLedOn = false;
    digitalWrite(RED_LED_PIN, LOW);
  }
}