#include <Arduino.h> // not needed in Arduino IDE but included for clarity
// Pin definitions
const int led1Pin = 8;
const int led2Pin = 9;
const int btn1Pin = 22; // Toggle led1
const int btn2Pin = 23; // Toggle led2
const int btn3Pin = 24; // Reset both to OFF
// Variables to store the LED states (false = OFF, true = ON)
bool led1State = false;
bool led2State = false;
// Debounce delay (ms)
const unsigned long debounceDelay = 150;
// Stable delay after an action (ms)
const unsigned long stableDelay = 2000;
void setup() {
// Initialize LED pins as outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// Ensure LEDs start off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
// Initialize button pins as input with internal pullup enabled.
pinMode(btn1Pin, INPUT_PULLUP);
pinMode(btn2Pin, INPUT_PULLUP);
pinMode(btn3Pin, INPUT_PULLUP);
}
void loop() {
// Check button 1 (toggle led1)
if (digitalRead(btn1Pin) == LOW) { // button pressed?
delay(debounceDelay); // debounce
if (digitalRead(btn1Pin) == LOW) { // still low after debounce?
// Toggle LED1 state
led1State = !led1State;
digitalWrite(led1Pin, led1State ? HIGH : LOW);
// Wait for the LED state to remain stable for 2 seconds
delay(stableDelay);
// Wait until the user releases the button to avoid repeat toggling
while(digitalRead(btn1Pin) == LOW) {
// do nothing, waiting for release
}
delay(debounceDelay); // additional debounce after release
}
}
// Check button 2 (toggle led2)
if (digitalRead(btn2Pin) == LOW) { // button pressed?
delay(debounceDelay); // debounce
if (digitalRead(btn2Pin) == LOW) { // check confirmed low
// Toggle LED2 state
led2State = !led2State;
digitalWrite(led2Pin, led2State ? HIGH : LOW);
// Wait for the LED state to remain stable for 2 seconds
delay(stableDelay);
// Wait until the button is released
while(digitalRead(btn2Pin) == LOW) {
// wait
}
delay(debounceDelay); // post-release debounce
}
}
// Check button 3 (reset both LEDs to off)
if (digitalRead(btn3Pin) == LOW) { // pressed?
delay(debounceDelay); // debounce
if (digitalRead(btn3Pin) == LOW) {
// Reset both LEDs to off
led1State = false;
led2State = false;
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
// Maintain off state for 2 seconds
delay(stableDelay);
// Wait until button released
while(digitalRead(btn3Pin) == LOW) {
// wait for release
}
delay(debounceDelay);
}
}
}