/********* 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: "LED Control"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-04-07 10:07:08

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

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onPressed(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;

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool red_LED_PIN_D3_rawData = LOW;
bool green_LED_PIN_D4_rawData = LOW;

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(tilt_PushButton_PIN_D2);

int greenLedFlashCount = 0; // Counter for green LED flashing

unsigned long redLedStartTime = 0; // Start time for red LED on duration
const unsigned long RED_LED_ON_DURATION = 15000; // Duration for red LED on (in milliseconds)

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

  button.begin();
  button.onPressed(onPressed);
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  
  button.read();
  updateOutputs(); // Refresh output data
  
  // Check if green LED flashing is required
  if (greenLedFlashCount > 0)
  {
    digitalWrite(green_LED_PIN_D4, HIGH);
    delay(100);
    digitalWrite(green_LED_PIN_D4, LOW);
    delay(100);
    greenLedFlashCount--;
  }
  
  // Check if red LED on duration has elapsed
  if (redLedStartTime > 0 && millis() - redLedStartTime >= RED_LED_ON_DURATION)
  {
    red_LED_PIN_D3_rawData = LOW;
    redLedStartTime = 0;
  }
}

void onPressed(void)
{
  Serial.println("Button pressed");
  
  // Flash the green LED 10 times
  greenLedFlashCount = 10;
  
  // Turn on the red LED for 15 seconds
  red_LED_PIN_D3_rawData = HIGH;
  redLedStartTime = millis();
}

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