Pages: [1]   Go Down
Author Topic: How to run 4 servos separately using the Arduino R3 board?  (Read 2142 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34230
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a starting point -

http://rcarduino.blogspot.com/2012/01/can-i-control-more-than-x-servos-with.html

Duane B

rcarduino.blogspot.com
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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(smiley-cool;  // 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??
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9636
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Four servo test code.

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

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34230
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: