Pages: [1]   Go Down
Author Topic: Randomly selecting output pins for 5 servos  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
Northeast England
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50366
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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:
{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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees
the comment doesn't match the code. Lose it; the code is obvious enough.
« Last Edit: January 19, 2013, 11:19:10 am by AWOL » Logged

"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.

Northeast England
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Northeast England
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
   for(int thisPin = 0; thisPin < pinCount; thisPin++)
     {pinMode (servoPins [thisPin], OUTPUT); }

Should IMO be:
Code:
// no change to the logic, considerable change to readability
for(int thisPin = 0; thisPin < pinCount; thisPin++)
{
    pinMode (servoPins [thisPin], OUTPUT);
}

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

Code:
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.
« Last Edit: January 19, 2013, 08:49:56 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13915
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

update: I did not fix bugs, only layout!
« Last Edit: January 20, 2013, 05:03:30 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26509
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

"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.

Northeast England
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-lol
Logged

Pages: [1]   Go Up
Jump to: