Again. And again.
Three ways to move the servo. Build them one step at a time.
One Arduino loop: loop().
Arduino runs the code in loop() over and over, again and again.
When it reaches the end, it starts again from the beginning.
We can write loops inside our code, too.
For now, think of two patterns:
Endless — keep repeating.
Counter — use a counter to control the repetitions.
C++ has three loop forms: while, do…while, and for. These are ways to write a loop; their conditions decide when it stops. We’ll get to that distinction shortly.
01 — The basic hello world.
Use our Servo object. Write the position in angles.
0° → 90° → 180° → 90°
Wait two seconds at each position.
void basicAngleTest() {
// 1. Move to the starting position (0 degrees).
myServo.write(0); // We write in angles.
delay(2000); // Wait 2 seconds.
// 2. Move to the midpoint (90 degrees).
myServo.write(90);
delay(2000);
// 3. Move to the maximum position (180 degrees).
myServo.write(180);
delay(2000);
// 4. Move back to the midpoint (90 degrees).
myServo.write(90);
delay(2000);
}This function runs its four moves once per call. When loop() calls basicAngleTest() on each pass, the sequence repeats. Use endpoint angles your servo can reach without straining.
Why don’t we “go to 0” at the end?
The next call starts at 0°.
loop() calls the function again, so its first move already takes us from 90° back to 0°.
Adding another 0° move and two-second wait at the end would repeat that endpoint and make the pause there four seconds long.
02 — Small steps with while.
We can move to 0°, 90°, and 180°. But we wanted small steps.
Should we write 181 separate commands?
First: replace the fixed angle with a variable.
int angle = 0;
myServo.write(angle);
delay(20);
angle++;
Send the angle, wait, then increase the variable by one. angle++ means angle = angle + 1.
Changing the variable alone does not move the servo. We need to send the next angle, too.
How do we do it again without writing it again?
WHILE — keep doing this while a condition is true.
void whileAngleTest() {
int angle = 0;
while (angle <= 180) {
myServo.write(angle);
delay(20);
angle++;
}
}
Check: Is the angle at most 180?
Do: Send the angle and wait.
Change: Increase the angle.
Repeat: Go back to the check.
After sending 180°, the variable becomes 181. The condition is false, so we leave the while loop and return from the function.
This is one upward sweep. Calling it again starts at 0° again; it does not gradually sweep back down. Arduino waits for this function to finish before continuing the rest of loop().
What happens if we remove angle++?
The variable stays at 0. The condition stays true.
We keep sending 0° and waiting. Now the loop is endless. This function never returns to the rest of Arduino’s loop().
A while loop is not automatically endless. It repeats for as long as its condition stays true.
Next: we have a starting value, a condition, and a counter change. A for loop puts those three things together in one line.
03 — Put the loop controls together.
We already have a loop with a counter, a condition, and a change.
So naturally, we want to package those pieces into one “component.”
That is our counter loop, called a for loop.
Three parts we already know.
STEP — create our counter and give it a starting value. We’ll call it the loop iterator: the variable that tracks our progress. “Iterate” means to repeat a process; each repetition is an iteration.
CHECK — should we do another repetition?
CHANGE — update the counter for the next repetition.
We can remember it as:
for (step; check; change)
Separate the three parts with semicolons.
Put our angle counter into that pattern.
for (int angle = 0; angle <= 180; angle++)
Start at 0. Check whether we’re still within range. After each repetition, add one.
Now add the work we repeat.
void forAngleTest() {
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
delay(20);
}
}
The same movement we wrote with while, with its controls packaged together.
Does STEP happen every time?
No. STEP runs once.
Then we repeat: CHECK → run the body → CHANGE → CHECK again.
When CHECK is false, we leave the loop.
04 — How do we go back and forth?
One option: two for loops.
One counts up. The other counts down.
One loop each way.
void twoWayForTest() {
for (int angle = minAngle; angle <= maxAngle; angle++) {
myServo.write(angle);
delay(20);
}
for (int angle = maxAngle; angle >= minAngle; angle--) {
myServo.write(angle);
delay(20);
}
}
One call travels up and back down. Call it on each pass of Arduino’s loop() to repeat.
Or: keep one range and use a flag.
The range stays set: minAngle to maxAngle. We already declared those limits as 0 and 180.
While we are inside that range, keep moving. When we reach an end, change direction.
A boolean flag remembers which way we are going:
movingForward = true → increase the angle with ++.
movingForward = false → decrease the angle with --.
The range check and direction flag.
void backAndForthTest() {
int angle = minAngle;
bool movingForward = true;
while (angle >= minAngle && angle <= maxAngle) {
myServo.write(angle);
delay(20);
// At an endpoint, change direction BEFORE taking the next step.
if (angle >= maxAngle) {
movingForward = false;
} else if (angle <= minAngle) {
movingForward = true;
}
// Use the flag to choose the next step.
if (movingForward) {
angle++;
} else {
angle--;
}
}
}The range stays fixed. The direction changes.
angle >= minAngle means we have not gone below the lower limit.
angle <= maxAngle means we have not gone above the upper limit.
&& means AND: both checks must be true. The endpoints are included.
The range check decides whether to repeat. The flag decides which way to step.
At 180°, set the flag to false and step down to 179°.
At 0°, set it to true and step up to 1°.
… 178 → 179 → 180 → 179 → 178 …
… 2 → 1 → 0 → 1 → 2 …
We reverse before stepping outside the range. We never need to change the limits.
With minAngle below maxAngle and one-degree steps, this while loop keeps running: the angle stays in range. Unlike the two-for version, this function does not return to the rest of Arduino’s loop(). The flag changes our commanded angle; the servo’s internal controller handles the motor direction.