-->
Page 1 of 1

Set Relay On For Spesific Time Limit

PostPosted: Mon Feb 19, 2018 7:51 pm
by landkart
Okay here i have a problems on how to set my relay on for spesific time limit. I mean i made some conditions which is to make the relay on for some spesific time limit. For example if the code that nodemcu recieve is number 6, the relay will on for 10 mins. if the code is 5, relay will on for 8 mins. and if the code is 4 then relay will on for 6 mins. hows the code to make it like that?
as logic its simple, but im strugling to find the solutions..
thanks anyway

Re: Set Relay On For Spesific Time Limit

PostPosted: Tue Feb 20, 2018 7:47 am
by btidey
In the function that receives the code to turn the relay on.

a) Set a global variable (e.g. unsigned long relayStart) to current millisecond tick count millis()
b) Set a global variable (e.g unsigned long relayDelay) to number of milliseconds needed depending on code received. Code 6 -> 10 minutes -> 600000 milliseconds

In the foreground loop check if relayDelay > 0 and (current millisecond tick - relayStart) is greater than relayDelay. If so call relayOff function.

if(relayDelay > 0 && (millis() - relayStart) > relayDelay) relayOff();

relayOff function should turn relay Off and set relayDelay = 0