how can i control 4 servos but at diff times

Hi i'm trying to figure out how to control 4 servos at diff times for a robo hand that i have built and it's all working and i can get them all working at once but not at separate times.please help!

Hi Lantz,

It would be helpful if we could see your code. Please post a copy for us to review. When you do, highlight it and then press the # button above to put it in a code block.

You'll also need to describe how you want them individually controlled. Each by its own switch, or pot? What are you trying to do?

Sounds interesting, looking forward to learning more about it!

Pat.

You send the servo commands at the seperate times.

#include <Servo.h>

Servo myservo[5];
int buttonPin[] = {6, 9, 13, 11}; //pin numbers on the board for the buttons
int buttonState[] = {0, 0, 0, 0, 0};
int lastButtonState[] = {1, 1, 1, 1, 1};
int servospeeds[] = {10, 20, 30, 40, 50}; //in milliseconds
int pos[] = {0, 0, 0, 0, 0}; //stores the next positions the servos are heading to, this is needed for the speed control
int lastPos[] = {0, 0, 0, 0, 0};
int positions[5][5] = //the ultimate servo destinations

{
{3, 45, 90, 135, 177},
{177, 3, 45, 90, 135},
{135, 177, 3, 45, 90},
{90, 135, 177, 3, 45},
{45, 90, 135, 177, 3},
};

void setup()
{
int f, g;
Serial.begin(9600);
int x = 11;
for (f=0; f<5; f++)
{
myservo[f].attach(x); x--; //pin numbers on the board for the servos
}
for(g = 0; g < 5; g++)
{
pinMode (buttonPin[g], INPUT); //sets the button pins as inputs
}
}

void moveServo(int pp)
{
int i, j;
buttonState[pp] = digitalRead(buttonPin[pp]);
if ((buttonState[pp] != lastButtonState[pp])&&(buttonState[pp] == 0)) /if button has changed state and is now depressed/
{
for(pos = lastPos; pos <= positions[pp]; pos ++)
* {*
* for(j = 0; j < 5; j++)*
* {*
myservo[j].write(pos_); /i starts where it was left before and is incremented in steps of 1 degree to control the speed. I know the problem is around here, I tried to nest another for loop to increment the rows of the positions array first and then incrementing the columns, but what I get is that the servos do their things one after the other while I want them to move altogether./
* }
delay(servospeeds[3]);_

lastPos _= pos;
}
for(pos = lastPos; pos >= positions[pp]; pos --)
{
for(j = 0; j < 5; j++)
{
myservo[j].write(pos);
}
delay(servospeeds[3]);_

lastPos _= pos;
}
}
lastButtonState[pp] = buttonState[pp];
}
void loop()
{
int p;
for (p = 0; p < 5; p++)
{
moveServo(p);
}
}*

and i am just trying to go straight from computer_

Just a quick comment. Go back and modify your last post: select all and then press the # button above to put it in a code block window.

The code looks like it controls 5 servos, not 4.

Also, it looks like you are controlling them independently - each has its own button.

I'm not sure I understand what you want it to do, then...

Ok myservo[5] => 0-5 or 6 in all
...p < 5 => ( if you start p at zero) 0-4, it never gets to 5, so you were close.
It should be p<4 => 0-3 or 4 in all.

With this code, you are trying to control 5 servos, not 4 like tou posted, is that correct?

This code sends 6 proportional channels every 20 mS and a telemetery packet back evey 100mS
http://kiwitricopter.blogspot.co.nz/2012/11/building-arduino-receiver-yellow-boats.html

TX code
https://docs.google.com/open?id=0B8mGA5b7qZ15dFJHaEdxbmF5dTQ

RX Code
https://docs.google.com/open?id=0B8mGA5b7qZ15b1pDOHA1SnU3cEE

The Rx is setup for a V tail and gyro stability control

#include <Servo.h>

Servo myservo[5];                           
int buttonPin[] = {6, 9, 13, 11};          //pin numbers on the board for the buttons
int buttonState[] = {0, 0, 0, 0, 0};        
int lastButtonState[] = {1, 1, 1, 1, 1};
int servospeeds[] = {10, 20, 30, 40, 50};   //in milliseconds
int pos[] = {0, 0, 0, 0, 0};                //stores the next positions the servos are heading to, this is needed for the speed control 
int lastPos[] = {0, 0, 0, 0, 0};
int positions[5][5] =                       //the ultimate servo destinations

