Go Down

Topic: Need help for four relay I/O timer. (Read 440 times) previous topic - next topic


Hey guys, I need some help for my project if you please. So i have to make a relay timer but a little complicated one. In first place, is it possible to make arduino to execute two different loops, one default and one if i press and/or hold a button? If can how will look the sketch for this? Second, i need relay 1 to make cycles 5min on and 30min off, Relay 2, same but to start after Relay 1 is off and relay 3 to work for 20sec on every 2-5 min if relay 1 is on :). I had think i can do it but unfortunately, best i mate till now is to use button to turn on and off one of the relays or to make simple cycle timers but can't made them work together as well or at least use that input to make relay 3 work only if Relay 1 is on. Please help me :(


Uh two different loops is technically a NO but if you google Arduino two things at once it may help. Or if your only wanting it do do something when you press a button then an "if" statement should handle it


In the examples of the IDE you can find a sketch called BlinkWithoutDelay.
Try to understand the concept.
Then Google "Arduino SeveralThingsAtTheSameTime".


Thanks a lot, I will. But if I just want to make the simple variant, how to set that third timer to work 20 seconds on every 5min if some condition (If Relay 1 is ON)? Thanks in advance!


Jan 13, 2018, 09:15 am Last Edit: Jan 13, 2018, 09:15 am by TomGeorge
What do you want the default loop to do?
The one that operates before you press the button?

Is this timing diagram what you are looking for when you press/hold the button?

Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....


Hi Tom,

yes this is exactly what I want for now. If I can set Relay 3 in that sequence to work only if Relay 1 is ON, I can skip the button.


Two timer loops like you see in blink without delay example.

Set a flag, e.g. relay1On, and have the second relay only operate the second relay when that variable is true.

Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.


Jan 13, 2018, 02:26 pm Last Edit: Jan 13, 2018, 03:21 pm by dave-in-nj
this is NOT actual code, just one possible way of doing your loop

the idea is to give you a way to have each step be done in turn as per your system clock.

[ I think I skipped a step for timer3 last time on/off   but the idea is to set lastTime from millis() and then each event occurs at a specified time from that start.   at the end, it re-sets and starts all over again. ]

this starts with a re-set.
you can add a button to reset the timerFlag to 0
or if you have a button to set it to 9 it will stop executing the loop

Code: [Select]

timer1, timer2, timer3  all =  OFF
timerFlag = 0
unsigned long  lastTime
int duration  //  may need to make long ??


lastTime = millis()


duration  = millis() - lastTime // this will give you a number.

if timerFlag== 0
timer1 = ON
timer3 = ON
timerFlag= 1
        lastTime = millis()

if timerFlag == 1
        if duration >  20 seconds
timer3 = OFF
timerFlag = 2

if timerFlag==2
if duration = 2 min 50 sec  //  total coung is now 2 minutes 50 seconds
timer3 = ON

if timerFlag==3
if duration > 3 minutes and 10 seconds
timer3 = OFF
timerFlag = 4

if timerFlag == 4
if duration > 5 minutes
timer1 = OFF
timer2 = ON

if timerFlag==5
if duration > 5 minutes and 40 seconds
timer3 = ON
timerFlag = 6

if timerFlag==6
if duration > 6 minutes
timer3 = OFF

if timerFlag==7
if duration > 10 minutes
timer2 = OFF

if timerFlag==8
if duration > 35 minutes
timerFlag = 0


Hi Tom,

yes this is exactly what I want for now. If I can set Relay 3 in that sequence to work only if Relay 1 is ON, I can skip the button.
what does the button do ?  
does this run all the time or just as an event when other things happen ?
also, does relay 3 = 5.0 minutes of combined ON/OFF time ? or is it 2.5 minutes plus 20 seconds ?


Hi Dave,

thanks to your advice. Button was because I'm thinking more hardware than software. I was think to use that button as "flag". I should use NO contact of my machine and start Relay 3 timer when that NO go to close state. :) Shame on me, but it was easyer way for me to do it.


Jan 13, 2018, 03:18 pm Last Edit: Jan 13, 2018, 03:24 pm by dave-in-nj
mittko, there is no right or wrong way.  there is only any way that works and all ways that do not work.

timing with 20 seconds ON and 2 min 30 seconds off

0 start 1 and 3
20 second  3 off            time 20 second
2min 30sec later, 3 on   time 2min 50 sec
20 sec later 3 off           time 3 min 10 sec
== timer1 OFF at 5 min
== timer2 ON at 5 min
2 min, 30 sec later 3 on   time 5 min 40 sec
20 sec later, 3 off           time 6 min


you can have the N/O contact start relay 3, and then, 20 seconds after Relay1 starts, open relay3, wait 2.5 min, close relay3....

the great thing about software is you can have many things be used as a reference for other things.


Hi again

thanks a lot for your guidance till now. I was busy that days and because this is hobby project i had no time to try that options but I will as soon as possible, probably in the end of the week. Anyway, till now I use the simple option to calculate times to delay between three relays and made simple order that everything work in time I want. It not ok according to learn how the thinks work and how it's should work but I hope will do it right after all. :) Thanks again and will write soon when try to make thing better. C ya :)

Go Up