Stage 7: You're in Control
We code the mBot in mBlock 5. Keep this tab open all week. Open in a new tab — don’t use the buttons in this page to leave the course.
a keyboard controller that drives your mBot live, like a remote-control car
how the robot can react to *you* — keys you press — not just sensors
an mBot you can steer around the room with the arrow keys
Before campers code, show the room:
- Press the up arrow and drive a robot forward; release and it stops. Then steer it around with all four arrows. Say: "The keys are a sensor too — a sensor for what I want."
- Press the space bar to honk the buzzer. Big reaction guaranteed.
- Make the point: "Stage 5 let the robot decide. This stage lets you decide. Real robots do both — sometimes auto, sometimes driver."
The big idea
So far your robot has reacted to sensors — distance, the line. This stage adds a new thing for it to react to: you. A key you press is a kind of input, just like a sensor reading. The robot asks "is the up arrow held down right now?" the same way it asked "is the wall close?"
The trick to smooth control is the same forever loop you already know. Each time around, the robot checks which key is held and drives that way — and if no key is held, it stops. Hold a key, it moves; let go, it stops. That is exactly how a remote-control car feels.
forever:
up held? → forward
down held? → backward
left held? → turn left
right held? → turn right
nothing? → stop
The motors you met in Stage 1, now steered in real time by your keys.
- input
- anything the robot reacts to — a sensor or a key press
- live mode
- blocks run on the computer and drive the robot in real time
- held down
- a key that is being pressed right now
- controller
- the set of keys you use to drive
Make sure you've finished Stage 6: Follow the Line. Keep the robot connected by its cable, and make sure mBlock is in Live mode so your keys reach the robot instantly. Give it floor space.
Build it
Step 1 — Drive forward while a key is held
Start with one key. Hold up to go, release to stop.
Up to go
when green flag clicked forever if <key [up arrow v] pressed?> then mBot move [forward v] at speed (50) % :: motion else mBot stop moving :: motion end end
Run it and hold the up arrow. The robot drives. Let go — it stops. The forever loop checks the key many times a second, so it feels instant.
Step 2 — Build the full controller
Add the other three arrows. Each one is a question; the last else is "no key, so stop."
Four-arrow driver
when green flag clicked forever if <key [up arrow v] pressed?> then mBot move [forward v] at speed (50) % :: motion else if <key [down arrow v] pressed?> then mBot move [backward v] at speed (50) % :: motion else if <key [left arrow v] pressed?> then mBot turn [left v] at speed (40) % :: motion else if <key [right arrow v] pressed?> then mBot turn [right v] at speed (40) % :: motion else mBot stop moving :: motion end end end end end
Now steer your robot around the room. Up, down, left, right — and it stops the moment you let go.
Step 3 — Add a horn and lights
A controller needs a horn. Use the space bar for a beep and a light flash.
Press space to honk
when [space v] key pressed set led [all v] to color [#ffde59] :: looks play tone on note (C5) for (0.3) beats :: sound set led [all v] to color [#000000] :: looks
Drive around and press space whenever you want to honk. Your robot is now a real remote-control car.
Pacing Lab
This lab is required before you move on. The goal is a controller that feels good to drive, tuned by you.
Part A — Driving course (20 minutes)
Set up a short course with tape or cups: drive forward, turn around a cone, come back, and park on a target. Time yourself. On paper:
My drive speed: ____ My turn speed: ____
Too twitchy / too sluggish / just right: ____
My new speeds: drive ____ turn ____
Tune the speeds until the course feels smooth — fast enough to be fun, slow enough to control.
Part B — Pass-the-controller check (10 minutes)
Let a partner drive your robot through your course with no coaching. If they can steer it well on the first try, your speeds are good. If they fight it, adjust and let them try again.
Understand it
The big idea here is that a key press is an input, no different from a sensor. Your robot's whole life is one question repeated: what is true right now, and what should I do about it? Sometimes "what's true" is a distance; sometimes it is a key you are holding. The forever-loop-with-if shape handles both. You did not learn a new kind of program — you pointed the same shape at a new input.
Holding-a-key-to-move (checked in a loop) feels much smoother than a "when key pressed" block that runs once. That is because the loop is constantly re-deciding, so the robot stops the instant you release. Real game controllers and robot remotes work this way: they sample the buttons over and over, many times a second, and act on the latest answer.
Try this
Try this
Three short experiments. Predict before you run, then test your guess.
What happens if you hold up and left at the same time, with this code? Look at the order of the if blocks and predict which one wins before you test.
Try the "hold to move" version, then a version using separate when [up arrow v] key pressed blocks. Which one stops more cleanly when you let go? Now you know why the loop version feels better.
You can drive the robot, and the robot can drive itself (Stage 5). In Stage 10's soccer game, which moments want you in control and which want the robot to react on its own?
Test your stage
- Holding an arrow key drives the robot; releasing it stops the robot.
- All four arrows work: forward, backward, left, right.
- The space bar honks with a beep and a light.
- You drove your robot through a course without losing control.
- Design check. Hand the controller to a friend. Does it feel natural to them, or twitchy? What speed change would help?
If it breaks
- The robot keeps going after I release the key. Your
else ... stopis missing or in the wrong place. The finalelsemust catch "no key held" and stop the motors. - Keys do nothing. Click the green flag first, then click the stage area so mBlock is listening for keys. Make sure you are in Live mode, not Upload mode.
- It only drives forward, never turns. Your
ifblocks may not be nested as else-branches, so only the first one ever runs. Check that each new key check sits in theelseof the one above. - There's a tiny delay. A little lag is normal over the cable. If it is bad, close other programs, and keep the speeds moderate.
This is the most fun stage of the week and the easiest to run — kids get instant feedback and need little help. Use the energy: the driving course and pass-the-controller check turn "zoom around randomly" into real tuning practice.
The recurring bug is the missing or misplaced final else, so the robot won't stop. It is the same nesting skill from Stage 6, now with a clear felt consequence, which makes it a great teaching moment.
Make sure everyone is in Live mode. If keys seem dead, it is almost always Upload mode or the stage not having focus (click it once). Watch for cable tangles as robots zoom — long cables and floor driving fight each other.