Control servos simultaneously

I want to control two servos, 1 and 2, at the same time, after servo 3 have moved.

So far, I can only control one or all servos at the same time doing the same thing.

This is the program I use to control servo b and c.

The problem is, after servo1 moved to 0 (80-i) and servo2 to 80 (0+i), the program restarts ( rough movement of both servos to starting position), then moves servo1 to 0 and servo2 to 80 again.

#include <Servo.h>

Servo servo1;
Servo servo2;


int servoPos;
int servoPos2;

void setup() {
  // put your setup code here, to run once:
servo1.attach(9);

servo2.attach(5);



}

void loop() {
  // put your main code here, to run repeatedly:
for (int i = 0; i < 80; i ++ )
{
servoPos = 80 - i;
servoPos2 = 0 + i ;


servo1.write(servoPos);
servo2.write(servoPos2);
delay(100);
}

for (int i = 80; i > 0; i -- )

{
 servoPos = 0 + i;
 servoPos2 = 80 - i;
  
  servo1.write(servoPos);
  servo2.write(servoPos2);
  delay(100);
}
}

Servo_80-1.ino (533 Bytes)

If the Arduino is actually resetting then it is often a power supply problem. How are the servos connected? If you have them powered from the Arduino 5V pin then don't. They should be independently connected to a battery or power supply NOT powered through the Arduino.

It would be more useful to see the sketch with 3 servos and a description of what happens when that runs.

Steve

slipstick:
If the Arduino is actually resetting then it is often a power supply problem. How are the servos connected? If you have them powered from the Arduino 5V pin then don't. They should be independently connected to a battery or power supply NOT powered through the Arduino.

It would be more useful to see the sketch with 3 servos and a description of what happens when that runs.

Steve

Forgot to use my power supply, sorry.

Anyway,
I use a protoshield in my arduino, which makes more 5v outputs.
Also, I use 12v battery to power it.
Do I still need to power the servos independently? Or is it okay?
Rough movement went to a very fast movement.

Update:
Need help with the code now.
After going to servo1 to 0 and servo2 to 80, the servo moves very fast to go back to the original position (servo1 =80, servo2 = 0)

wanpanchman:
Update:
Need help with the code now.
After going to servo1 to 0 and servo2 to 80, the servo moves very fast to go back to the original position (servo1 =80, servo2 = 0)

Put some Serial.print() statements into your code so you can see what happening. For example put Serial.println("Starting"); in setup() so you can tell if the Arduino is resetting.

And print a message before each servo movement starts.

...R

+1 to @slipstick's focus on the likely power problem.

But I wanted to comment on your code style.
Thanks for using code tags. :smiley:
In the future, hit CTRL-T (some other key if you are using a Mac)
This auto formats the code and makes it easier to read.
Consider removing the dead white space.
There is never a need for multiple blank lines in a row.

If you have a 12V battery powering the Arduino then you definitely shouldn't be running the servos from it. With 12V in the regulator can only handle very limited current without failing. You really need a 12V-5V DC-DC regulator to run the servos. Something like this should work.

Steve

Robin2:
Put some Serial.print() statements into your code so you can see what happening. For example put Serial.println("Starting"); in setup() so you can tell if the Arduino is resetting.

And print a message before each servo movement starts.

...R

Hmmm, thank you very much.

vinceherman:
+1 to @slipstick's focus on the likely power problem.

But I wanted to comment on your code style.
Thanks for using code tags. :smiley:
In the future, hit CTRL-T (some other key if you are using a Mac)
This auto formats the code and makes it easier to read.
Consider removing the dead white space.
There is never a need for multiple blank lines in a row.

Hahahaha, sorry. Just na old habit of mine. Thanks for the tip.

slipstick:
If you have a 12V battery powering the Arduino then you definitely shouldn't be running the servos from it. With 12V in the regulator can only handle very limited current without failing. You really need a 12V-5V DC-DC regulator to run the servos. Something like this should work.

Steve

So I cannot use this?

Edit: Can I use this for 4 servos?