NEWBIE ALERT!! trying to reverse the servo!

A stupid question im sure!
I have 2 servos, the second servo I want to move anti clockwise first and then clockwise. I've tried everything, but not knowing what I'm doing does cause problems :smiley:

#include <Servo.h>

Servo servo1;// moves eyes
Servo servo2;// moves eyelids

int pos1;      // angle of servo1
int pos2;      // angle of servo2

void setup()
{
servo1.attach(4);  // attaches the servo on pin 9 to the servo object
servo2.attach(5);  // attaches the servo on pin 10 to the servo object
} 

void loop() {
 for (pos1 = 30; pos1 <= 150; pos1 += 1) { // goes from 30 degrees to 150 degrees
   // in steps of 1 degree
   servo1.write(pos1);              // tell servo to go to position in variable 'pos1'
   delay(10);                       // waits 10ms for the servo to reach the position
 }
 for (pos1 = 150; pos1 >= 90; pos1 -= 1) { // goes from 150 degrees to 90 degrees
   servo1.write(pos1);              // tell servo to go to position in variable 'pos1'
   delay(10);                       // waits 10ms for the servo to reach the position
}
delay(5000);

 for (pos2 = 100; pos2 >= 30; pos2 -= 1) { // goes from 100 degrees to 30 degrees
   // in steps of 1 degree
   servo2.write(pos2);              // tell servo to go to position in variable 'pos2'
   delay(10);                       // waits 10ms for the servo to reach the position
 }
 for (pos2 = 30; pos2 <= 100; pos2 += 1) { // goes from 30 degrees to 100 degrees
   servo2.write(pos2);              // tell servo to go to position in variable 'pos2'
   delay(10);                       // waits 10ms for the servo to reach the position
 }
}

servo2 just goes clockwise then anti clockwise even though I change the angles and the > signs etc.

Please help!!
Any help would be gratefully received,

Jon

This part moves servo2 one way:

 for (pos2 = 100; pos2 >= 30; pos2 -= 1) { // goes from 100 degrees to 30 degrees
   // in steps of 1 degree
   servo2.write(pos2);              // tell servo to go to position in variable 'pos2'
   delay(10);                       // waits 10ms for the servo to reach the position
 }

This part moves servo2 the other way:

 for (pos2 = 30; pos2 <= 100; pos2 += 1) { // goes from 30 degrees to 100 degrees
   servo2.write(pos2);              // tell servo to go to position in variable 'pos2'
   delay(10);                       // waits 10ms for the servo to reach the position
 }

Why not just swap the two parts?

I've tried everything and it still goes clockwise, no matter whether I swap them or not!
am I being stupid or missed something?

If you want to move one servo in the opposite direction to the other this maths might help

servo1Pos = 125; // just an angle for example
servo2Pos = 180 - servo1Pos;

...R

OK this is amazing thanks, but....... where do I put that bit of code please? sorry!!!

slugbut:
OK this is amazing thanks, but....... where do I put that bit of code please? sorry!!!

In your program (OK, it's bedtime)

If you can't figure it out then make a guess and post the program that includes your guess.

...R

Thanks,

Servo 2 doesnt need to reverse in direction relative to servo 1,

I just want servo 2 to move anticlockwise and then clockwise

I've even used the sweep example and swapped the two movements around, but the servo still moves clockwise first.

Am I missing something really simple?

Thanks,

Jon

Skip the other servo that works ok for a while, both in your connections and in your code. Minimize the whole setup while trying to discover why it works in an unwanted way.

Thanks again everyone.

I've used the standard sweep example, with one servo connected to a nano.

I've swapped the program around to enable the sweep to go from 180 to 0 but it still goes clockwise first!

What am I doing wrong?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

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

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

Thanks fo ryour help in advance,

Jon

slugbut:
I've used the standard sweep example, with one servo connected to a nano. I've swapped the program around to enable the sweep to go from 180 to 0 but it still goes clockwise first!

When you 'attach' the servo it will move to 90° unless you previously .write() a position. Could that be what you are seeing?
Since the sweep is continuously moving, how do you tell which way it is moving 'first'? Put a delay at the top of loop() so you can tell when a new cycle starts.
Do you have Arduino Ground connected to the Ground side of the servo power supply? DON'T try to power a servo off the +5V pin. They can draw more current than the Arduino can supply and that can cause your Arduino to reset.

I'm getting there!

Servo1 now goes in the right direction.
Just about to add the second servo. Any suggestions? Thanks again for your help

#include <Servo.h>

Servo servo1;  // create servo object to control a servo
Servo servo2; // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int pos2 = 0;   // variable to store the servo position

void setup() {
 servo1.attach(5);  // attaches the servo on pin 5 to the servo object
 servo2.attach(4);  // attaches the servo on pin 4 to the servo object
 servo1.write(90);  // set servo to mid-point
 servo2.write(90);  // set servo to mid-point
  
}

void loop() {
  delay(1000);
  for (pos = 50; pos <= 150; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 150; pos >= 50; pos -= 1) { // goes from 180 degrees to 0 degrees
    servo1.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15); 
   // waits 15ms for the servo to reach the position
  }
  delay(5000);
}