CCCPaste

Button Blink rev_06

/********* 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:07:59

********* 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, θα πρέπει να ανάβει για 15 δευτερόλεπτα */
	/* και μετα να σβηνη */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include  // Include the EasyButton library

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

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t redLEDPin = 3;
const uint8_t greenLEDPin = 4;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool isButtonPressed = false;
bool isGreenLEDOn = false;
bool isRedLEDOn = false;

/****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
EasyButton button(buttonPin); // Create an instance of the EasyButton class for the button

void setup(void)
{
  // put your setup code here, to run once:

  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(redLEDPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);

  button.begin(); // Initialize the button

  button.onPressed(blinkGreenLED); // Set the callback function for button pressed event
}

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

  // Read the state of the button
  button.read();

  // Check if the button is pressed
  if (button.isPressed())
  {
    isButtonPressed = true;
  }
  else
  {
    isButtonPressed = false;
  }

  // Check if the green LED is on
  if (isGreenLEDOn)
  {
    if (isButtonPressed)
    {
      // Blink the green LED 10 times
      for (int i = 0; i < 10; i++)
      {
        digitalWrite(greenLEDPin, HIGH);
        delay(500);
        digitalWrite(greenLEDPin, LOW);
        delay(500);
      }
      isGreenLEDOn = false;
    }
  }
  else
  {
    if (!isButtonPressed)
    {
      // Turn on the red LED for 15 seconds
      turnOnRedLED();
      delay(15000);
      turnOffRedLED();
    }
  }
}

void blinkGreenLED()
{
  // Callback function for button pressed event
  // Perform any actions you want when the button is pressed
  Serial.println("Button pressed");
  
  // Turn on the green LED
  isGreenLEDOn = true;
}

void turnOnRedLED()
{
  digitalWrite(redLEDPin, HIGH);
  isRedLEDOn = true;
}

void turnOffRedLED()
{
  digitalWrite(redLEDPin, LOW);
  isRedLEDOn = false;
}