// Button-controlled LED for Arduino Uno
const int buttonPin = 2; // Connect a push button to pin 2
const int ledPin = 13;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState=digitalRead(buttonPin);
if (buttonState != lastButtonState){
if (buttonState == HIGH){
// Toggle the LED state when the button is pressed
digitalWrite(ledPin, !digitalRead(ledPin));
}
delay(20); // Debouncing delay
}
lastButtonState = buttonState;
}