Go Down

Topic: How to run 4 servos separately using the Arduino R3 board? (Read 2670 times) previous topic - next topic

Nazih

I am another new commer to this nice hobby.
Purchased an Arduino Uno R3, downloaded the software, connected one Futaba Servo, used the Servo "sweep" example on pin 13 and everything worked perfect.
I want to run 3 more servos separately on other pins (with an outside power supply) but got stuck on the code.
How can I make each servo roate separately (left - right) by modifying the servo "sweep" example?

Appreciate assistance from any one. Regards,

Grumpy_Mike

Simply declare another three instances of the servo. That is the it where you say what pin it is on. Use a different pin and a diffrent name.
Post your code if you have troubl. Use code tags if you do.

DuaneB

Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

Nazih


Simply declare another three instances of the servo. That is the it where you say what pin it is on. Use a different pin and a diffrent name.
Post your code if you have troubl. Use code tags if you do.


Thanks Mike for the feedback. I actually did that but I faced a weird problem. When I connected the two servos on an external power supply the two servos moved in one direction and stopped, however when I connected them both to the Arduino 5V power both servos worked simultaneous. I then used one on the external power and one on the board power, only the one connected to the board works. I am using two identical servos and used signals from pins 8 and 12. What is going wrong? Below is the code:


#include <Servo.h>

Servo myservo;  // create servo1 object to control a servo
                // a maximum of eight servo objects can be created

Servo myservo2;  // create servo2 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(8);  // attaches the servo on pin 8 to the servo object
  myservo2.attach(12);  // attaches the servo on pin 10 to the servo object


void loop()
{
  for(pos = 0; pos < 160; pos += 1)  // goes from 0 degrees to 160 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
  }

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


  for(pos = 160; pos>=1; pos-=1)     // goes from 160 degrees to 0 degrees
  {                               
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }


for(pos = 120; pos>=1; pos-=1)     // goes from 120 degrees to 0 degrees
  {                               
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 30ms for the servo to reach the position
  }
}



Note: after the upload is complete I am getting the following note at the bottom of the page (the black space): avrdude: stk500_getsync() : not in sync: resp=0x00. Does this have anything with the fault??

zoomkat

Four servo test code.

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

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 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 Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Grumpy_Mike

Quote
Note: after the upload is complete I am getting the following note at the bottom of the page (the black space): avrdude: stk500_getsync() : not in sync: resp=0x00. Does this have anything with the fault??

It normally means that the code has not been uploaded to the board correctly and so the sketch running is not the one you think has just been downloaded.

Go Up