// Pin definitions
const int buttonPin = 2; // The pin the button is connected to
const int ledPin = 13; // The pin the LED is connected to
// Variables
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button is pressed (active LOW)
if (buttonState == LOW && lastButtonState == HIGH) {
// Toggle the LED state
digitalWrite(ledPin, !digitalRead(ledPin));
}
// Save the current state of the button for the next loop iteration
lastButtonState = buttonState;
}