Lesson 07 · 60 minutes
Making Decisions
If, else, and the robot thinking for itself
Today's goal
Write a program that behaves differently depending on what the sensor sees.
Ideas in this lesson
- A condition is a question with a yes/no answer
- 'If' runs a block only when the answer is yes
- Our robot has no 'else', so we ask the opposite question instead
- Now the robot is doing the thinking — this is the autonomy slider moving
Sense → Think → Do
SENSE feeding THINK feeding DO, for the first time, all inside the robot.
How the session runs
- Look at the sensor list in the if-block: line sensors, proximity, wall-closer-than-25cm, and their opposites.
- Build: if wall closer than 25 cm, then back up. Run it while holding a book in front.
- Notice sensors 6, 7 and 8 are the opposites of 5, 4 and 3. That is how we fake an 'else'.
- Build a pair: if wall close, spin left; if path clear, drive forward. Two ifs, opposite questions.
- Wrap the pair in a repeat so it keeps checking. Now it is a behaviour, not a one-shot.
Sample program
Repeat ten times: IF a wall is closer than 25 cm, turn left; IF the way ahead is clear, drive forward. Two opposite questions do the job of if/else.
V 5 P 10 I 5 L 350 Z I 6 F 400 Z Q
Paste it into the Workshop's Robot language box, then press Text → blocks to see it as blocks — or just send it straight to the robot.
Try this
- Make the robot follow a wall on its right without touching it.
- Make it stop dead if BOTH line sensors see black (a finish line).
- The robot only checks the sensor when it reaches the if-block. What does it miss in between? Design an experiment to prove it.
When it goes wrong
| What you see | What it means |
|---|---|
| The robot ignores the sensor completely | The check happens at the instant the if-block runs, not continuously. A long drive before it means a stale answer. |
| Both branches run | That cannot happen if the questions are true opposites. Check you used 5 and 6, not 5 and 5. |
Notes for the grown-up
The last challenge is the deep one: sampling versus continuous awareness. It is the same reason a self-driving car needs to check often, and it is a genuinely hard idea, so leave time.