TactivergeLite

Lite / Building Machines That Respond / Week 19

Set up once, loop forever

Your laptop's programs start and stop. A microcontroller's program never ends. How do you write a program that runs forever, on purpose?

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

Why should I care?

Nearly every small machine you will program has the same skeleton: prepare the hardware once, then go round a loop that reads sensors, decides and acts, forever. Learn this pattern and every later idea in this term (state machines, timers, feedback) slots straight into it.

Predict

A loop reads a button, then waits 500 ms, then repeats. You tap the button for 0.1 s. What happens?

  1. It is always noticed
  2. It might be missed, because the loop spends most of its time waiting
  3. The chip crashes
Guess first, then reveal

The loop only looks at the button once every half second. A 0.1 s tap that falls inside the wait is never seen. Responsive firmware avoids long waits; week 25 shows how.

Discover

Choose 'Blocking: delay(500)'. Scrub the time from 0 to 2,000 ms and watch when each job in the loop runs. Did the loop notice the two button presses? Now switch to the non-blocking loop and compare.

What the interactive shows (text version)

The timing scrubber shows three jobs (toggle an LED, read a sensor, read a button) on a 0–2,000 ms timeline. With a blocking delay of 500 ms, every job runs only once every 500 ms, and two short button presses (70 ms and 50 ms long) fall between reads and are missed. With a non-blocking loop, the button is read every 20 ms and both presses are seen.

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

Understand

Two parts: setup and loop

Embedded programs have a setup part that runs once at power-on (set pin modes, start the ADC, set variables) and a loop that runs forever. In Python-style code:

setup_pins()
while True:
    temp = read_temperature()
    if temp < 18:
        heater_on()
    else:
        heater_off()

There is no 'end of program': if the loop ever stopped, the machine would stop responding.

Read, decide, act

Each pass of the loop is one turn of sense–think–act: read the inputs, decide with ifs and comparisons, then set the outputs. Keep each pass short. A greenhouse loop that takes 5 ms reacts within 5 ms; one that sleeps for a minute is blind for a minute.

Why delays hurt

A delay(500) freezes the CPU for half a second. Nothing else runs: no buttons, no sensors, no safety checks. One delay is fine for blinking a single LED, but a machine doing several jobs needs a loop that never waits. You will build that in week 25 with timers.

Explain it another way

Intuitive

A lighthouse keeper's routine: wake up once and prepare the lamp, then forever: look at the sea, decide, act, and look again.

Technical

This is the 'super-loop' architecture: setup() once, then loop() called endlessly (as in Arduino), or while True: in MicroPython. Interrupts can break in for urgent events, and an RTOS can schedule several loops, but small machines often need only one well-written loop.

Applied

A microwave's firmware loops thousands of times a second: read the keypad, check the door switch, update the countdown, set the magnetron and the display. Opening the door is noticed within a few milliseconds because the loop never waits long.

Worked example 1

Write pseudocode for a night light: an LED should be on when a light sensor reads below 300 counts.

set_pin_output(LED)
while True:
    light = read_adc(LIGHT_SENSOR)
    if light < 300:
        write(LED, HIGH)
    else:
        write(LED, LOW)

Setup once; then read, decide, act, every pass.

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.

Practice 1. Where should 'set pin 3 as an output' go?
Practice 2. In the heater loop (heater on if temp < 18, otherwise off), the readings on three passes are 17.5, 18.0 and 18.4. What does the heater do on each pass?

passes

Practice 4. A learner writes the heater program without `while True:`. The heater works once at power-on, then never changes. Why?

instructions

Apply it

Write the setup and loop for a fridge-door alarm in Python-style pseudocode: a door switch on one pin, a buzzer on another. The buzzer sounds if the door is open. Then add a second sensor (a thermometer) without adding any delay, and trace your loop for three passes with made-up readings.

Reflect

Name a machine that would be dangerous if its loop stopped for one second. What would go wrong?

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

← Counting in nanosecondsPins: high, low and in between →