I have two relays and I want them to work consecutively where the program is executed, and each period of time one of the two relays is ON and the other one is OFF. Then after such a time the first one is OFF and the other one is ON. Is there any function to do that or any method?
No, there is not a “function” or “method” to do this. You need to code the steps individually...
Turn one relay on
Turn the other off
Wait (ideally not with delay, but it will do in this simplistic program)
Turn the first relay off
Turn the second relay on
Wait (again ideally not with delay)
Try starting with the LED blinky example. It will teach you how to turn something on and off.
Hi HassanAli23,
there is no pre-defined function that is doing this. But this is a really beautiful programming exercise to improve your knowledge about programming
This programming execise requieres some initial knowledge. The best way to describe your knowledge-level that you already have about programming is to post code with your try how it might work.
It doesn't matter if your try has only two lines of code or many more.
Two lines of code would just indicate: "I need to know the very fundamental basics".
To me it makes absolutely no sense to post ready to use code that you do not understand. My understanding of this forum is: help others to improve their own knowledge. It is not about replace the users own knowledge through ready to use code.
So please post your attempt how this functionality can be coded by using the basic commands like
pinMode, digitalWrite etc.
best regards Stefan
Good advice from Stefan there.
In the IDE Start with...
File -> Example -> 01. Basics -> Blink
You should at least be able to get that to turn ONE relay on and off, and also have a stab at producing some code for a second output pin to control a second relay.
LED_BUILTIN is pin 13 or most Arduino boards. Which board are you using?
Great advice. Thanks StefanL38.
However, it's just come to my mind to use timer interrupts and I think it can achieve what I want. Isn't it?
I am going to perform a specific task in the loop service routine while in the interrupt service routine I am going to program the switching process depending on the time I am specifiting. Please advise me if this helps or not
StefanL38:
Hi HassanAli23,there is no pre-defined function that is doing this. But this is a really beautiful programming exercise to improve your knowledge about programming
![]()
This programming execise requieres some initial knowledge. The best way to describe your knowledge-level that you already have about programming is to post code with your try how it might work.
It doesn't matter if your try has only two lines of code or many more.
Two lines of code would just indicate: "I need to know the very fundamental basics".To me it makes absolutely no sense to post ready to use code that you do not understand. My understanding of this forum is: help others to improve their own knowledge. It is not about replace the users own knowledge through ready to use code.
So please post your attempt how this functionality can be coded by using the basic commands like
pinMode, digitalWrite etc.best regards Stefan
You're just making you life harder by using interrupts because you aren't writing your "service" code in loop properly.
Learn to write your code in loop() without needing delay() or other long running software loops.
Then you won't need interrupts.