Back to engineering archive

Record 074 / Firmware and control

Sequenced multi servo controller

PIR-triggered firmware runs four coordinated outward-and-return sweeps across two servo outputs.

Firmware development
Servo controlSequencingPIR input

Firmware flow

Repeat a coordinated two-servo sweep

Selected runServoSequence implementation.

Each cycle commands both servo outputs during an upward sweep and a downward sweep. A busy-wait follows each step. After four cycles, both outputs are commanded back to zero.

The code blocks during its busy-waits. The diagram describes servo commands and repetition, not measured servo motion or timing accuracy.

Read the implementation

Selected firmware excerpt. The diagram explains the code; it is not a hardware test.

The unchanged excerpt shows the return sweep. The flow follows the complete reviewed runServoSequence implementation at lines 59–93.

Selected return-sweep loop from the four-cycle routine

    for (int pos = 60; pos >= 0; pos--) {
      servo1.write(pos);
      int pos2 = map(pos, 0, 60, 0, 20);
      servo2.write(pos2);
      
      unsigned long tStart = millis();
      while (millis() - tStart < stepDelay) {
        // Non-blocking wait
      }
    }
ActionStep upward through the mapped servo rangesServo 1: 0 to 60; servo 2: mapped 0 to 20.
ActionStep back through the mapped ranges
ResultCommand both servos to zero

Technical notes

A defined movement sequence

runServoSequence() maps the first servo's 0–60 range to the second servo's 0–20 range. It repeats the outward and return sweeps four times, then commands both outputs to zero.

Trigger and cooldown

The main loop starts the sequence when the PIR input is high and the three-second cooldown has elapsed. It switches an LED on during the sequence and records completion before accepting another trigger.

Blocking step timing

Each step uses a busy-wait, so the main loop is blocked while the sequence runs. The displayed return-loop comment calls this non-blocking, but the implementation waits in place. These are commanded angles, not verified physical positions; no UART command task is implemented.

Engineering archiveArchive record

Project enquiries

Discuss your project.

Tell us what you’re developing and where you need engineering support.

Discuss your project