/********* 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 Toggle
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2025-10-06 22:37:28

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* give me example led */
/****** END SYSTEM REQUIREMENTS *****/


/* START CODE */

/* BUTTON EXAMPLE: Toggle LED with push button on Arduino Uno
 * Button on pin 2 (INPUT_PULLUP). LED on pin 13 (built-in).
 * Debounced toggle on each press.
 */

// Pin definitions
const uint8_t BUTTON_PIN = 2; // Digital pin connected to the button (active LOW with INPUT_PULLUP)
const uint8_t LED_PIN = 13;   // Built-in LED on the Uno

// Debounce logic
bool lastButtonState = HIGH;       // Previous reading from the button
bool ledState = false;             // Current LED state
unsigned long lastDebounceTime = 0; // Timestamp of the last state change
const unsigned long debounceDelay = 50; // Debounce delay in milliseconds

void setup(void)
{
    // Initialize button with internal pull-up and LED as output
    pinMode(BUTTON_PIN, INPUT_PULLUP);
    pinMode(LED_PIN, OUTPUT);
    digitalWrite(LED_PIN, LOW);

    // Read initial button state to set baseline
    lastButtonState = digitalRead(BUTTON_PIN);
}

void loop(void)
{
    int currentReading = digitalRead(BUTTON_PIN);

    // If button state changed, reset the debouncing timer
    if (currentReading != lastButtonState)
    {
        lastDebounceTime = millis();
    }

    // If enough time has passed, consider the button state stabilized
    if ((millis() - lastDebounceTime) > debounceDelay)
    {
        // If the reading changed since last stable state, act on it
        if (currentReading != lastButtonState)
        {
            lastButtonState = currentReading;

            // Button is pressed when the reading is LOW (due to pull-up)
            if (currentReading == LOW)
            {
                ledState = !ledState; // Toggle LED state
                digitalWrite(LED_PIN, ledState ? HIGH : LOW);
            }
        }
    }
}

/* END CODE */
