# Servo Coding Problem

Hey, I want to operate two servos facing each other and their directions are opposite as both goes 0 to 180 degrees. I want to synchronize both like when one goes 0 to 180 degree while the other goes 180 to 0 degree.

``````#include <Servo.h>

Servo servo9;
Servo servo10;

int pos9 = 0;    // variable to store the servo position
int pos10 = 180;

void setup()
{
servo9.attach(9);  // attaches the servo on pin 9 to the servo object
servo10.attach(10); // attaches the servo on pin 10 to the servo object
}

void loop()
{
for(pos9 = 0; pos9 < 180; pos9 += 1)       //servo 9 goes from 0 degrees to 180 degrees
for(pos10 = 180; pos10 < 0; pos10 += 1)    //servo 10 goes from 180 degrees to 0 degrees
{                                         // in steps of 1 degree
servo9.write(pos9);                     // tell servo to go to position in variable 'pos'
delay(15);                              // waits 15ms for the servo to reach the position
servo10.write(pos10);
delay(15);
}

for(pos9 = 180; pos9>=1; pos9-=1)         // goes from 180 degrees to 0 degrees
for(pos10 = 0; pos10>=1; pos10-=1)        // goes from 0 degrees to 180 degrees
{
servo9.write(pos9);                     // tell servo to go to position in variable 'pos'
delay(15);                              // waits 15ms for the servo to reach the position
servo10.write(pos10);
delay(15);
}
}
``````

thanks

Ok never mind , I finally got it myself, I used this simple code, was lot easier.

this worked.

``````#include <Servo.h>

Servo servo9;
Servo servo10; // Define our Servo

void setup()
{
servo9.attach(9);   // servo on digital pin 9
servo10.attach(10); // servo on digital pin 10
}

void loop()
{
servo10.write(0);  // Turn Servo Left to 45 degrees
servo9.write(180);
delay(1000);          // Wait 1 second
servo10.write(180);   // Turn Servo Left to 0 degrees
servo9.write(0);
delay(1000);          // Wait 1 second
}
``````

Below is a typical way to mirror servo positions (n is the servo command position). Sometimes used with bots that use continuous rotation servos for the drive wheels.

``````   servo10.write(n);
servo9.write(180-n);
``````