Randomly selecting output pins for 5 servos

Hi

I am new to this forum and quite new to the Arduino.
I have compiled a Sketch that will operate 5 servos, each will open a gate for 3 seconds then close wait 5 seconds them proceed to the next servo.
I have checked to program and it works as required.
I now need to be a able to select each gate randomly and not repeat in a sequence of 5.
I believe the random function is not truly random so I considered creating arrays with alternate sequences and selecting appropriately.

If someone could advise a solution or point in right direction I would really appreciate it.

The code I have compiled is listed below. I have tried to notate as much as possible.

// 5 Servos connected via separate power supply

//To create 5 gates each controlled by separate servo to to open for 3 seconds with 5 seconds between next event



#include <Servo.h>
 
Servo myservo5;  // create servo object to control a servo
Servo myservo1; // 5 servos to control
Servo myservo2; // all numbered accordingly
Servo myservo3;  
Servo myservo4;        
 
int pos = 0;    // variable to store the servo position
int del3 = 3000;  // sets delay time to 3 secondd
int del5 = 5000;   // sets delay time to 5 seconds
 
void setup()
{
  myservo5.attach(13);  // attaches the servos on pins 9 to 13 to the servo object
  myservo1.attach(12);
  myservo2.attach(11);
  myservo3.attach(10);
  myservo4.attach(9);
}
 
 
void loop()                        //loop opening and closing gates
{
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of 1 degree
    myservo5.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
{delay(del3);                        // Holds gate open for 3 seconds before closing sequence
}
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees
  {                                
    myservo5.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 {delay(del5);                        // Waits 5 seconds before next gate opens
}
//******************************servo5****************************************
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
{delay(del3);                        // Holds gate open for 3 seconds before closing sequence
}
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees
  {                                
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   {delay(del5);                      // Waits 5 seconds before next gate opens
}
//******************************servo4****************************************
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of 1 degree
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
{delay(del3);                         // Holds gate open for 3 seconds before closing sequence
}
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees
  {                                
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   {delay(del5);                         // Waits 5 seconds before next gate opens
}
//******************************servo3****************************************
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of 1 degree
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
{delay(del3);                        // Holds gate open for 3 seconds before closing sequence
}
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees
  {                                
    myservo3.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   {delay(del5);                          // Waits 5 seconds before next gate opens
}
//******************************servo2****************************************
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of 1 degree
    myservo4.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
{delay(del3);                        // Holds gate open for 3 seconds before closing sequence
}
  for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees
  {                                
    myservo4.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
   {delay(del5);                          // Waits 5 seconds before next gate opens
}
//******************************servo1****************************************
}

Many thanks in advance

Fred

Your post title is wrong. You do not want to randomly select the pins that the servos are attached to. Those are fixed when you connect the servos.

You could create an array of servos, and randomly select the index into the array, to define which servo to act on.

It would be useful for you to learn when { and } are needed, and when they simply look silly.

  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
  {                                  // in steps of 1 degree
    myservo5.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }

Needed.

{delay(del3);                        // Holds gate open for 3 seconds before closing sequence
}

Silly.

If you are going to have comments that state the obvious, they should at least be correct. Your loops do not end where the comment says they do.

You'd be better using an array of servo objects, with a corresponding boolean array.
The random number generated gives you an index into the array.
When you've moved the servo, you set the corresponding boolean.
Booleans are reset at the end of all five sequences.

for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degreesthe comment doesn't match the code. Lose it; the code is obvious enough.

Thanks for the prompt responses.

Sorry PaulS if my coding was stupid - I have just started learning this software and will try harder to ensure no further silly coding.
Thanks AWOL for your constructive response , I will read up on using an array of servo objects.

Many thanks

Fred

Hi Again
I have re done my code using arrays.
My code is shorter and more compact but I am at a loss as to where I control the angular movement of the servos.

Here is what I have at the moment:-

// 5 Servos connected via separate power supply

//To create 5 gates each controlled by separate servo to to open for 3 seconds with 5 seconds between next event



#include <Servo.h>
 

Servo servo1;
Servo servo2;
Servo servo3;  
Servo servo4;   
Servo servo5;
 
int pos = 0;    // variable to store the servo position
int del3 = 3000;  // sets delay time to 3 secondd
int del5 = 5000;   // sets delay time to 5 seconds
int servoPins[] = {9,10,11,12,13 };
int pinCount = 5;
int Gate[] = {'servo1','servo2','servo3','servo4','servo5'};
 
void setup()
{
 int thisPin;
   for(int thisPin = 0; thisPin < pinCount; thisPin++)
     {pinMode (servoPins [thisPin], OUTPUT); }
}
 
 
void loop() //loop opening and closing gates

{
  // loop from the lowest pin to the highest:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) 
  {
    // turn the pin on:
    digitalWrite(servoPins[thisPin], HIGH);  
      for(pos = 0; pos < 90; pos += 1)  
      // i'm sure a Gate to go here somewhere?
        delay(del3);                       
       for(pos = 90; pos>=1; pos-=1)  
        // Gate to go here?    
        delay(del5); 
    // turn the pin off:
    digitalWrite(servoPins[thisPin], LOW); }

I hope this one is not silly

Regards

Fred

FredGB:
I hope this one is not silly

Your use of braces ({ and }) is still silly IMO, I'm afraid.

You should have braces at the start and end of each function. You should have them after each for, if, else, do, while and switch clause. You should not have them in the middle of a linear code sequence.

Each { and } should be on a separate line with matching pairs indented by the same amount and code between them indented by one extra level.

For example:

   for(int thisPin = 0; thisPin < pinCount; thisPin++)
     {pinMode (servoPins [thisPin], OUTPUT); }

Should IMO be:

// no change to the logic, considerable change to readability
for(int thisPin = 0; thisPin < pinCount; thisPin++)
{
    pinMode (servoPins [thisPin], OUTPUT); 
}
      for(pos = 0; pos < 90; pos += 1)  
      // i'm sure a Gate to go here somewhere?
        delay(del3);                       
       for(pos = 90; pos>=1; pos-=1)  
        // Gate to go here?    
        delay(del5);

For loops without braces, making it easy to inadvertently associate the wrong statements with them. Make it explicit like this:

for(pos = 0; pos < 90; pos += 1)  
{
    // i'm sure a Gate to go here somewhere?
    delay(del3);                       
    for(pos = 90; pos>=1; pos-=1)  
    {
        // Gate to go here?    
        delay(del5); 
    }
}

(I'm not sure I got the structure right here, because it's not at all obvious from your code what you intended it to be or what it actually was.)

You do nothing useful in both the for loops above so presumably this code is in a partially-hacked state and you are intending to populate some of those for loops with code to move servos.

this is a way to proper intended your code // removed some superfluous comments and added some { } where needed.

// 5 Servos connected via separate power supply
// To create 5 gates each controlled by separate servo to to 
// open for 3 seconds with 5 seconds between next event

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;  
Servo servo4;   
Servo servo5;

int pos = 0;        // the servo position
int del3 = 3000;    // delay time of 3 seconds
int del5 = 5000;    // delay time of 5 seconds

int servoPins[] = {
  9, 10, 11, 12, 13 };
int pinCount = 5;
int Gate[] = {
  'servo1','servo2','servo3','servo4','servo5'};

void setup()
{
  int thisPin;
  for (int thisPin = 0; thisPin < pinCount; thisPin++)
  {
    pinMode (servoPins [thisPin], OUTPUT); 
  }
}

void loop()
{
  for (int thisPin = 0; thisPin < pinCount; thisPin++) 
  {
    digitalWrite(servoPins[thisPin], HIGH);  
    for(pos = 0; pos < 90; pos += 1)
    { 
      // i'm sure a Gate to go here somewhere?
    }
    delay(del3);                       
    for(pos = 90; pos>=1; pos-=1)
    {
      // Gate to go here?
    }    
    delay(del5); 
    digitalWrite(servoPins[thisPin], LOW); 
  }
}

you turn :wink:

update: I did not fix bugs, only layout!

int Gate[] = {
  'servo1','servo2','servo3','servo4','servo5'};

I think I see what you're trying to do, and whilst that will compile, it can't ever work.
You need an array of Servo objects.

Hi

Many thanks for the comments and information.
I now see what was meant by previous posts.

I will use this info and try again.

Regards to all

Fred XD