Skip to content
Tactiverge

School/△ Builder/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

1.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.

  • ◇ Write a non-blocking timing pattern so several tasks share one loop.
  • ◇ Calculate how many times a periodic task runs in a time window.
  • ◇ Explain why delay() blocks other tasks and how elapsed-time checks avoid it.

2.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A non-blocking loop runs a task every 250 ms, the first time at t = 0 ms. Counting that first run, how many times has the task run by t = 25 ms (including a run exactly at 25 ms, if one is due)?

runs

4.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.

Open BUILD 001: Design a Washing Machine →

5.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

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.

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

  • To do two things at once you need two processors.
    ✓ One CPU can interleave many short tasks so quickly that they all appear simultaneous.
  • Non-blocking code never runs late.
    ✓ A task can be late by up to one loop pass. Keep passes short and measure them.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A non-blocking loop runs a task every 20 ms, the first time at t = 0 ms. Counting that first run, how many times has the task run by t = 825 ms (including a run exactly at 825 ms, if one is due)?

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?

Practice 5

One loop pass takes at most 3 ms. A task is due at t = 400 ms and the loop checks at 399 ms. At the latest, by how many ms is the task late?

ms

7.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?

Guess first. Being wrong here is how the surprise works.

8.Reflect

Concepts: Super-loops and finite state machines · Real-time computing on robots