// https://forum.arduino.cc/t/programming-help-switch-off-relays/1239207
// https://wokwi.com/projects/393250806386976769
# include <ezButton.h>
# define WINDOW 1000 // for "simultaneous"
# define DURATION 2000 // active period for relays
const byte ledPins[] = {12, 11, 10}; // A, B, BOTH
const byte buttonPins[] = {A1, A2};
ezButton buttonA(buttonPins[0]); // create ezButton object that attach to pin 7;
ezButton buttonB(buttonPins[1]); // create ezButton object that attach to pin 7;
void setup()
{
Serial.begin(115200);
Serial.println("\nJello Whirled!\n");
buttonA.setDebounceTime(20);
buttonB.setDebounceTime(20);
}
unsigned long now;
unsigned long durationTimer;
unsigned long bothTimer;
bool needA;
bool needB;
bool awaitin;
void loop()
{
now = millis();
buttonA.loop();
buttonB.loop();
bool pressA = buttonA.isPressed();
bool pressB = buttonB.isPressed();
if (pressA) {
Serial.println("A request");
needA = true;
if (!awaitin) {
bothTimer = now;
awaitin = true;
}
}
if (pressB) {
Serial.println("B request");
needB = true;
if (!awaitin) {
bothTimer = now;
awaitin = true;
}
}
if ((awaitin && now - bothTimer > WINDOW) || (needA && needB)) {
if (needA && needB) {
digitalWrite(ledPins[2], HIGH);
}
else if (needA) {
digitalWrite(ledPins[0], HIGH);
}
else digitalWrite(ledPins[1], HIGH);
durationTimer = now;
needA = false;
needB = false;
awaitin = false;
}
if (now - durationTimer > DURATION) {
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
}
}
/*
// check multiple buttons and toggle LEDs
enum { Off = LOW, On = HIGH };
byte PinsLed [] = { 10, 11, 12 };
#define N_LED sizeof(PinsLed)
byte PinsBut [] = { A1, A2 };
#define N_BUT sizeof(PinsBut)
byte butState [N_BUT];
const unsigned long MsecPeriod = 10000;
unsigned long msecLed [N_LED];
bool active [N_LED];
char s [90];
// -----------------------------------------------------------------------------
void
loop ()
{
unsigned long msec = millis ();
for (unsigned n = 0; n < N_LED; n++) {
if (active [n] && msec - msecLed [n] >= MsecPeriod) {
active [n] = false;
digitalWrite (PinsLed [n], Off);
sprintf (s, " %d expired", n);
Serial.println (s);
}
}
for (unsigned n = 0; n < N_BUT; n++) {
byte but = digitalRead (PinsBut [n]);
if (butState [n] != but) {
butState [n] = but;
delay (5000); // debounce
if (LOW == but) {
msecLed [n] = msec; // only used if acitve
active [n] = true;
sprintf (s, " %d active", n);
Serial.println (s);
}
}
active [2] = (active [0] && active [1]);
if (active [2]) {
active [0] = active [1] = false;
digitalWrite (PinsLed [2], On);
sprintf (s, " %d on", 2);
Serial.println (s);
}
else if (active [1]) {
digitalWrite (PinsLed [1], On);
sprintf (s, " %d on", 1);
Serial.println (s);
}
else if (active [0]) {
digitalWrite (PinsLed [0], On);
sprintf (s, " %d on", 0);
Serial.println (s);
}
}
}
// -----------------------------------------------------------------------------
void
setup ()
{
Serial.begin (9600);
for (unsigned n = 0; n < sizeof(PinsBut); n++) {
pinMode (PinsBut [n], INPUT_PULLUP);
butState [n] = digitalRead (PinsBut [n]);
}
for (unsigned n = 0; n < sizeof(PinsLed); n++) {
digitalWrite (PinsLed [n], Off);
pinMode (PinsLed [n], OUTPUT);
}
}
*/
BBB
AAA
B
A
BOTH