magalia ca
Offline
Jr. Member
Karma: 0
Posts: 70
When can I get an Arduino
|
 |
« on: August 02, 2009, 07:39:42 pm » |
I having a very hard time running 4 servos with the arduino. I can only get 2 to work. #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.
|
|
|
|
|
Logged
|
|
|
|
|
New York
Offline
Edison Member
Karma: 1
Posts: 1023
E != m*c^2
|
 |
« Reply #1 on: August 02, 2009, 08:57:07 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
magalia ca
Offline
Jr. Member
Karma: 0
Posts: 70
When can I get an Arduino
|
 |
« Reply #2 on: August 03, 2009, 05:32:46 pm » |
I have downloaded this link and it gave me this code #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
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Newbie
Karma: 0
Posts: 49
Woot
|
 |
« Reply #3 on: August 03, 2009, 07:02:03 pm » |
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 val = map(val, 0, 1023, 0, 179); instead of: val = map(val, 0, 1023, 179, 0); So your code would look like: 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: 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...
|
|
|
|
« Last Edit: August 03, 2009, 07:06:44 pm by calcdffirefighter »
|
Logged
|
Chris Its hard to put the smoke back in the wires
|
|
|
|
magalia ca
Offline
Jr. Member
Karma: 0
Posts: 70
When can I get an Arduino
|
 |
« Reply #4 on: August 03, 2009, 08:51:08 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
New York
Offline
Edison Member
Karma: 1
Posts: 1023
E != m*c^2
|
 |
« Reply #5 on: August 03, 2009, 09:09:13 pm » |
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 #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); }
|
|
|
|
|
Logged
|
|
|
|
|
magalia ca
Offline
Jr. Member
Karma: 0
Posts: 70
When can I get an Arduino
|
 |
« Reply #6 on: August 03, 2009, 09:58:36 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
New York
Offline
Edison Member
Karma: 1
Posts: 1023
E != m*c^2
|
 |
« Reply #7 on: August 03, 2009, 10:52:12 pm » |
Although it quite well might be code, it might just be an issue of lack of current. How are the servos powered?
|
|
|
|
|
Logged
|
|
|
|
|
magalia ca
Offline
Jr. Member
Karma: 0
Posts: 70
When can I get an Arduino
|
 |
« Reply #8 on: August 03, 2009, 11:25:55 pm » |
#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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: August 04, 2009, 01:41:15 am » |
The library says that it can control 12 servos on one pin It does? Where?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Chile
Offline
Edison Member
Karma: 28
Posts: 1146
Arduino rocks
|
 |
« Reply #10 on: August 04, 2009, 02:17:33 am » |
I don't like using a bloated library to control servos. The best way to control servos is directly with: #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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19052
I don't think you connected the grounds, Dave.
|
 |
« Reply #11 on: August 04, 2009, 03:44:01 am » |
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
New York
Offline
Edison Member
Karma: 1
Posts: 1023
E != m*c^2
|
 |
« Reply #12 on: August 04, 2009, 02:15:34 pm » |
Well, 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.
|
|
|
|
« Last Edit: August 04, 2009, 02:17:02 pm by jezuz »
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #13 on: August 04, 2009, 02:38:57 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
New York
Offline
Edison Member
Karma: 1
Posts: 1023
E != m*c^2
|
 |
« Reply #14 on: August 04, 2009, 02:58:39 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
|