Pages: 1 [2] 3 4 ... 6   Go Down
Author Topic: really need help with 4 servos with the arduino  (Read 3391 times)
0 Members and 1 Guest are viewing this topic.
London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

afap, the code in post #2 attaches servos on consecutive pins starting from pin 22. Unless you have a mega board this will not work.  If you change the define  FIRST_SERVO_PIN  from 22  to 2, itw ill attach 12 servos starging from pin 2 to pin 14 (analog pin 0).


The code in post #8 is missing the delay at the end of loop. A delay of 15 milliseconds (as you have in your code in post #2)  puts much less stress on the system. Without the delay the code is rewriting each channels value thousands of times a second (and turning off and on interrupts for each write) . Because the channel data is only updated every 20 milliseconds the system is less stressed without handling all the unnecessary calls to Servo.write()

Try this:
Code:
#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);
  delay(15);  // delay 15 milliseconds
}
« Last Edit: August 04, 2009, 02:59:41 pm by mem » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:#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
Just in case you missed AWOL's warning about this suggestion - [glow]please do not do this [/glow]using a standard hobby servo. Using analogWrite to drive a servo can potentially destroy it.

« Last Edit: August 04, 2009, 03:05:17 pm by mem » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Not sure if your comment was  completely serious but I have reworded the playground page, is the following clearer:

This library allows an Arduino board to control one to twelve RC (hobby) servo motors on a standard Arduino board or up to 48 servos on an Arduino Mega. Each servo can be attached to any unused digital pin.
Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It was serious sarcasm, but it's clearer now. And good catch on the missing delays as well  smiley-wink
Logged

magalia ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
When can I get an Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for taking a look at this every one.. here is the code i have uploaded edited by mem

Code:
#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);
  delay(15);  // delay 15 milliseconds
}


unfortunately I still have the same problems. One of the analogs are moving 2 servos. the servo shakes real bad because of getting 2 inputs. with i pull one of the analog inputs out,, then the shaking stops but but still 2 servos turn off from the other analog in, just more smooth. and before you say it NONE of the signal wires are even close to touching.  its got to be the code.  i have moved around and tryed other pins and it always turns out the same problem. Now also please keep in mind that all other servos run fine.  

again i really want to thank all of you for taking the time to help me with this, This is not just a project, its to have pan and tilt on my cams for my house. I have a wife and baby and not to long ago to men broke in to some ones house just down the street more me. anyway the tided up the people how lived there and took there time about 30 to 45 mins robing the place. and they got away with it. So im hope have some big cams on each side of my house with servos making them look even more crazy will word off these people. :-[ The cams go to a PC DVR.
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suggest you print the output values you get from analogRead. Also, for testing purposes, change the delay from 15 to 1000. This will make the values easier to read. (when you have everything working you can change the values back)
Logged

magalia ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
When can I get an Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have never done that, how is it done?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use the Serial object and the serial monitor on the IDE.
http://arduino.cc/en/Reference/Serial
Logged

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

magalia ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
When can I get an Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

WOW im so sick of trying to get this to work,,, i think im just gong to use 2 arduinos i have spend a week or more trying to make this work. and i dont have that kind of time i need it to work now.
Thank you for trying every one./ :-/
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i think im just gong to use 2 arduinos
That's just crazy-talk!

What's the problem, and what have you done to track it down?

Can you post your sketch here?

How are you powering the servos?
« Last Edit: August 06, 2009, 02:04:07 am by AWOL » Logged

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

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yea, like I asked before, how are the servos powered, my bet is on there being insufficient current running to your 4 servos.
Logged

magalia ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
When can I get an Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have a box that im working in. Not using bread board but using rails. one is 5volts taken from the arduino the other is ground taken from arduino. so its not like i can really mess that one up. the trubble is in the signal because i have tested each servo on is in 2s with the code that only runs 2 and they all work fine that way. I read the code over and over again and i can find nothing at all wrong with it. I look over every wire and every thing is right.  I just dont know. What i want to do is set up my other arduino with the same code and set it all up on a bread board and see if that helps me at all.
Logged

magalia ca
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
When can I get an Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
my bet is on there being insufficient current running to your 4 servos.

no i know that problem, it thats different when you us 3 servos at ones with a load on them they "give up" for about on sec, then they try to work again.  I had this problem before but fixed it by using about 10 to 11 volts the run the arduino. I could not use the 12 volts because the arduino got to hot. but it runs nice now.
« Last Edit: August 06, 2009, 12:06:16 pm by Chris_Herrera » Logged

New York
Offline Offline
Edison Member
*
Karma: 1
Posts: 1023
E != m*c^2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure I understand the middle part of what you said - the part about testing the servos... could you rephrase it? When you did the testing, what exactly did you do (be specific). If you tested only one or two servos at a time, you won't have a problem... but running more probably caused issues. You should add another rail on the breadboard hooked up to a secondary power source, perhaps a 4AA battery pack giving you 6V... the ground from this batter should be connected to the ground rail you already have. Use the 6v to power the servos instead of the arduino's +5v.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I had this problem before but fixed it by using about 10 to 11 volts the run the arduino
It doesn't matter how many volts you use (as long as it is greater than about 6 or 7 volts) - the regulator will take care of that, whilst getting hot in the process.

The problem is that the Arduino's regulator is not meant to supply motor current for the servos as well - if you're using servos, you MUST use a separate supply to power them.
Logged

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

Pages: 1 [2] 3 4 ... 6   Go Up
Jump to: