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

Sense → Think → Do

SENSE feeding THINK feeding DO, for the first time, all inside the robot.

How the session runs

  1. Look at the sensor list in the if-block: line sensors, proximity, wall-closer-than-25cm, and their opposites.
  2. Build: if wall closer than 25 cm, then back up. Run it while holding a book in front.
  3. Notice sensors 6, 7 and 8 are the opposites of 5, 4 and 3. That is how we fake an 'else'.
  4. Build a pair: if wall close, spin left; if path clear, drive forward. Two ifs, opposite questions.
  5. 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

When it goes wrong

What you seeWhat it means
The robot ignores the sensor completelyThe check happens at the instant the if-block runs, not continuously. A long drive before it means a stale answer.
Both branches runThat 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.

← Doing It AgainFollowing the Line →