Requesting help programming multiple relays on a single relay board...

Hello, I've been trying desperately for the past few weeks or so to obtain any information to solve my simple-seeming yet frustrating problem. Now I have used the Blink programs to blink LED's and even used it to run and activate a single SONGLE relay, yet to my surprise as soon as I try to run the same program yet adding in multiple relays and initializing multiple pins as outputs to those relays, I get power provided and everything seems great until I try to run the program for multiple relays at separate times instead of running them in sequence. I almost didn't include the code in this post because of how much of a noob I am at programming, and the fact the code I "programmed" myself is just an added few lines to the "read-only" version of blink. Here is the code I used, (not knowing what format to put it in due to being a noob at programing and to this forum...

#define RELAY_ON 0
#define RELAY_OFF 1

#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5

void setup() /****** SETUP: RUNS ONCE ******/
{
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);

//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(1000000); //Check that all relays are inactive at Reset

}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
delay(1000); // wait for a second
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(500); // wait for a second
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
delay(500); // wait for a second
digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
delay(500); // wait see all relays ON

digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
delay(500); // wait for a second
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(500); // wait for a second
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
delay(500); // wait for a second
digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF
delay(500); // wait see all relays OFF

}

GeezusDaOG:
Here is the code I used, (not knowing what format to put it in due to being a noob at programing and to this forum...

Read this before posting a programming question ...

  digitalWrite(Relay_1, RELAY_OFF);
  
//---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);

You write to the pin before you set it as an output?

delay(1000000); //Check that all relays are inactive at Reset

one thousand second wait?? 15 minutes. Seems like nothing works??

You write to the pin before you set it as an output?

Yes, that is correct to guarantee startup with no relays activated.

not knowing what format to put it in due to being a noob at programing and to this forum...

Maybe if you read this before posting a programming question it would have helped

As to your problem, have a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

terryking228:
one thousand second wait?? 15 minutes. Seems like nothing works??

Yes, that is correct to guarantee startup with no relays activated.

Yeah I just did that as a quick fix when I set delays to 500 ms. and watched the "knight rider" effect take place. But now I'm going to try the fix provided by UKHeliBob. Thank you all for the prompt replies!!

UKHeliBob:
Maybe if you read this before posting a programming question it would have helped

As to your problem, have a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

I skimmed through the [ but didn't take much from it as it didn't solve my problem, so, sorry about that. But I did some searching through the forums for a little while and couldn't find anyone with a similar problem, that's why I assumed it was an easy fix. I am going to read and study the "using millis() for timing", yet interestingly enough, I've read this article already, and have watched many videos on the topic of millis for timing, but am finding it so hard to grasp and can't fully conceptualize how it works. I will post back here if I require more help after really diving into and fully understanding each and every function and its capability to change things in order to help with my simple project.

Thanks!!](http://forum.arduino.cc/index.php?topic=97455.0)

millis() for timing ?
Easy :

Make sure that there is nothing preventing loop() running freely
Save the value of millis() when the trigger event occurs and set a boolean value to true
Each time through loop()
if the boolean in true
check whether the current time minus the start time is greater than the required period
if so then the period is over so execute the end of period actions and set the boolean to false
if not then keep going round loop()