problem with robot arm simple code with 4 Servos

Hi ,

I need help with this code , my problem is the code run once successfully as i want and in the second turn only 2 motions of the arm are executed
(the lifter down and up ) but the gripper and "lhand" , "rhand" don't work .

i do know that the void loop function code run forever fully not particularly .

here is my code

#include <Servo.h>     // header file for Servo Motors 

Servo lifter;
Servo gripper;
Servo rhand;
Servo lhand;

void setup () 
{ 

  lifter.attach(9);
  gripper.attach(10);
  lhand.attach(5);
  rhand.attach(6); 

  lifter.write(90);
  gripper.write(90);
  lhand.write(20);
  rhand.write(150);
}


void loop () 
{ 
  
  
  delay(2000);
  lifter.write(120);
  delay(2000);
  gripper.write(65);
  delay(2000);
  lifter.write(60);
  delay(2000);
  lhand.write(90); rhand.write(90);
  delay(2000);
  
}

The usual issue with multi-servo setups is a totally inadequate power supply. How are you powering your servos?

I'm not sure what you think should happen.

It seems to me that on every iteration of your loop() your servos are being asked to move to the same position. After the first iteration they will be at that position so they won't need to move any more in subsequent iterations.

Have you looked at the servo-sweep example that comes with the Arduino IDE?

I wonder if what you really want is for all the servos to move a tiny bit in each iteration of loop() so that it takes many iterations for them all to get to their desired positions. That way they will all seem to be moving at the same time rather than one after the other.

You should probably also study the Blink Without Delay example sketch and the demo several things at a time

...R

Im really happy for your replies , thank u very much
i think i got it :smiley:

i'm powering my servos with external power source ( 5V, 1A) for each one (separate channel ) .

BUT i'm facing a new problem that the servos is very fast , it may damage the thing they carry
so i tried the" sweep" way to make the servo go from angle.. say 90 to 120 step by step with delay 20 ms
it worked successfully.
but i'm asking
1 - if there is any other way better than that way to make the servos move slower and smooth
2 - in this case which pin type should i connect the servos with ( PWM or Digital ) .

thank u again .

Sgermany:
1 - if there is any other way better than that way to make the servos move slower and smooth
2 - in this case which pin type should i connect the servos with ( PWM or Digital ) .

You can vary the numbers in the sweep concept to get different speeds. If you use servo.writeMicroseconds() you can get finer control. 1000 microsecs is about 0 deg, 1500 = approx 90 and 2000 = approx 180. You should experiment to find suitable values for your servos as they are not all the same.

While the position of a servo is determined by the width of the pulse the servo signal is very different from the signal produced by analogWrite() which can only use the PWM pins. Servos don't have to be on PWM pins. And just to clarify the pins marked PWM can be used as normal digital pins - as can the analog pins. (I don't think servos can be on the analog pins)

...R

A discussion concerning controlling the speed of servo rotation that might be of interest.

http://forum.arduino.cc/index.php?PHPSESSID=701fqattee0p8dn8tp30eegrl6&topic=61586.msg445064#msg445064

Thanks a lot .

Robin2:
(I don't think servos can be on the analog pins)

I just tested a servo on A0 and A5 with the sweep example, it does work.

JimboZA:
I just tested a servo on A0 and A5 with the sweep example, it does work.

Proof, if proof were needed, of the value of learning-by-doing

...R

i am just confused. i am also creating a program for controlling servos in a robot arm. i read that you should use servo.refresh() every 50 or 20ms but this code does not work for the library servo.h. but why? what is the difference between servo.h and SoftwareServo.h . i can only see the code servo.refresh in SoftwareServo.h. please answer.. thank you! :slight_smile:

Mary_Yvette:
i am just confused. i am also creating a program for controlling servos in a robot arm. i read that you should use servo.refresh() every 50 or 20ms but this code does not work for the library servo.h. but why? what is the difference between servo.h and SoftwareServo.h . i can only see the code servo.refresh in SoftwareServo.h. please answer.. thank you! :slight_smile:

I am not familiar with the SoftwareServo library but I guess if you must call refresh() that often that it only generates a pulse for the servos when you call refresh() - strange! There is a similar (but different) concept in the AccelStepper library where you need to call step() regularly. But the difference is that a servo expects a signal continuously whereas a stepper motor only needs a signal when you actually want it to move.

The normal Servo.h library continuously repeats the pulse for the current servo position and updates the pulse width as soon as you do servo.write(). And the ServoTimer2 library also works that way.

Have you any reason not to use the standard servo library?

...R

i am just confused.

That question probably warranted a new thread, but anyhoo....

In support of what Robin2 just said, the attached pic from my 'scope shows that a servo.attach() or servo.write() from servo.h does indeed repeat the pulse on at 50Hz. You might find this thread of use, Mary_Yvette.

edit.... The op has started a thread on this servo vs softwareservo thing, here.

servo attached.bmp (219 KB)