Trying to figure out this chronic problem every time I want to run more than 1 servo on a program.
If I use the sample servo sweep program with only the ONE servo, it sweeps smoothly in both directions at the same speed as it should.
However, if I try to simply copy and paste the program and add a second servo, both servos either rotate slowly in one direction and snaps back to its starting point repeatedly or snaps to the end point and rotates smoothly back to its starting point.
Ive double checked everything but this has been a constant issue. Seems like they're both interfering with each other.
Maybe digital vs analogue servos are the problem?
This is because a simple copy and paste will use the same variables. Each servo needs its own name both for the attach command and the servo write command.
hi. Sorry about that. Here it is but its an exact copy of the example code for the one servo sweep but with 2 servos.
Thanks so much.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2; // 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() {
myservo1.attach(8); // attaches the servo on pin 9 to the servo object
myservo2.attach(9); // attaches the servo on pin 10 to the servo object
}
void loop() {
for (pos = 90; pos <= 0; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.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 <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo2.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 >= 90; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo2.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Hi. No. I'm running the 2 servos through a 6 volt AA battery pack. Ive had the same problem even when using an adafruit motor controller so voltage or current doesn't seem to be the issue
Can you please edit post #6, select all code and click the </> button to apply code tags and next save your post. It makes it easier to read, easier to copy and prevents the forum software from incorrect interpretation of the code.