Thank you for the proto code! I will test it tonight. I don't unnerstand this switch_state && switch_timestamp && (millis() - switch_timestamp) >= switchPin1TriggeredTime) (i.e. how it is able to do what it does!) which worries me but maybe after playing with it I will.
Is there an easy way to call a function (to play a tone on a piezo speaker) every two minutes when the switch has remained "on" for the pre-determined time limit? Or would I need to use variables to a) track when the piezo function was called and b) create an "alert" state which would control when the function was called?
static unsigned long switch_timestamp = 0;
bool switch_state = digitalRead(switchPin1);
const int switchPin1 = 1;
long switchPin1TriggeredTime = 7200000;
if(switch_state && switch_timestamp && (millis() - switch_timestamp) >= switchPin1TriggeredTime) {
//play a short alert tone every minute
}
else if (!switch_timestamp && switch_state) {
//The !switch_timestamp is to make sure we don't refresh the timer if the switch remained on.
//The pin just changed to on, start the timer
switch_timestamp = millis();
}
else if(!switch_state && switch_timestamp) {
//Switch went off, while timer was running, disable the timer
switch_timestamp = 0;
}
if(switch_state) {
//actual code for when switch is on goes here?
}