const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin = 5; // the number of the LED pin
// variables for storing the pushbutton status and LED state
int buttonState = 0;
int lastButtonState = 0;
int ledState = LOW;
void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
// check for a button press (transition from HIGH to LOW)
if (buttonState == HIGH && lastButtonState == LOW) {
// toggle the LED state
ledState = (ledState == LOW) ? HIGH : LOW;
// update the LED
digitalWrite(ledPin, ledState);
// print the LED state to the Serial monitor
Serial.println(ledState);
}
// save the current button state for the next iteration
lastButtonState = buttonState;
}