Turning two digital pins on and off with different delays ?

Hello ,

Im currently working on a project that utilizes a 2-Channel Relay module and I need them to switch ON at the same time then switch OFF after different periods of time,

Example :- I want them turn ON at the same time then I want relay number 1 to switch OFF after 3 seconds and relay number 2 to switch OFF after 5 seconds.

I dont think this can be done using the delay(); function, can anyone help me please ? :slight_smile:

I dont think this can be done using the delay()

Yes, such a simple specification can be accomplished with delay(). Just have in mind that the micro does not do anything else when "delaying()" (just waiting).

Regards

It could be done using 2 delay()s one after the other but using millis() would be a better solution.

Save the time the start event happens then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action. In your case there will be 1 start event and 2 different periods so 2 separate checks for the end of period.

UKHeliBob:
It could be done using 2 delay()s one after the other but using millis() would be a better solution.

Save the time the start event happens then each time through loop() check whether the required period has elapsed since the event. If not then go round loop() again reading inputs etc. If the period has elapsed then take the required action. In your case there will be 1 start event and 2 different periods so 2 separate checks for the end of period.

Sorry for the late response, your solution worked like charm, thank you :slight_smile:

its very simple I dont know how I didnt think of that :stuck_out_tongue:

your solution worked like charm

Not actually mine, more a general solution commonly known as BlinkWithoutDelay after the example in the IDE of that name. Now that you know about it you will find it very useful for timing in programs that must keep running freely so that they can do other things such as reading inputs.