2 servo sweep

Hi!

I’m working on a code for a mask that I am making; I need to get the eyelids to blink.

I have 2 mini servos and I used the Sweep code provided through Arduino as the skeleton for my code. The original code is only for one servo. I need to add another servo that does exactly the opposite of the first servo (so when myservo is at 115 degrees, mysecondservo is at 3 degrees and vice versa). Here’s what I have:

//doubleSweep
//edited from the original code by BARRAGAN

#include “Servo.h”

Servo myservo; // create servo object to control a servo
Servo mysecondservo; // a maximum of eight servo objects can be created

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

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

void loop()
{
for(pos = 3; pos < 115; pos += 1) // goes from 3 degrees to 115 degrees in steps of 1 degree
{
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 = 115; pos < 3; pos += 1) // goes from 115 degrees to 0 degrees in steps of 1 degree
{
mysecondservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 115; pos>=3; pos-=1) // goes from 115 degrees to 3 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 = 3; pos>=115; pos-=1) // goes from 3 degrees to 115 degrees
{
mysecondservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

I’ve been trying different combos with no luck. Pin9 is fine but the servo attached to Pin10 just twitches. Please help!

(Also, how would I tell the servos to move as fast as possible from 3 degrees to 115 degrees and vice versa?–So the motion is more like a blink and less like a slow closing and opening of the eyes.)

This is only the second time I’ve used Arduino, so any and all help is much appreciated!

for(pos = 115; pos < 3; pos += 1) // goes from 115 degrees to 0 degrees in steps of 1 degree

When there is a conflict between the code and the comment, the code is ALWAYS right.

This loop starts with pos equal to 115. The loop body is executed while pos is less than 3. At the end of each pass through the loop, the value in pos is incremented by 1.

Since 115 is not less than 3, the body is never executed.

I’m not sure that you want 4 loops, anyway. Presumably you want both servos moving together, not for one to finish moving before the other starts.

You should have only one loop where you write always to both servos. For the second one you might need some complicated formula like:

 for(pos = 3; pos < 115; pos += 1)  // goes from 3 degrees to 115 degrees in steps of 1 degree
 {                                  
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   [glow]mysecondservo.write(115 + 3 - pos);[/glow]
   delay(15);                       // waits 15ms for the servo to reach the position
 }

That should move them more or less at the same time. The delay of 15 is very short as your Servo is updated only every 20ms, but if it give you the desired effect not a problem.

Korman