#include <elapsedMillis.h>
int buttonGreen = 11;
int buttonRed = 12;
int ledGreen = 3;
int ledRed = 2;
int on = LOW;
int off = HIGH;
int lastGreen = HIGH;
int lastRed = HIGH;
int timerGreen;
int timerRed;
int timeOut = 0;
int timeOutMax = 5000; // in milliseconds
void setup() {
digitalWrite(ledGreen, HIGH);
digitalWrite(ledRed, HIGH);
pinMode(buttonGreen, INPUT_PULLUP);
pinMode(buttonRed, INPUT_PULLUP);
pinMode(ledGreen, OUTPUT);
pinMode(ledRed, OUTPUT);
timerGreen = 0;
timerRed = 0;
}
void loop() {
elapsedMillis timeOut;
while (timeOut < timeOutMax){
// GREEN side control
// turn on the green output when the green input is low
while (digitalRead(buttonRed) == LOW){
digitalWrite(ledRed, on);
timerRed++;
timeOut = 0;
delay(1);
}
// turn on the green output when the green input is low
while (digitalRead(buttonGreen) == LOW){
digitalWrite(ledGreen, on);
timerGreen++;
timeOut = 0;
delay(1);
}
// turn off the green output when the input goes high and the input was active less than 1 second
if (digitalRead(buttonRed) == HIGH && timerRed < 200){
digitalWrite(ledRed, off);
timerRed=0;
}
// turn off the green output when either if the inputs goeas low
// and wait 0.5 sec to prevent jamming
while (digitalRead(ledRed)==on && digitalRead(buttonRed) == LOW || digitalRead(buttonGreen) == LOW){
digitalWrite(ledRed,off);
timerRed=0;
// delay(500);
}
}
digitalWrite(ledGreen, off);
digitalWrite(ledRed, off);
}