So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By landkart
#74073 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
User avatar
By btidey
#74088 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