Go Down

Topic: Randomly selecting output pins for 5 servos (Read 548 times) previous topic - next topic

FredGB

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.

Code: [Select]
// 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

PaulS

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.

Code: [Select]
  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.

Code: [Select]
{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.

AWOL

#2
Jan 19, 2013, 05:17 pm Last Edit: Jan 19, 2013, 05:19 pm by AWOL Reason: 1
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.

Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

FredGB

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

FredGB

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:-

Code: [Select]


// 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

Go Up