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
#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,
 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
 }
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.
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);
 }
}
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.
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);
}