Go Down

Topic: really need help with 4 servos with the arduino (Read 4820 times) previous topic - next topic

afap

I having a very hard time running 4 servos with the arduino. I can only get 2 to work.

Code: [Select]
#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;  // create servo object to control a servo
Servo myservo4;

int potpin1 = 0;  // analog pin used to connect the potentiometer
int potpin2 = 1;
int potpin3 = 2;  // analog pin used to connect the potentiometer
int potpin4 = 3;
int val;    // variable to read the value from the analog pin

void setup()
{
 myservo1.attach(9);  // attaches the servo on pin 9 to the servo object
 myservo2.attach(10);  // attaches the servo on pin 10 to the servo object
 
   myservo3.attach(5);  // attaches the servo on pin 5 to the servo object
 myservo4.attach(6);  // attaches the servo on pin 6 to the servo object
}

void loop()
{
 val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
 myservo1.write(val);                  // sets the servo position according to the scaled value
 val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo2.write(val);
 
 val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo3.write(val);
 
 val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 179, 0);     // scale it to use it with the servo (value between 0 and 180)
 myservo4.write(val);                  // sets the servo position according to the scaled value
                   // sets the servo position according to the scaled value
 delay(15);                           // waits for the servo to get there
}


Thats the code i em using, the turbble is that pin 5 and 6 are not sending pwm signals or any signal for that mater (aslo used LED light to test this) every thing else works. 9 and 10 are running 2 of the servos just fine. all 4 inputs are tested and good. Please help me i have been stuck on this for some time now.

jezuz

I think that you can only run 2 servos using the standard Servo library. You should look into this:

http://www.arduino.cc/playground/Code/MegaServo

afap

I have downloaded this link and it gave me this code

Code: [Select]
#include <MegaServo.h>

// test sketch for MegaServo library
// this will sweep all servos back and forth once, then position according to voltage on potPin


#define FIRST_SERVO_PIN  22  

MegaServo Servos[MAX_SERVOS] ; // max servos is 32 for mega, 8 for other boards

int pos = 0;      // variable to store the servo position
int potPin = 0;   // connect a pot to this pin.

void setup()
{
 for( int i =0; i < MAX_SERVOS; i++)
   Servos[i].attach( FIRST_SERVO_PIN +i, 800, 2200);

 sweep(0,180,2); // sweep once    
}

void sweep(int min, int max, int step)
{
 for(pos = min; pos < max; pos += step)  // goes from 0 degrees to 180 degrees    
 {                                  // in steps of 1 degree
   for( int i =0; i < MAX_SERVOS; i++){
     Servos[i].write( pos);     // tell servo to go to position  
   }
   delay(15);                  // waits 15ms for the servo to move
 }
 for(pos = max; pos>=min; pos-=step)     // goes from 180 degrees to 0 degrees
 {                                
   for( int i =0; i < MAX_SERVOS; i++){
     Servos[i].write( pos);     // tell servo to go to position  
   }
   delay(15);                  // waits 15ms for the servo to move
 }  
}

void loop()
{
 pos = analogRead(potPin);   // read a value from 0 to 1023
 for( int i =0; i < MAX_SERVOS; i++)
   Servos[i].write( map(pos, 0,1023,0,180));  
 delay(15);  
}


I have 4 analog inputs (potentiometer) gong in (0,1,2,3,) and 4 servo. I really don't understand this code it says i can run up to 12 servos with it. I uploaded it to the board as is and go one of the servos to turn one way, so i know im doing something wrong. Im new at this and have a very hard time with code. I still don't under stand why i cant controll more then 2 servos with the pwm pins. I have make lots of led lights fade in and out.

I cam move my wires around to fit any code that being said dose any one have code that will make this work?

Dose any one have an ez way to help me out with this. Thank you

windhamrules

#3
Aug 04, 2009, 02:02 am Last Edit: Aug 04, 2009, 02:06 am by calcdffirefighter Reason: 1
Just out of curisoity... did you mean to intentionaly reverse the range of numbers for myservo1 (pin 9) and myservo4 (pin 6) using the map func? What happens if you set all the servo's to the same range's by saying
Code: [Select]
val = map(val, 0, 1023, 0, 179);

instead of:
Code: [Select]
val = map(val, 0, 1023, 179, 0);

So your code would look like:

Code: [Select]
val = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo1.write(val);                  // sets the servo position according to the scaled value
 val = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo2.write(val);

 val = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo3.write(val);

 val = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo4.write(val);                  // sets the servo position according to the scaled value


Maybe that may help? Dunno but may yield diff results at least.

And I dont know if it matters but if indeed you do want the values of myservo1 and myservo4 reversed you may try doing this and seeing if it yields different results:
Code: [Select]
val = map(val, 0, 1023, 0, 179);
val = map(val, 0, 179, 179, 0);


