/********* 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: "Tilt Control"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-04-06 11:17:28

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* when the easy button was pressed, the green led */
	/* 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>    //https://github.com/evert-arias/EasyButton
#include <ezBuzzer.h>    //https://github.com/ArduinoGetStarted/buzzer

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void flashGreenLED(void);
void turnOnRedLED(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 *****/
/***** used to store raw data *****/
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 *****/
/***** used to store data after characteristic curve transformation *****/
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 tiltPushButton(tilt_PushButton_PIN_D2, true); // Initialize EasyButton object for tilt push button
ezBuzzer buzzer(bazer_PassiveBuzzer_Signal_PIN_D5); // Initialize ezBuzzer object for passive buzzer

bool greenLedFlashCompleted = false;

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

    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);

    tiltPushButton.begin(); // Start monitoring the tilt push button
}

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

    updateOutputs(); // Refresh output data

    tiltPushButton.read(); // Read the tilt push button state

    // Check if the tilt push button is pressed
    if (tiltPushButton.isPressed())
    {
        flashGreenLED();
    }

    // Check if the green LED has completed flashing
    if (greenLedFlashCompleted)
    {
        turnOnRedLED();
    }
}

void flashGreenLED()
{
    // Flash the green LED 10 times
    for (int i = 0; i < 10; i++)
    {
        green_LED_PIN_D4_rawData = HIGH;
        delay(500);
        green_LED_PIN_D4_rawData = LOW;
        delay(500);
    }

    greenLedFlashCompleted = true;
}

void turnOnRedLED()
{
    // Turn on the red LED for 15 seconds
    red_LED_PIN_D3_rawData = HIGH;
    delay(15000);
    red_LED_PIN_D3_rawData = LOW;

    // Reset the green LED flag
    greenLedFlashCompleted = false;
}

void updateOutputs()
{
    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);
}