{  
  {3, 45, 90, 135, 177},       
  {177, 3, 45, 90, 135},        
  {135, 177, 3, 45, 90},       
  {90, 135, 177, 3, 45},        
  {45, 90, 135, 177, 3},          
};


void setup() 
{
  int f, g;
  Serial.begin(9600); 
  int x = 11;
  for (f=0; f<5; f++)
  {                                   
   myservo[f].attach(x); x--;           //pin numbers on the board for the servos
  }
  for(g = 0; g < 5; g++)
  {                                                
   pinMode (buttonPin[g], INPUT);       //sets the button pins as inputs
  }
}


void moveServo(int pp)
{
 int i, j;
 buttonState[pp] = digitalRead(buttonPin[pp]);
 if ((buttonState[pp] != lastButtonState[pp])&&(buttonState[pp] == 0))  /*if button has changed state and is now depressed*/ 
 {
  for(pos[i] = lastPos[i]; pos[i] <= positions[pp][i]; pos[i] ++)
  {
   for(j = 0; j < 5; j++)
   {
    myservo[j].write(pos[i]); /*i starts where it was left before and is incremented in steps of 1 degree to control the speed. I know the problem is around here, I tried to nest another for loop to increment the rows of the positions array first and then incrementing the columns, but what I get is that the servos do their things one after the other while I want them to move altogether.*/
   }
   delay(servospeeds[3]);
   lastPos[i] = pos[i];
  }
  for(pos[i] = lastPos[i]; pos[i] >= positions[pp][i]; pos[i] --)
  {
   for(j = 0; j < 5; j++)
   {
    myservo[j].write(pos[i]);
   }
   delay(servospeeds[3]);
   lastPos[i] = pos[i];
  }
 }
 lastButtonState[pp] = buttonState[pp]; 
} 

void loop()
{
 int p;
 for (p = 0; p < 5; p++)
 {
  moveServo(p);
 } 
}

I wanted to be able to rearrange the code to do diff things

I wanted to be able to rearrange the code to do diff things

Without knowing what the different things are it is impossible to help.

However, if you want truly independent control then you need to follow the method used in the blink without delay example sketch found in your arduino software.

and correct I wanted 4 servos not 5 thanks!!

hey zoomkat i tried but the board restarts the program every up load
can i do anything for that?
thanks!

The code you posted, is that your code or someone else's?

A friend emailed it to me i had to change a couple things but i was never able to get them working at diff times.

i'm starting to think i have to restart the code
I want to make it more like the knob code but controlling 4 servos

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
void setup() 
{ 
  myservo.attach(11);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  myservo.write(170);                  // sets the servo position according to the scaled value 
  delay(1000);                           // waits for the servo to get there 
  myservo.write(100);                  // sets the servo position according to the scaled value 
  delay(1000);                           // waits for the servo to get there                          
}

but able to be move around the code to do diff things

lantz_tristan:
hey zoomkat i tried but the board restarts the program every up load
can i do anything for that?
thanks!

The arduino resets/reboots when a program is uploaded or the arduino detects something making a serial connection to its serial port. The reset can be blocked by using a capacitor or resistor on the board.

Out of curiosity how are you powering the 4 servo motors?

Lefty

i came up with this
and right now my hand is playing marry had a little lamb (well the first part) thanks for all the help!
but i have 1 more question how can i set the mid point for one of the servos?

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(11);  // attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  myservo.attach(13);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);    // waits 15ms for the servo to reach the position 
  }
   myservo.attach(6);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
  myservo.attach(13);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
    myservo.attach(11);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(11);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(11);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(13);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(13);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(13);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
      myservo.attach(11);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(11);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
    myservo.attach(11);
  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

but i have 1 more question how can i set the mid point for one of the servos?

First please change all those POS to POS1, POS2, 3, 4, etc... dont keep them all the same, because it makes it harder to debug.
Second, that is A LOT of copy and paste.

To start at mid point, your POS must be at 90 then either decrement or increment to 0 or 180.

and right now my hand is playing marry had a little lamb

Too much information!