This just breaks up the math and maybe helps you find an error somewhere...

Chris
Its hard to put the smoke back in the wires

afap

the numbers have been moved around to get the servos facing the right way.  (4in) when i would turn the knob left the servo would go right. so switching the numbers around made the servo turn with the knob.

jezuz

Ok man, I was a little pissed off at the Mega Servo library at first, but later on I figured out that it was just like the normal library. The library says that it can control 12 servos on one pin (if I understood it correctly, this is what I have been thinking for a while now, so please correct me if I am wrong). Since you dont need that many servos, you can simple do a little switchover from normal library to mega library to allow servos on all pins (a little confusing, but here is some code to help)

untested, but if memory serves me right, this will work and should get you started... the servos are connected to pins 8, 9, 10, 11... potentiometers are Analog 0 - 3

Code: [Select]

#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;




void setup()
{

 servo1.attach(8);
   servo1.attach(9);
     servo1.attach(10);
       servo1.attach(11);

}


void loop()
{
 
int A1 = analogRead(0);
int A2 = analogRead(1);
int A3 = analogRead(2);
int A4 = analogRead(3);

int val1 = map(A1, 0, 1023, 0, 180);
int val2 = map(A2, 0, 1023, 0, 180);
int val3 = map(A3, 0, 1023, 0, 180);
int val4 = map(A4, 0, 1023, 0, 180);

servo1.write(val1);
servo2.write(val2);
servo3.write(val3);
servo4.write(val4);
}


afap

ok i have it set up just how you said to and i got the code uploaded but only 1 servo is working out of 4, theres still something wrong with the code.

jezuz

Although it quite well might be code, it might just be an issue of lack of current. How are the servos powered?

afap

Code: [Select]
#include <MegaServo.h>

MegaServo servo1;
MegaServo servo2;
MegaServo servo3;
MegaServo servo4;




void setup()
{

 servo1.attach(3);
   servo2.attach(9);
     servo3.attach(10);
       servo4.attach(11);

}


void loop()
{
 
int A1 = analogRead(0);
int A2 = analogRead(1);
int A3 = analogRead(2);
int A4 = analogRead(5);

int val2 = map(A1, 0, 1023, 180, 0);
int val3 = map(A2, 0, 1023, 180, 0);
int val4 = map(A3, 0, 1023, 0, 180);
int val5 = map(A4, 0, 1023, 0, 180);

servo1.write(val2);
servo2.write(val3);
servo3.write(val4);
servo4.write(val5);
}





had to mod the code a bit but now all of them are working but there is a glitch. the analog in for 2 is running 2 servos at once. is like its bleeding over so how. my wires are flawless so i really dont know whats going to, i dont see the problem in the code (but then again i mite just not be seeing it) other idea is the arduino mite have some kind of problem. I just cant fig out how the one input is getting split to run 2 different pins going to the servos. Grrrrr  :-[im so so close to having this right!!!!  when i have this last thing all done i will take some pics and post them so you can see what im doing. and i want to thank you for getting me this far.

AWOL

Quote
The library says that it can control 12 servos on one pin


It does?
Where?
"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.

eried

I don't like using a bloated library to control servos. The best way to control servos is directly with:

Code: [Select]
#define ANGLE_MIN -90
#define ANGLE_MAX 90
#define PWM_MIN 130
#define PWM_MAX 235


void rotate(int angle, int servo)
{
 analogWrite(servo,map(angle,ANGLE_MIN,ANGLE_MAX,PWM_MIN,PWM_MAX));
}


The servo must be connected to a digital PWM pin in your arduino. Angle and PWM values depends on your servo. To find the PWM value just try a high or lower value if the default does not work well.

Check your arduino for the PWM digital pins, there are almost 4 of them or more = 4 servos at once without troubles  ;D
My website: http://ried.cl

AWOL

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

jezuz

#12
Aug 04, 2009, 09:15 pm Last Edit: Aug 04, 2009, 09:17 pm by jezuz Reason: 1
Well,

Quote
Quote:
The library says that it can control 12 servos on one pin


It does?
Where?


"This library allows an Arduino board to control one to twelve RC (hobby) servo motors on any digital pin on a standard Arduino board"

This is the first line in the MegaServo playground page. Perhaps I am misreading, but I think it is saying "12 servos per pin", not "12 servos total, and each can be placed on whatever pin you want". Like i said, correct me if I'm wrong.

mem

Quote
This library allows an Arduino board to control one to twelve RC (hobby) servo motors on any digital pin on a standard Arduino board
Means that unlike the standard arduino library that can only use pins 9 and 10, the MegaServo library allows the selection of any pin for a servo.  I had assumed it was obvious that each servo needs its own pin, as illustrated in the example sketch.

jezuz

that's too bad, my bad for the mis-information afap. And there I was being all happy that the arduino can run 12 servos per pin.

Go Up