I'm trying to make a project where two servos rotate at the same time. However, my code doesn't seem to work. The result was that 1st one rotated as I programmed, but the 2nd servo doesn't rotate until the first servo is done rotating. Another word, the 2nd servo waited until the first servo done with the rotation then it starts rotating right after 1st servo is done its job while I want them to work in the same time.
This is my code
#include <Servo.h> // servo library
Servo servo1; // servo control object
Servo servo2;
void setup()
{
servo1.attach(10);
servo2.attach(9);
}
void loop()
{
int pos0,pos360,i;
for(pos0=0,pos360=360,i=0;i<=360;pos0++,pos360--,i++){
//for loop statement that I've tried to spin 2 servos in the same time, one is clockwise, another
//is counter clockwise. I'm making a servo car where 2 servos are the rear wheels. Therefore,
//both of them have to work in the same time, but they work one at the time
servo2.write(pos0);
delay(20);
servo1.write(pos360);
delay(20);
}
}
Please help me which code should I have to run 2 servos in the same time
This is often done with an update every 10ms.
There is no delay needed between the servo.write(). Update the position to every servor motor and have a single delay of 10ms at the end.
for(pos0=0,pos360=360,i=0;i<=360;pos0++,pos360--,i++){
//for loop statement that I've tried to spin 2 servos in the same time, one is clockwise, another
//is counter clockwise. I'm making a servo car where 2 servos are the rear wheels. Therefore,
//both of them have to work in the same time, but they work one at the time
servo2.write(pos0);
servo1.write(pos360);
delay(10);
}
}
Peter_n:
This is often done with an update every 10ms.
There is no delay needed between the servo.write(). Update the position to every servor motor and have a single delay of 10ms at the end.
How do you power the two servo motors ?
How do I update the position with 10 ms at the end? Can you give me an example?
I use 12 volt battery holder to supply 2 servos in the same line. Each servo requires 6 volts and 4.8 load. I think I have no problem with power. Both of them work without twitching. I think it's just the code
//for loop statement that I've tried to spin 2 servos in the same time, one is clockwise, another
//is counter clockwise. I'm making a servo car where 2 servos are the rear wheels. Therefore,
//both of them have to work in the same time, but they work one at the time
These are schematics that I wired up my servos. The first one (picture 1) is the original one I connected. And once you questioned about connecting 2 servos in the same series, I redo it with one of my 6 volts charger (picture 2) but still doesn't work, same result happened, servo waiting for another one to finish.
Neither circuit looks right for normal servos. In the first one both of the servos are powered by a 12V supply and in the second one is powered by a 12V supply,
It is possible to get what are known as "high voltage" servos so can you please provide details of the ones that you are using and a full program that does not work using the
#include <Servo.h> // servo library
Servo servo1; // servo control object
Servo servo2;
void setup()
{
servo1.attach(10);
servo2.attach(9);
}
void loop()
{
servo2.write(0);
servo1.write(180);
delay(20);
servo2.write(180);
servo1.write(0);
delay(20);
//you may have to adjust your delays to match the time it takes the shafts to swing from one end to the other
}
}
Servos sweep by default, you don't need to of do a for loop to make sure it hits every angle. Also it goes from 0 to 180( For the type you show in the picture) not 360
UKHeliBob:
Neither circuit looks right for normal servos. In the first one both of the servos are powered by a 12V supply and in the second one is powered by a 12V supply,
It is possible to get what are known as "high voltage" servos so can you please provide details of the ones that you are using and a full program that does not work using the
for(pos0=0; pos0 <= 180; pos0++){
servo1.write(pos0);
servo2.write(180 - pos0);
type of program.
Do both of the servos work if used individually using the servo Sweep program ?
It works now! The problem was the schematic 1 (picture 1) was incorrect, but schematic 2 is correct. And my code was wrong. Your code works but it has to be