Using an IFTTT action to break another action

Hi,

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?

Thanks,
Rich

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

Hi Fragma,

Thanks for your response. Do you have any example code that you could point me to for this type of solution?

Thanks,
Rich

How about this:

#define relayPin D4
volatile bool relayStatus = false; 
bool priorStatus = false;
unsigned long onTime = -1;


void setup() {
	pinMode(relayPin, OUTPUT);
	Particle.function("relayOn", relayOn); 
	Particle.function("relayOff", relayOff); 
}
	
void loop() {
	unsigned long nowSec = millis()/1000UL;
	if (priorStatus != relayStatus)
	{
		if (relayStatus)
		{
			onTime = nowSec;
			digitalWrite(relayPin, HIGH);
			priorStatus = true;
		}
		else
		{
			digitalWrite(relayPin, LOW);
			priorStatus = false;
		}
	}
	if (relayStatus && onTime + 60 <= nowSec) //relay has been on 60sec
		relayStatus = false;

	Particle.process();
}

int relayOn(String extra)
{
	relayStatus = true;
    return 1;
}

int relayOff(String extra)
{
	relayStatus = false;
    return 0;
}

I do something similar in one of my programs but instead of an on/off I send in a runtime so it’s more flexible and you only need 1 function.

Fabulous! Thank you.