Robotics & Arduino
Lesson 2 · Refinement

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.

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?

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.

How do we do it again without writing it again?

WHILE — keep doing this while a condition is true.

What happens if we remove angle++?

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.

Now add the work we repeat.

Does STEP happen every time?

04 — How do we go back and forth?

One option: two for loops.
One counts up. The other counts down.

One loop each way.

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.

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.