/********* 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: "Digital Control"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-04-06 08:57:25

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* when the easy button was pressed, the green led */
	/* and bazeer flashed 10 times */
/****** SYSTEM REQUIREMENT 2 *****/
	/* when the green led stops turn on the red led for */
	/* 15 seconds */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <EasyButton.h>
#include <ezBuzzer.h>

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

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

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t red_LED_PIN_D3 = 3;
const uint8_t green_LED_PIN_D4 = 4;
const uint8_t bazer_PassiveBuzzer_Signal_PIN_D5 = 5;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool red_LED_PIN_D3_rawData = 0;
bool green_LED_PIN_D4_rawData = 0;
bool bazer_PassiveBuzzer_Signal_PIN_D5_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float red_LED_PIN_D3_phyData = 0.0;
float green_LED_PIN_D4_phyData = 0.0;
float bazer_PassiveBuzzer_Signal_PIN_D5_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton tilt_PushButton(tilt_PushButton_PIN_D2);
ezBuzzer bazer_PassiveBuzzer(bazer_PassiveBuzzer_Signal_PIN_D5);

int flashCount = 0;
bool isRedLedOn = false;
unsigned long redLedStartTime = 0;

void setup(void)
{
  pinMode(tilt_PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(red_LED_PIN_D3, OUTPUT);
  pinMode(green_LED_PIN_D4, OUTPUT);
  pinMode(bazer_PassiveBuzzer_Signal_PIN_D5, OUTPUT);

  tilt_PushButton.begin();
  // bazer_PassiveBuzzer.begin(); // Commented out as it seems to be causing the error
}

void loop(void)
{
  updateOutputs();

  // Check if the button is pressed
  if (tilt_PushButton.read())
  {
    // Flash the green LED and buzzer 10 times
    if (flashCount < 10)
    {
      green_LED_PIN_D4_rawData = !green_LED_PIN_D4_rawData;
      bazer_PassiveBuzzer.beep(100);
      delay(100);
      flashCount++;
    }
    else
    {
      // Turn off the green LED and buzzer
      green_LED_PIN_D4_rawData = LOW;
      bazer_PassiveBuzzer.stop();

      // Turn on the red LED and start the timer
      if (!isRedLedOn)
      {
        red_LED_PIN_D3_rawData = HIGH;
        redLedStartTime = millis();
        isRedLedOn = true;
      }
    }
  }

  // Check if the red LED has been on for 15 seconds
  if (isRedLedOn && (millis() - redLedStartTime >= 15000))
  {
    // Turn off the red LED
    red_LED_PIN_D3_rawData = LOW;
    isRedLedOn = false;
  }
}

void updateOutputs(void)
{
  digitalWrite(red_LED_PIN_D3, red_LED_PIN_D3_rawData);
  digitalWrite(green_LED_PIN_D4, green_LED_PIN_D4_rawData);
  digitalWrite(bazer_PassiveBuzzer_Signal_PIN_D5, bazer_PassiveBuzzer_Signal_PIN_D5_rawData);
}
