PyRasp

15. Capstone — build a system

A capstone is not a bigger version of a single lesson. It is a system: several parts, more than one state, and at least one thing that must happen at the right time. The skill is not typing more code; it is planning the pieces so each one can be tested on its own.

A system in three parts: inputs such as a button, sensor or potentiometer feed the program, which keeps states, times and decisions, and drives outputs such as an OLED, a buzzer or a relay.

The capstone checklist

Before writing code, answer these six questions on paper:

  1. What are the states? Name them: READY, RUNNING, DONE, ALARM. What moves between them?
  2. What is the input? A button, a sensor, a bus device. Is it debounced? Is it pulled?
  3. What is the output? LED, OLED, buzzer, relay, motor. Does it need a driver?
  4. What is timed? A countdown, a blink, a timeout, a minimum on-time. Which clock: sleep or ticks_ms?
  5. What is the happy path? Press → run → finish. Say it in one sentence.
  6. What is the failure path? Button held, sensor unplugged, timer already running, power cut.

If you cannot name the states, you are not ready to write the loop. A state machine on paper is worth an hour of debugging.

Worked capstone 1 — Kitchen timer

What it does: the screen says READY. Press the button; the screen says TIMER and a 3-second countdown starts. At zero, a 1000 Hz buzzer sounds. Press again; the buzzer stops and the screen returns to READY.

States:

The kitchen timer’s states: READY, then a press starts TIMER, then after 3 seconds DONE, and a press returns to READY.

Parts: OLED (GP4/GP5), button GP12, passive buzzer GP13 through 330 Ω, all 3.3 V or 5 V as the problem specifies.

Plan the layers. Build and test in this order; do not start with the whole system:

  1. Screen first. Init the OLED and show READY. You should see it before any button works.
  2. Button edge. Print start once per press, with the 20 ms debounce. No state yet.
  3. State. Add running. The first press starts, the second stops, the screen changes.
  4. Time. When running, compare ticks_diff(now, started) with 3000. At zero, print done.
  5. Sound. Set the PWM duty to half at zero; set it back to 0 on the next press.
  6. Edge cases. Press during the countdown (does it cancel or restart?); hold the button (one action, not many); press immediately at zero (stop cleanly).

The shape of the loop:

running = False
started = 0
held = False

while True:
    pressed = button.value() == 1
    if pressed and not held:
        utime.sleep_ms(20)
        if button.value() == 1:
            held = True
            if running:
                running = False
                buzzer.duty_u16(0)
                label("READY")
                print("stop")
            else:
                running = True
                started = utime.ticks_ms()
                buzzer.duty_u16(0)
                label("TIMER")
                print("start")
    elif not pressed:
        held = False

    if running and utime.ticks_diff(utime.ticks_ms(), started) >= 3000:
        buzzer.duty_u16(32768)

    utime.sleep_ms(5)

label() is the OLED helper from lesson 8: clear the frame, draw the text, set the window, send the buffer.

A design question. The timer is “done” and buzzing, but running is still true. Is that the state you want? A third state (DONE) makes the next feature easier: a snooze button, a different screen, a repeated beep. Name the states you will need tomorrow, not just today.

What the checks look for: silence before the start, half-duty buzzing at 3.7–3.9 s, silence after the second press, the words start and stop, and something on the OLED. Read the check labels; they tell you the exact windows.

Worked capstone 2 — Plant waterer

What it does: a potentiometer stands in for a soil sensor. Below 30 % the pump runs; above 70 % it stops; in between it keeps doing what it was doing.

This is hysteresis plus a relay. The gap is not a bug: wet soil reads lower while the pump is running, and a single threshold would make the pump chatter.

on = False
while True:
    reading = soil.read_u16()
    if reading < 0.3 * 65535:
        on = True
    elif reading > 0.7 * 65535:
        on = False
    relay.value(on)
    utime.sleep_ms(20)

Plan the layers: LED instead of the pump first; then the relay click; then the sensor; then the thresholds. Test the “hold” band explicitly: start dry, move to 50 %, and check that the pump keeps its state.

A real-world note. A real soil sensor drifts and corrodes, and a real pump must not run dry. The simulator's potentiometer is clean; the design lesson is the hold band and a fail-safe (if the reading is impossible, stop the pump).

Worked capstone 3 — Reaction game

What it does: the LED waits one second, then lights. The first of two players to press wins; print player 1 or player 2 and turn the LED off.

The interesting part is the race: two inputs, one winner, no double print.

led.off()
utime.sleep(1)
led.on()
while True:
    if one.value() == 1:
        print("player 1")
        break
    if two.value() == 1:
        print("player 2")
        break
    utime.sleep_ms(5)
led.off()

Extend it: add a false-start rule (a press before the light prints too soon), a random delay, or a best-of-three score. Each extension adds a state; draw it first.

The integration order that works

StepBuildTest
1One output (LED or OLED)It turns on and off on command
2One input (button or sensor)print shows the raw value or the press
3The state variableTransitions happen once per press
4TimingThe event happens in the right window
5The second outputThe buzzer/relay follows the state
6Failure pathsHold, double press, missing sensor, restart

The two-hour rule. If a capstone has been failing for two hours, stop changing code. Draw the state machine again and compare it with the code. The bug is usually a missing state or a transition that runs every loop instead of once.

What “good” looks like

A finished capstone has:

  • a state machine you can explain in five transitions or fewer;
  • one job per function or block — display, input, decision, output;
  • debounced human inputs and a reasonable sleep in every loop;
  • a defined start state and a way back to it (a second press, a reset, a timeout);
  • a check you ran yourself, not just “it looked right”;
  • a short explanation of what was hard and what you would change.

A capstone is done when the problem's checks pass and you can explain it. A program that passes but cannot be explained is not finished; an explanation with no working program is not either.

Other capstones to try

  • Doorbell — a three-note tune on a passive buzzer.
  • Tilt alarm — read the MPU-6050's X/Y registers; light an LED past a threshold.
  • Colour mixer — a potentiometer selects red, green or blue on an RGB LED.
  • Follow the light — an LDR angle maps to a servo position.
  • Traffic light — a timed sequence with a pedestrian request button.
  • Two-floor lift — two buttons, two floor lights, and a servo door.

Pick one you have not already solved. The point is the planning, not the answer.

Next