I have a general question. I’m programming my particle photon to trigger a relay to open a solenoid and water my garden. I want the solenoid to stay open for a certain amount of time so i have a “delay(xxx)” statement in my function. the function is basically this:
function: relayOn
turn relay on
delay
turn relay off
I have a separate function that will turn the relay off, stopping the flow of water:
function: relayOff
turn relay off
I’m using IFTTT to launch the function to make this happen and everything works well…unless i send commands in the order:
relayOn
relayOn
relayOff
The relay turns on. then it turns off when the relayOff command is sent. then after the delay in the first relay function is complete, the relay turns On again and waits for the delay.
So I gather from this that the current function stops when I use IFTTT to start a new function and then it resumes when the new function stops.
How can I send a turn off relay command that will exit all the existing relayOn functions in the queue?
Why not use a global volatile variable to track the state. Your functions would simply change the value of this variable. Your main loop would use it to turn on or off the relay. As for timing, save the current uptime when you turn on the relay, ie unsigned long onTime = millis(). Then in the loop check to see if the time you want has ellapsed ie if (onTime + 60000 <= millis()) // 60 sec