// https://wokwi.com/projects/353587272457307137
// https://forum.arduino.cc/t/timer-function-for-staggered-switching/1076239
# define mutePin 6
# define muteLED 7 // mute status lamp
# define MUTE4 777 // MUTE4 for 777 milliseconds
void setup() {
Serial.begin(115200);
Serial.println("mute timer demo\n");
pinMode(mutePin, INPUT_PULLUP);
pinMode(muteLED, OUTPUT);
}
unsigned long now; // time for all non-blocked functions
void loop()
{
now = millis();
// maybe initiaite/refresh a mute period
if (!digitalRead(mutePin))
mute();
// check if it is time to unmute
unMute();
}
bool muteIsOn; // just so the pump is only switched if it is in the wrong state
unsigned long muteTimer;
void mute()
{
muteTimer = now;
if (muteIsOn == false) {
muteIsOn = true;
Serial.println("mute turned ON!");
digitalWrite(muteLED, HIGH); // and do relay or whatever needs doing
}
}
void unMute()
{
if (now - muteTimer < MUTE4) // time to unmute?
return;
if (muteIsOn == true) {
muteIsOn = false;
Serial.println("mute going OFF.");
digitalWrite(muteLED, LOW); // and do relay or whatever needs doing
}
}