Hello there.
I am a newbie to Arduino and have a project that i m working on, i need some help :).
Project Definition: I have 5 relays connected to Arduino that should be energized after some delay in minutes. I read data from Serial port which indicates delay. All the relays have different delays depending on the user entered values on a screen.
(Find attached image)
The problem I am facing is that i used delay() function which hold my program to that point. Can anyone please help me out in programming.
Say if the user enters 7 mins for the first relay delay and 5 mins for the second relay, then Arduino should energize relay 1 and 2 after 7 and 5 mins respectively.
I would be very grateful if someone would help me out.
Do let me know if u need any other info from my side.
The problem I am facing is that i used delay() function
Put a period after that part of the sentence. And, NEVER use delay again.
Look at the blink without delay example. It shows how to note when an event occurs (such as turning a relay on). It shows how to determine if now minus then exceeds some value (like how long the relay should be on. It shows how to execute some action when that happens.
So you should now be able to apply the BWD principle to one relay. Have you done that ? Once you have one working then extending the idea to n relays will be relatively easy.
BWD Principle?? whats that??
Any ways this is my code now.
int intread(){
int i = Serial.read();
// delay(10);
Serial.flush();
// int j =Serial.read();
// delay(10);
// Serial.flush();/
// int m = i + j ;
return i;
// Serial.flush();
}
There is a very good page (written by another user of this forum) with a very nice explanation of this topic (and several other interesting things...).
It starts by showing how one can achieve a "delay effect" without using the delay(), and then it shows how it can be used for more than one elements (even "delays" with different duration for each element).
The page is here: http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
I think the important would be that you understand how it works, and then try to write your own code to do exactly what you want.
What should i put in void Delay(int n) function so that Arduino does not paralyze here??
You cannot simply replace delay() with some magic code that will not cause your program to stall. Instead you need to refactor your code.
When the start event occurs save the value from millis(). Now, on each pass through the loop() function check if required period of time has passed by comparing millis() now with the saved value. If the difference is greater than the required period then the 'delay' is over so act on it. If not go round loop() again checking for other elapsed periods in the same way and acting accordingly.