const int bp2 = 2; // Pin connected to the button
int buttonState2 = 0; // Current button state
int lastButtonState2 = HIGH; // Previous button state
void setup() {
pinMode(bp2, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(5, OUTPUT); // Set LED pin as output
digitalWrite(5, HIGH); // Initialize the LED as off
Serial.begin(9600); // Initialize serial communication
}
void loop() {
buttonState2 = digitalRead(bp2); // Read the button state
// Check for a transition from HIGH to LOW (button press)
if ( buttonState2 == LOW) {
// Toggle the LED
digitalWrite(5, LOW); // Turn LED on
}
else{
digitalWrite(5,HIGH);
}
delay(100);
// Save the current button state as the last state for the next loop
lastButtonState2 = buttonState2;
}