By Kevin McAleer, 5 Minutes
Page last updated November 24, 2026

One sense is easy. Five senses all shouting at once need rules. What should the robot do if it sees a wall and you pick it up at the same moment? This lesson is about giving the robot a sensible order of importance.
A robot doesn’t run five separate programs. It runs one loop that checks the senses in order, and does the first thing that matters. Here is the order Kev’s main.py uses:
flowchart TD
A["Read the senses"] --> B{"Tilted?"}
B -->|"yes"| C["Stop and react"]
B -->|"no"| D{"Blocked?"}
D -->|"yes"| E["Back off and turn away"]
D -->|"no"| F["Drive forward"]
C --> A
E --> A
F --> A
Safety comes first. If the robot has been lifted, nothing it sees on the way matters. Its tracks shouldn’t spin in mid-air at all.
This program runs the same priority order with made-up sensor values, so you can watch the decisions without hardware. Each loop reads two fake sensors and picks one action.
STOP_DISTANCE_CM = 10
TILT_THRESHOLD_G = 0.35
def choose_action(distance, pitch, roll):
# 1. Safety first: picked up or tipped
if abs(pitch) > TILT_THRESHOLD_G or abs(roll) > TILT_THRESHOLD_G:
return "stop and react"
# 2. Obstacle in the way
if distance is not None and distance < STOP_DISTANCE_CM:
return "back off and turn"
# 3. Otherwise carry on
return "forward"
# (distance, pitch, roll) at each moment
timeline = [
(80, 0.0, 0.0), # open floor
(30, 0.0, 0.0), # getting closer
(7, 0.0, 0.0), # a wall
(7, 0.6, 0.0), # picked up AND wall in front
(None, 0.0, 0.0), # put back down, nothing in range
]
for distance, pitch, roll in timeline:
print((distance, pitch, roll), "->", choose_action(distance, pitch, roll))
Look at the fourth row. A wall is in front and the robot has been lifted. Tilt wins, because it comes first in the function.
The loop only works if it comes round quickly. Two senses in this course are slow if you’re careless:
blocking=False in the loop.sleep_ms inside a driving robot means the robot isn’t looking where it’s going for the whole blink.The fix for the blink is to make it non-blocking. Instead of waiting, the loop asks “is it time for the next frame?” and draws one frame only if so. You can write the same idea with ticks_ms() and ticks_diff(). This complete program runs anywhere and prints a blink, a frame at a time, while pretending to do other work:
from time import sleep_ms, ticks_ms, ticks_diff
FRAMES = ["eyes open", "half shut", "shut", "half shut", "eyes open"]
FRAME_MS = 125
blink_frame = None # None means no blink is playing
last_frame_time = 0
loops = 0
start = ticks_ms()
while ticks_diff(ticks_ms(), start) < 1200:
loops += 1 # the "real work": sensing and driving goes here
now = ticks_ms()
# Start a blink after 300 ms
if blink_frame is None and ticks_diff(now, start) > 300 and loops > 1:
blink_frame = 0
last_frame_time = now
print("blink starts:", FRAMES[0])
# Advance the blink only when its next frame is due
if blink_frame is not None and ticks_diff(now, last_frame_time) >= FRAME_MS:
blink_frame += 1
last_frame_time = now
if blink_frame < len(FRAMES):
print(" frame:", FRAMES[blink_frame])
else:
blink_frame = None
print("blink done")
break
sleep_ms(20)
print("The loop ran", loops, "times while the blink played.")
The loop kept running throughout, so sensing never stopped. Kev’s repository does this with start_blink() and tick_blink(), called every time round.
Common mistakes:
choose_action() for a “hold still” case, such as a tilt only in roll.Problem: The robot keeps driving while you hold it. Solution: Check the tilt before anything else in the loop. Why: Order is priority. The first matching rule wins.
Problem: The robot hits things while it blinks or beeps. Solution: Use non-blocking beeps and a tick-based blink. Why: A blocking call stops the loop, so the robot isn’t sensing.
Comments