TactivergeLite

Lite / Building Machines That Respond / Week 25

Doing several things at once (without waiting)

Blink an LED every half second, read a sensor ten times a second and never miss a button press. With one CPU and no waiting. How?

About 35 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Real machines juggle: the washing machine watches the door, turns the drum and counts down the wash at the same time. The non-blocking pattern is the simple trick that lets one small CPU do all of it, and it is the backbone of your term project.

Predict

A task should run every 100 ms, first at t = 0. How many times has it run by t = 1,000 ms, counting a run exactly at 1,000 ms?

  1. 10
  2. 11
  3. 100
Guess first, then reveal

Runs at 0, 100, 200, …, 1,000 ms: that is 10 periods plus the first run = **11**. Off-by-one errors like this are why engineers list a few run times by hand.

Discover

Compare the two loop styles across the full 2,000 ms. In the non-blocking loop, count the sensor reads by 1,000 ms and check the two button presses are noticed. Then explain, in one sentence, why the blocking loop misses them.

What the interactive shows (text version)

The timing scrubber compares two loops over 2,000 ms. The blocking loop (toggle LED, read sensor, read button, delay 500 ms) runs every job only every 500 ms and misses two short button presses at 230–300 ms and 1,240–1,290 ms. The non-blocking loop runs the LED every 500 ms, the sensor every 100 ms and the button every 20 ms, so by 1,000 ms it has read the sensor 11 times and notices both presses.

You can also open the interactive on the full site when you have a faster connection.

Understand

Check the clock, don't wait for it

Instead of delay(500), each task remembers when it last ran. Every pass of the loop asks 'has enough time passed?' and only then does the work:

last_blink = 0
while True:
    now = millis()
    if now - last_blink >= 500:
        last_blink += 500
        toggle_led()
    if now - last_read >= 100:
        last_read += 100
        read_sensor()
    check_button()   # every pass

Why subtraction?

now - last >= period measures elapsed time, which keeps working even when the millisecond counter wraps around past its largest value (with unsigned integer maths in C). Adding the period to last (rather than setting last = now) stops small delays piling up, so a 500 ms blink stays at 500 ms on average.

Tasks are tiny state machines

Each task's 'last run time' is its memory, like a state. Longer jobs (a wash stage, a door opening) become state machines whose transitions fire on elapsed time or sensor events. Nothing ever waits, so every task gets a turn within a millisecond or two.

Explain it another way

Intuitive

A chef with three pans does not stare at one pan for ten minutes. They glance at each pan's timer in turn and act only on the pan that is ready.

Technical

This is cooperative multitasking in a super-loop. It requires every task to return quickly. Jitter (lateness) equals at most one loop pass. For hard deadlines, timers and interrupts take over; for many tasks, an RTOS schedules them.

Mathematical

Runs of a task with period p in [0, T], starting at 0: ⌊T/p⌋ + 1. Worst-case lateness ≈ the longest loop pass.

Applied

A washing machine's firmware checks the door switch every pass, updates the motor PWM every millisecond, and ends each wash stage on elapsed minutes or a water-level reading, all without one delay.

Worked example 1

Tasks every 20 ms, 100 ms and 500 ms run from t = 0. How many total task runs by t = 1,000 ms?

20 ms: ⌊1000/20⌋ + 1 = 51. 100 ms: 11. 500 ms: 3. Total 65 runs.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

runs

runs

Practice 3. A loop calls `delay(1000)` once per pass. What is the main problem when you add a safety button?
Practice 4. Why write `last += 500` instead of `last = now` when a task runs?

ms

Apply it

Do Level 2 of the Washing Machine build to choose how each stage ends. Then write the non-blocking main loop for its controller in Python-style pseudocode: check the door every pass, update the drum PWM every 1 ms, and end each stage on elapsed time or the sensor condition you chose.

Reflect

Which of your future project's tasks must run every pass, and which can wait their turn?

Write your answer on paper or in your own notes. Lite never stores what you write.

← PWM: dimming with an on/off switchOpen loop or closed loop? →