const int buttonPin = 4; // the number of the pushbutton pin
const int ledPin1 = 5; // the number of the LED pin
const int ledPin2 = 2; // the number of the LED pin
// variable for storing the pushbutton status and LED state
int buttonState = 0;
bool ledOn = false; // Updated variable name
void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// If button is pressed, toggle the LED
if (buttonState == HIGH) {
ledOn = !ledOn;
delay(200); // Add a small delay to debounce the button
}
if (ledOn) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
} else {
// turn LED off
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}