// https://wokwi.com/projects/374000430676422657
// https://forum.arduino.cc/t/single-button-input-controlling-multiple-outputs/1161323
# define LED_1_PIN 8
# define LED_2_PIN 9
# define BUTTON_PIN 2
unsigned long debounceDuration = 50; // millis
unsigned long lastTimeButtonStateChanged = 0;
byte lastButtonState = HIGH;
void setup() {
Serial.begin(115200);
Serial.println("I can see!\n");
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
unsigned long timeNow = millis();
if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = timeNow;
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
Serial.println(" // button has been released ONE");
digitalWrite(LED_1_PIN, HIGH);
}
else if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = timeNow;
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
Serial.println(" // button has been released TWO");
digitalWrite(LED_1_PIN, HIGH);
digitalWrite(LED_2_PIN, HIGH);
}
else if (timeNow - lastTimeButtonStateChanged > debounceDuration) {
byte buttonState = digitalRead(BUTTON_PIN);
if (buttonState != lastButtonState) {
lastTimeButtonStateChanged = timeNow;
lastButtonState = buttonState;
if (buttonState == HIGH) { // button has been released
Serial.println(" // button has been released THREE");
digitalWrite(LED_1_PIN, LOW);
digitalWrite(LED_2_PIN, LOW);
}
}
}
}
}
}
}
}