// Define pins for LEDs
const int led1Pin = 8;
const int led2Pin = 9;
// Define pins for buttons
const int btn1Pin = 2;
const int btn2Pin = 3;
const int btn3Pin = 4;
// Variables to store LED states
bool led1State = false;
bool led2State = false;
// Variables for debouncing
unsigned long lastDebounceTime1 = 0;
unsigned long lastDebounceTime2 = 0;
unsigned long lastDebounceTime3 = 0;
unsigned long debounceDelay = 50; // 50 ms debounce time
// Variables to handle the 2-second stable state for each LED and reset
unsigned long lastLed1ToggleTime = 0;
unsigned long lastLed2ToggleTime = 0;
unsigned long lastResetTime = 0;
unsigned long stableStateDelay = 2000; // 2 seconds
// Previous button states
int lastBtn1State = HIGH;
int lastBtn2State = HIGH;
int lastBtn3State = HIGH;
void setup() {
// Initialize LED pins as outputs
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// Initialize button pins as inputs with pull-up resistors
pinMode(btn1Pin, INPUT_PULLUP);
pinMode(btn2Pin, INPUT_PULLUP);
pinMode(btn3Pin, INPUT_PULLUP);
// Initially, both LEDs are off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
}
void loop() {
// Read the current state of the buttons
int btn1State = digitalRead(btn1Pin);
int btn2State = digitalRead(btn2Pin);
int btn3State = digitalRead(btn3Pin);
// Debouncing and action for Button 1
if (btn1State != lastBtn1State) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (btn1State == LOW && lastBtn1State == HIGH) { // Button pressed (LOW due to INPUT_PULLUP)
// Only toggle if enough time has passed since the last toggle of LED 1
// and enough time has passed since the last reset
if (millis() - lastLed1ToggleTime > stableStateDelay && millis() - lastResetTime > stableStateDelay) {
led1State = !led1State;
digitalWrite(led1Pin, led1State);
lastLed1ToggleTime = millis();
}
}
}
// Debouncing and action for Button 2
if (btn2State != lastBtn2State) {
lastDebounceTime2 = millis();
}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (btn2State == LOW && lastBtn2State == HIGH) { // Button pressed
// Only toggle if enough time has passed since the last toggle of LED 2
// and enough time has passed since the last reset
if (millis() - lastLed2ToggleTime > stableStateDelay && millis() - lastResetTime > stableStateDelay) {
led2State = !led2State;
digitalWrite(led2Pin, led2State);
lastLed2ToggleTime = millis();
}
}
}
// Debouncing and action for Button 3
if (btn3State != lastBtn3State) {
lastDebounceTime3 = millis();
}
if ((millis() - lastDebounceTime3) > debounceDelay) {
if (btn3State == LOW && lastBtn3State == HIGH) { // Button pressed
// Reset both LEDs to off (regardless of their stable state)
led1State = false;
led2State = false;
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
lastResetTime = millis();
}
}
// Update the last button states
lastBtn1State = btn1State;
lastBtn2State = btn2State;
lastBtn3State = btn3State;
}
mega:SCL
mega:SDA
mega:AREF
mega:GND.1
mega:13
mega:12
mega:11
mega:10
mega:9
mega:8
mega:7
mega:6
mega:5
mega:4
mega:3
mega:2
mega:1
mega:0
mega:14
mega:15
mega:16
mega:17
mega:18
mega:19
mega:20
mega:21
mega:5V.1
mega:5V.2
mega:22
mega:23
mega:24
mega:25
mega:26
mega:27
mega:28
mega:29
mega:30
mega:31
mega:32
mega:33
mega:34
mega:35
mega:36
mega:37
mega:38
mega:39
mega:40
mega:41
mega:42
mega:43
mega:44
mega:45
mega:46
mega:47
mega:48
mega:49
mega:50
mega:51
mega:52
mega:53
mega:GND.4
mega:GND.5
mega:IOREF
mega:RESET
mega:3.3V
mega:5V
mega:GND.2
mega:GND.3
mega:VIN
mega:A0
mega:A1
mega:A2
mega:A3
mega:A4
mega:A5
mega:A6
mega:A7
mega:A8
mega:A9
mega:A10
mega:A11
mega:A12
mega:A13
mega:A14
mega:A15
led1:A
led1:C
led2:A
led2:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r