Multiple continuous servo motor control independently.

Hello, i’ve been trying to write some code for the use of 4 continuous rotation servomotors and i still haven’t figured it out. I’ve written some stuff but they continiously twitch and do strange things and don’t follow the code or they restart de loop before it has ended. Here is one example of the code i’ve done, i would much apreciate if you could give me any tips or tell me what is wrong because i haven’t found any information on the intenet about multiple servos control, so i’m a little bit lost about it. I’m building a robot and i need to be able to control the 4 servos independently because each one does a thing at different times, so the first servos runs while they all stop then servo number three has to turn and so on. But ii haven’t beeen able to figure it out. Here is my best attempt of the code but in the first paragraph of code it stops and starts the loop al over again. The notes are in Spanish because it’s my native language but these notes are irrelevant to understant the code, they are just for my robot.

#include <Servo.h>

Servo S1;
Servo S2;
Servo S3;
Servo S4;

void setup() {

S1.attach(9);
S2.attach(8);
S3.attach(7);
S4.attach(6);

Serial.begin(9600);

S1.write(90);//parat
S2.write(90);//parat
S3.write(90);//parat
S4.write(90);//parat

}

void loop() {

//delay(20000);//10 segons per començar després de conectar

//aixecar(180), baixar(0), avançar(180), retrocedir(0)

S1.write(90);//
S2.write(180);//aixecar
S3.write(90);//
S4.write(90);//

delay(3000);//temps que doni a la fòrmula

S1.write(180);//avançar
S2.write(90);//
S3.write(90);//
S4.write(90);//
delay(41000);

S1.write(90);//
S2.write(0);//baixar
S3.write(90);//
S4.write(90);//
delay(3000);

S1.write(90);//
S2.write(90);//
S3.write(180);//aixecar
S4.write(90);//
delay(3000);

Hi joanro4,
Read through the sticky link at the top of the forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
It tells how to post code using the code tags.

It looks to me like what you posted is incomplete. Can you post it including the entire sketch?

99% of the time, when servos are misbehaving and restarting the sketch, the cause is pulling too much power from the arduino.
Are you powering the servos from the arduino? Don't. :slight_smile:
Servos pull more current than the regulator on the arduino can supply. This is bad.
Much better to provide a separate power supply for the servos. Make sure the grounds are connected.

I usually have a picture I post showing how it can be done but I am on my iPad rather than my PC so have no access.

joanro4:
but they continiously twitch and do strange things and don’t follow the code or they restart de loop before it has ended.

Twitch and do strange things does not convey what actually happens.

The restarting of loop() makes me wonder if you are trying to power the servos through the Arduino (which you should not do) which is causing the Arduino to reset when the voltage drops. Servos (and any motor) should be powered separately from the Arduino and should have a common GND with the Arduino.

If the problem exists even though you do have separate power for the servos it may be useful to make a short video of the problem and upload it to YouTube so we can view it.

It is always a good idea to include Serial.print() statements in your program so you can see which parts are active.

Finally, please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

And, as @vinceherman says, post a complete program

…R

Now I am at work so I can post from my PC.

ServoDiagram.jpg

Your power source can handle the max current of four of them? This you need to check.

Don'tcha love those F**ing diagrams where you have to put labels next to the pictures to explain that you didn't really mean to use that component in the picture.

:slight_smile:
I know. But for showing servos and batteries, this image works.