#define LED_PIN 9
#define BUTTON 2
bool ledState = false;
bool lastButton = HIGH;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
digitalWrite(LED_PIN, LOW); // LED OFF initially
}
void loop() {
bool currentButton = digitalRead(BUTTON);
// detect button press
if (lastButton == HIGH && currentButton == LOW) {
ledState = !ledState; // toggle LED
delay(200); // debounce
}
lastButton = currentButton;
if (ledState) {
digitalWrite(LED_PIN, HIGH); // LED ON
} else {
digitalWrite(LED_PIN, LOW); // LED OFF
}
}