Declare global variable to keep track of the time that the switch has remained in it's position. Record the edge time for when the switch changed states.Keep checking for the the time it's been since it's been switched (millis() - timeSwitched) and compare that to your interval time.

Declare global variable to keep track of the time that the switch has remained in it's position. Record the edge time for when the switch changed states.Keep checking for the the time it's been since it's been switched (millis() - timeSwitched) and compare that to your interval time.

Yes that's what I thought initially, but how do I avoid overwriting the timeSwitched with millis() everytime the IF statement checks to see if the switch is still HIGH?

Declare global variable to keep track of the time that the switch has remained in it's position. Record the edge time for when the switch changed states.Keep checking for the the time it's been since it's been switched (millis() - timeSwitched) and compare that to your interval time.

Yes that's what I thought initially, but how do I avoid overwriting the timeSwitched with millis() everytime the IF statement checks to see if the switch is still HIGH?

Thanks!

Remember the previous state of the switch, and only record the 'start time' when you see the switch change to state B. This is what Arrch refers to as the edge time.

I only provide help via the forum - please do not contact me for private consultancy.

Remember the previous state of the switch, and only record the 'start time' when you see the switch change to state B. This is what Arrch refers to as the edge time.

Yup. The "Edge time" refers to the time in which the state changes. The rising or positive edge is when the state goes from low to high, and the falling or negative edge is low to high. Do you will need to keep track of one of those by keeping track of the last state.

So the pseudo code for tracking the positive edge would be:

get current state

if last state is low and current state is high // Positive Edge set edge time to current

set last state to current state

if edge time minus current time is greater than threshold time // Do something here

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?

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(); }

if (switch_state == HIGH) //The switch is depressed{ if (switch_timestamp > 0) //The switch was depressed the last time we checked it too { if (millis() - switch_timestamp >= switchPin1TriggeredTime) //It's been 2 minutes. { //Play a short tone. } }}

To call a function every 2 minutes, you are correct - you would need a variable to track the last time it was called, and update the variable every time you called it. Same exact logic we're using to determine when to press it the first time.

One "hack" you could do to get this working fast, is every time you play the tone, reset the switchPin1TriggeredTime back to the current system clock.

Thank you for this hack, it seems to have a lot of potential. However when using it, I've found that what happens is that the warning tone is re-fired after switchPin1TriggeredTime - not every minute. Would you mind seeing if there's something simple to address to get it working? I've had a play myself to no avail :-)

What this means (assuming timestamp isn't changed), is that after 6 seconds, the timestamp will will be equal to the current time, so that means overall, it will run again 6 + interval seconds. If you want it to run every minute, then you simply need to add 1 minute to what the current timestamp is:

What this means (assuming timestamp isn't changed), is that after 6 seconds, the timestamp will will be equal to the current time, so that means overall, it will run again 6 + interval seconds. If you want it to run every minute, then you simply need to add 1 minute to what the current timestamp is: