#define LED_PIN 3
#define BUTTON_PIN 2
int buttonState = 0; // Variable to store button state
int lastButtonState = 0; // Previous state of the button
bool ledState = false; // LED state: false = OFF, true = ON
void setup() {
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
}
void loop() {
buttonState = digitalRead(BUTTON_PIN); // Read the state of the button
// Check if button is pressed
if (buttonState == LOW && lastButtonState == HIGH) {
// Toggle LED state
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW); // Turn LED ON/OFF
delay(200); // Debounce delay
}
lastButtonState = buttonState; // Save the current button state for next loop
}