Connecting two servos to an arduino nano

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
}
}

By "unexpected movements" do you mean the very first servo movement where they jump from their initial position to position 30?

Yes. But it appeared that the servos moved to larger angles then went back to 30.
The servos need to fit in parts with narrow gaps where movements are limited. Therefore, I can't afford to have such large initial movements.

If you know the position you want them to start at is 30 then do a write(30) immediately BEFORE the attach(). That stops them from jumping to the default position (write(90)) and then back.

Steve

And just along the way, a matter of doing things in an orderly fashion:

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can frequently be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

It works. Thanks Steve.

slipstick:
If you know the position you want them to start at is 30 then do a write(30) immediately BEFORE the attach(). That stops them from jumping to the default position (write(90)) and then back.

Steve