I am a beginner and trying to connecting two servos to an arduino nano. The servos roughly did what I expected except the servos had unexpected movements when the power was on. After the irregular movements, the servos moved exactly what I intended.
Below are my ardunio sketch, can anyone give me some advice on what's wrong? Thanks a lot.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo
Servo myservo2;
// two servo objects created
int ledpin3 = 3;
void setup() {
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo2.attach(10);
pinMode(ledpin3, OUTPUT);
}
void loop() {
digitalWrite(ledpin3, HIGH);
for (int pos = 30; pos <= 90; pos += 1) { // goes from 30 degrees to 90 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 (int pos = 30; pos <= 59; pos += 1) { // goes from 30 degrees to 60 degrees
// in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos = 60; pos >= 30; pos -= 1) { // goes from 60 degrees to 30 degrees
// in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}
delay(1000);
for (int pos = 91; pos <= 150; pos += 1) { // goes from 91 degrees to 150 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
}
delay(1000);
for (int pos = 151; pos >= 91; pos -= 1) { // goes from 151 degrees to 91degrees
// 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 (int pos = 30; pos <= 59; pos += 1) { // goes from 30 degrees to 60 degrees
// in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (int pos = 60; pos >= 30; pos -= 1) { // goes from 60 degrees to 30 degrees
// in steps of 1 degree
myservo2.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}
delay(1000);
for (int pos = 90; pos >= 31; pos -= 1) { // goes from 90 degrees to 30 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
}
}