Loop count down for controlling relays

Hi All :)

I am new to the Arduino scene and to all things coding.

I am trying to control 4 relays and have them count down a given number of cycles, I just cant seem to get my head around the "IF" statement, any help would be greatly appreciated.

My attempted code so far:

define RELAY1 10

define RELAY2 11

define RELAY3 12

define RELAY4 13

void setup()

{

// Initialise the Arduino data pins for OUTPUT

pinMode(RELAY1, OUTPUT);

pinMode(RELAY2, OUTPUT);

pinMode(RELAY3, OUTPUT);

pinMode(RELAY4, OUTPUT);

}

void loop()

{

digitalWrite(RELAY1,HIGH); // Turns OFF Relay 1

digitalWrite(RELAY2,LOW); // Turns ON Relay 2

digitalWrite(RELAY3,HIGH); // Turns OFF Relay 3

digitalWrite(RELAY4,LOW); // Turns ON Relay 4

delay(3500); // Pause 3.5 seconds

digitalWrite(RELAY1,LOW); // Turns ON Relay 1

digitalWrite(RELAY2,HIGH); // Turns OFF Relay 2

digitalWrite(RELAY3,LOW); // Turns ON Relay 3

digitalWrite(RELAY4,HIGH); // Turns OFF Relay 2

delay(30000); // Pause 30 seconds

//Then repeat the cycle 100 times and then stop

}

My attempted code so far:

No ifs in there I notice. What did you try ?

Rather than counting and testing with an if why not use a for loop ?

for (int count = 0; count < 100; count++)
{
  //execute this code
}

Put this for loop in the setup function and it will only run once.

@MetMac: Welcome to the Forum. As a newcomer, please read the guidelines for using the Forum by Nick Gammon which appear at the top of the post list. It will help you get the most from the all of us. Also, please use code tags (explained in the posts by Nick) when posting source code...it makes it easier for us to read and use your code.

As to your relay issues, Bob has already made a suggestion, but also keep in mind that the Arduino pins are only designed to sink so much current. Make sure you connect your relays to the Arduino with that limitation in mind. Otherwise, you could end up with a "silicon brick".

Hi UKHeliBob & econjack

Thank you both for your advice and guidance it has been greatly appreciated, the code works a treat. :slight_smile:

Kind regards
Mac

#define RELAY1  10    
#define RELAY2  11                        
#define RELAY3  12                        
#define RELAY4  13

void setup()
            
            
           {for (int count = 0; count < 100; count++)  // cycle count 100
            
           {
            pinMode(RELAY1, OUTPUT);         
            pinMode(RELAY2, OUTPUT);         
            pinMode(RELAY3, OUTPUT);         
            pinMode(RELAY4, OUTPUT);

            
           digitalWrite(RELAY1,HIGH);          // Turns OFF Relay 1
           digitalWrite(RELAY2,LOW);           // Turns ON Relay 2
           digitalWrite(RELAY3,HIGH);          // Turns OFF Relay 3
           digitalWrite(RELAY4,LOW);           // Turns ON Relay 4
           delay(3500);                        // Pause 3.5 seconds
        
           digitalWrite(RELAY1,LOW);           // Turns ON Relay 1
           digitalWrite(RELAY2,HIGH);          // Turns OFF Relay 2
           digitalWrite(RELAY3,LOW);           // Turns ON Relay 3
           digitalWrite(RELAY4,HIGH);          // Turns OFF Relay 2
           delay(90000);                       // Pause 90 seconds 
          }    
          }

 void loop()
                    
          {
           }

No need for the pinMode()s to be in the for loop. Once is enough