const int BUTTON = 2; // Button pin
const int LED = 3; // LED pin
int ledState = LOW; // Start with LED OFF
int buttonState;
int lastButtonState = HIGH; // Because of pull-up
void setup() {
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP); // Use internal pull-up resistor
}
void loop() {
buttonState = digitalRead(BUTTON);
// Detect button press (LOW when pressed)
if (buttonState == LOW && lastButtonState == HIGH) {
delay(50); // debounce
ledState = !ledState; // Toggle LED state
digitalWrite(LED, ledState);
}
lastButtonState = buttonState;
}