Go Down

Topic: Controlling 2+ servos simultaneously (Read 1 time) previous topic - next topic

dandres126

Hello I am fairly new to programming and Arduino in general.
I have searched countless forums for the answer to my question but I can't find anything that applies.
I am currently working on a robot that has different motions.
Two servos will be powering one leg of the robot. One of the servos will control the vertical motion (from an angle -60 degrees to 60 degrees).  The other one will control a rotation from -45 degrees to 45 degrees. I am able to program these motions separately, but I hope to achieve the motions simultaneously. So as it moves vertically there will also be the rotation. Both of the motions will happen in the span of 1 second, and it will go backwards as well.
I need to be able to do this on two legs (so four servos) and then there will be an additional servo that will go from -45 degrees to 45 degrees in 1 second as well.
With these parameters how can use arduino to program this motion?

In addition to this, I know to not power the servos through the Arduino board, I am using a 4 AA battery pack, since I have 5 servos, should I consider using an additional 4AA battery pack to power the larger servos?
The servos I'm using are:
2 Hitec HS-805BB Mega Power
2 Hitec HS-646WP
1 Micro Servo 9g A0090

Thank you for reading this inquiry, any help would be greatly appreciated.

zoomkat

Test code for multiple servos.

Code: [Select]

//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up