Hello, I really need some help figuring out how to make this work. I am trying to set my two servos up for a project I am working on so that the first servo goes from 90 to 150 degrees and back, and also a second servo that goes from 90 degrees to 30 and back at the same time. All I get with my code is a twitching of the servos, so any help is greatly appreciated.
Here is my current code:
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
Servo myservotwo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 90; // variable to store the servo position
int postwo = 90; // variable to store the servo position
void setup()
{
myservo.attach(7); // attaches the servo on pin 7 to the servo object
myservotwo.attach(5); // attaches the servo on pin 5 to the servo object
}
void loop()
{
for(pos = 90; pos <= 150; pos += 1) // goes from 90 degrees to 150 degrees
for(postwo = 90; postwo >= 30; postwo -= 1) // goes from 90 degrees to 30 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservotwo.write(postwo); // tell servo 2 to go to position in variable 'postwo'
delay(15); // waits 15ms for the servo to reach the position
}
delay(5000);
for(pos = 150; pos>=90; pos-=1) // goes from 150 degrees to 90 degrees
for(postwo = 30; postwo <= 90; postwo -= 1) // goes from 30 degrees to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservotwo.write(postwo); // tell servo 2 to go to position in variable 'postwo'
delay(15); // waits 15ms for the servo to reach the position
}
}