Sub topic 6
Smooth Servo Sweeps Without `delay()`
Prerequisites and the problem with `delay()`
This chapter assumes that you can upload an Arduino sketch, use setup() and loop(), and connect a servo with a suitable external power supply and a shared ground. A hobby servo is a motor with its own position controller. The Arduino sends it a position signal, but the servo should usually receive power from a separate supply.
A simple sweep often uses delay():
for (int angle = 10; angle <= 170; angle++) {
servo.write(angle);
delay(15);
}This works, but delay() blocks the processor. During the delay, the sketch cannot check a button, read a sensor, update a display, or respond to another part of the project. A sweep can appear smooth while the rest of the project becomes unresponsive.
The alternative is non-blocking timing. Non-blocking code checks the clock and performs a small action only when enough time has passed. The loop() function continues running between those actions.
The `millis()` timing pattern
millis() returns the number of milliseconds since the Arduino started. Store the time when the servo last moved, then compare it with the current time.
| Variable | Purpose |
|---|---|
| `lastStep` | Time at which the previous servo step occurred |
| `STEP_MS` | Minimum time between steps |
| `angle` | Current commanded position |
| `step` | Direction of movement, either `1` or `-1` |
#include <Servo.h>
Servo servo;
const int SERVO_PIN = 9;
const int MIN_ANGLE = 10;
const int MAX_ANGLE = 170;
const unsigned long STEP_MS = 15;
int angle = MIN_ANGLE;
int step = 1;
unsigned long lastStep = 0;
void setup() {
servo.attach(SERVO_PIN);
servo.write(angle);
}
void loop() {
unsigned long now = millis();
if (now - lastStep >= STEP_MS) {
lastStep = now;
angle += step;
if (angle >= MAX_ANGLE) {
angle = MAX_ANGLE;
step = -1;
} else if (angle <= MIN_ANGLE) {
angle = MIN_ANGLE;
step = 1;
}
servo.write(angle);
}
// Other work can run here on every pass through loop().
}The expression now - lastStep is preferred to checking whether now is greater than a calculated deadline. Unsigned subtraction continues to work when the millis() counter eventually rolls over.
Each update changes the requested angle by one degree. With a 15 millisecond interval, the servo receives about 66 updates per second. The actual movement depends on the servo's speed, load, power supply, and internal control circuit.
Choosing limits and timing
Do not assume that every servo safely reaches exactly 0 or 180 degrees. The plastic gears may hit a mechanical stop, causing noise, high current, and heat. Start with conservative limits such as 10 and 170 degrees. Increase them only if the servo moves freely.
A smaller STEP_MS sends updates more often, but it does not guarantee faster movement. A larger angle change per update makes the commanded path less fine. If the servo jitters, check the power wiring and load before changing the timing code.
A common error is using a signed type for a time value or resetting the timer incorrectly. Keep millis() values as unsigned long. Also, avoid putting a long delay() elsewhere in loop(), because that still prevents timely checks.
> callout note > Non-blocking timing does not make the servo physically faster. It keeps the Arduino available for other work while the servo moves.
Exercise
Why does the example clamp angle to MIN_ANGLE or MAX_ANGLE before reversing direction?
Show answer
Clamping prevents the command from stepping beyond the chosen safe limits, which protects the servo from unnecessary contact with its mechanical stops.