const int buttonPin = 2; // Pin number for the button
const int ledPin = 13; // Pin number for the LED
int buttonState = 0; // Variable to store the button state
int lastButtonState = 0; // Variable to store the previous button state
int ledState = LOW; // Variable to store the LED state
void setup() {
pinMode(buttonPin, INPUT); // Set the button pin as an input
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the state of the button
if (buttonState != lastButtonState) { // Check if the button state has changed
if (buttonState == HIGH) { // Check if the button is pressed
ledState = !ledState; // Toggle the LED state
}
}
digitalWrite(ledPin, ledState); // Update the LED state
lastButtonState = buttonState; // Save the current button state for comparison
}