Make the servos move in opposite directions

I’m trying to make the 2 servos to move in opposite directions. im a beginer in programming, so some one please tell me if this program is right.

#include <Servo.h>

Servo myservo;

Servo myservo2;

int pos = 90;

int pos2 = 90;

void setup() {

myservo.attach(9);

myservo2.attach(10);

}

void loop() {

for (pos = 90; pos <= 150; pos += 1) {

for (pos2 = 90; pos2 >= 30; pos -=1){

myservo.write(pos);

myservo. write(pos2);

delay(15);

}}

for (pos = 150; pos >= 90; pos -= 1) {

for (pos2 = 30; pos2 <=90; pos2 +=1) {

myservo.write(pos);

myservo2.write(pos2);

delay(15);

}}

}

Thank you in advance.

Does it work?

  for (pos = 90; pos <= 150; pos += 1) {
    myservo.write(pos);           
    pos2 = 180 - pos;
    myservo.write(pos2);
    delay(15);
  }

You probably mean to do something like this,
Please post code within </> code tags, and always leave ‘}’ closing curly braces on a line by themselves.

Another way to sweep out and back in opposite directions:

  for (int offset = 0; offset <= 60; offset++) 
  {
    myservo.write(90 + offset);
    myservo.write(90 - offset);
    delay(15);
  }

  for (int offset = 60; offset >= 0; offset--) 
  {
    myservo.write(90 + offset);
    myservo.write(90 - offset);
    delay(15);
  }

The problem is that in one pin, the servos work properly. in the other, the servo moves like the seconds hand of the clock.

Is it still a programming problem ,or is it something else?

You don't say WHICH code isn't working for you. But did you notice that in both of them myservo should sometimes have been myservo2?

Steve

"Is it still a programming problem ,or is it something else?’

Yo might post the code that has issues using the </> code tags in the tool bar. You might also swap the servos to see if it is a servo issue.

slipstick:
You don’t say WHICH code isn’t working for you. But did you notice that in both of them myservo should sometimes have been myservo2?

Steve

Oopsidaisy ! you are so correct !! John did you see that ?

  for (pos = 90; pos <= 150; pos += 1) {
    myservo.write(pos);           
    pos2 = 180 - pos;
    myservo2.write(pos2); // omg that was funny...
    delay(15);
  }

Deva_Rishi:
Oopsidaisy ! you are so correct !! John did you see that ?

Sorry about that. Yes, there should be two servo objects.

  for (int offset = 0; offset <= 60; offset++)
  {
    myservo.write(90 + offset);
    myservo2.write(90 - offset);
    delay(15);
  }

  for (int offset = 60; offset >= 0; offset--)
  {
    myservo.write(90 + offset);
    myservo2.write(90 - offset);
    delay(15);
  }

Could some one please tell the whole program for moving 2 servos in opposite direction…with the #include

I used this program…it still shows me an error :frowning:

#include <Servo.h>
Servo myservo;
Servo myservo2;
void setup() {
// put your setup code here, to run once:
myservo.attach(10);
myservo2.attach(11);
}

void loop() {
// put your main code here, to run repeatedly:
for (int offset = 0; offset <= 60; offset++)
{
myservo.write(90 + offset);
myservo2.write(90 - offset);
delay(15);
}

for (int offset = 60; offset >= 0; offset–)
{
myservo.write(90 + offset);
myservo2.write(90 - offset);
delay(15);
}
}

Or if any corrections are to be made,please tell.

One more thing, what ever i’m doing only pin 9 is working.

blessy_prince: I used this program...it still shows me an error ... One more thing, what ever i'm doing only pin 9 is working.

What error? It compiles and works for me.

What do you mean by only pin 9 works? Your program doesn't have anything on pin 9. The servos should be connected to pins 10 and 11.

Steve

Hi, Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum". OR http://forum.arduino.cc/index.php/topic,148850.0.html. Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom..... :)