Skip to content

Week 5

Making Robots React

Combine sensors with loops and conditions so a robot reacts to the world all by itself.

  • 70-85 minutes
  • Mission: Program a robot that reacts on its own: it loops, checks a sensor, and uses if/else to avoid obstacles or follow a line.

This is the big programming week. Students take the sensors from Week 4 and the sequences from Week 3 and combine them with loops and conditions so a robot can react on its own. They learn that a loop repeats steps, a forever loop repeats until it is stopped, and a repeat-until loop runs until a condition becomes true. They meet conditions and boolean (true/false) decisions, then use if and if/else to choose actions. They program obstacle avoidance - loop, read the distance sensor, and if something is close, turn - and explore line following by checking a light or color sensor to steer and stay on a line.

By the end of this week you can

  • Explain how a loop, a forever loop, and a repeat-until loop each repeat steps
  • Describe a condition as a check that is either true or false (boolean)
  • Use if to run steps only when a condition is true, and if/else to choose between two actions
  • Program obstacle avoidance by looping, reading a distance sensor, and turning when something is close
  • Explain how line following keeps checking a light or color sensor to steer and stay on a line

Learn

Loops: repeat without rewriting

In Week 3 you wrote every step out in a row. That works, but if you want the robot to check its sensor a hundred times, you don't want to write the same step a hundred times. A loop is a block that repeats steps for you.

A plain repeat loop runs its steps a set number of times, like 'repeat 4 times: drive forward, turn right' to make a square. The loop does the counting so your program stays short.

For example: Repeat 4 times to drive a square; Repeat 3 times to knock on a door; Repeat 10 times to inch forward and check

Forever loops and repeat-until loops

A forever loop repeats its steps over and over and never stops on its own - it keeps going until you stop the program. Robots use forever loops to keep watching the world, like 'forever: read the sensor and react'.

A repeat-until loop is different: it repeats its steps until a condition becomes true, then it stops and moves on. 'Repeat until the wall is close: drive forward' means keep driving forward, but the moment the wall is close, stop looping.

For example: Forever: keep checking the distance sensor; Repeat until line is found: drive forward; Repeat until button is pressed: wait

Conditions are true or false (boolean)

A condition is a question about the world that can only be answered yes or no - in programming we say true or false. A value that is only ever true or false is called a boolean.

'Is the wall closer than 10 cm?' is a condition. Right now it might be true; a second later, after the robot moves, it might be false. Conditions are how a robot turns a sensor reading into a clear yes-or-no it can act on.

For example: Is the wall close? -> true or false; Is the sensor over the dark line? -> true or false; Is the button pressed? -> true or false

If and if/else: choosing what to do

An if block runs its steps only when its condition is true. 'If the wall is close, turn right' means the robot turns only when a wall is actually close; otherwise it skips the turn.

An if/else block picks between two actions: it runs one set of steps when the condition is true and a different set when it is false. 'If the wall is close, turn right; else drive forward' means the robot always does one or the other, depending on what it senses.

For example: If line lost, turn to find it; If/else: wall close -> turn, else -> go forward; If item is red, turn to the red bin

Obstacle avoidance = loop + sensor + if/else

Now put it all together. Obstacle avoidance is a forever loop that reads the distance sensor and uses if/else to decide: if something is close, turn away; else keep driving forward. Because the loop repeats many times a second, the robot reacts the instant an obstacle appears.

This is the pattern behind self-parking cars and robot vacuums: sense, decide, act - over and over, forever.

For example: Forever: read distance; if close turn, else forward; A robot vacuum bumping around a room; A rover steering around a rock

Line following: keep checking and steer

Line following uses a light or color sensor pointed at the floor. A dark line reflects less light than a light floor, so the sensor can tell 'on the line' from 'off the line'. The robot keeps checking, again and again, and steers to stay on the line.

One simple rule: if the sensor sees the line, curve one way; else curve back the other way. The robot never drives perfectly straight - it wiggles along the edge of the line, correcting all the time. That constant checking and steering is responsive behavior.

For example: Warehouse robots following floor tape; A factory cart on a painted line; Repeat-until the end mark: keep following the line

Words to know

Loop:
A block that repeats one or more steps instead of writing them out over and over.
Forever loop:
A loop that keeps repeating its steps over and over until you stop the program.
Repeat-until:
A loop that repeats its steps until a condition becomes true, then stops and moves on.
Condition:
A check about the world, like 'is the wall close?', that is always either true or false.
If:
A block that runs its steps only when its condition is true, and skips them when it is false.
If/else:
A block that runs one set of steps when the condition is true and a different set when it is false.
Boolean:
A value that can only be one of two things: true or false. Conditions give a boolean answer.
Obstacle avoidance:
A behavior where the robot loops, reads a distance sensor, and turns away when something is close.
Line following:
A behavior where the robot keeps checking a light or color sensor and steers to stay on a marked line.
Responsive behavior:
When a robot changes what it does based on what it senses, in real time, instead of following a fixed list.

Stay safe

Read these before you build or run a robot this week.

  • Caution:Keep fingers, hair, and loose clothing away from moving wheels while a reacting robot drives, since it changes direction on its own.
  • Caution:Tape lines and obstacle-course pieces down flat and clear the floor so no one trips during runs.
  • Note:Save your block program often so a browser refresh doesn't lose your work.

Do it

Choose your path

Do this course with a robot kit, the browser simulator, or unplugged with household materials. Pick one - you can switch anytime without losing your work.

Obstacle-avoidance program

Robot kit

Program a robot to loop, read a distance sensor, and use if/else to turn when something is close and drive forward when the path is clear.

  • The pattern is always the same: forever loop, read the distance sensor, then if/else - if something is close, turn; else drive forward.
  • Decide your closeness rule first (for example, 'closer than 10 cm counts as close'). Then build the loop so the robot keeps reacting the whole time it runs.

Avoid real obstacles with a kit robot

You need: A robot kit with a distance sensor, Boxes or books for an obstacle course, Block program planning sheet

Steps

  1. 1Set up a few obstacles (boxes or books) with gaps the robot can drive through.
  2. 2Build a forever loop. Inside it, read the distance sensor.
  3. 3Add an if/else: if the distance is less than your closeness value, turn; else drive forward a little.
  4. 4Run it and watch the robot loop, sense, and steer around the obstacles on its own.

What success looks like: The robot drives forward on its own and turns away whenever an obstacle gets close, without a person steering.

Check for:
  • Program uses a forever (or repeat) loop
  • Program reads the distance sensor inside the loop
  • Program uses if/else to turn when close and drive when clear
  • Robot avoids at least one obstacle by itself

Safety: Keep fingers away from wheels while the robot drives. Give the robot a clear floor with no cords or steps.

If it doesn't work

The robot drives straight into obstacles
- Check the if is really reading the sensor, and that your closeness value is larger than the reading when a wall is right in front.
The robot spins in place forever
- Your closeness value may be too big so it always thinks something is close; lower it, or add a short forward move in the else.

Go further: Add a second rule with if/else so the robot turns left sometimes and right other times to escape corners.

Line-following explore

Robot kit

Use a light or color sensor with repeat-until and if to keep a robot steering along a marked line.

  • Line following is a keep-checking behavior: over and over, read the light or color sensor and steer to stay on the line.
  • A simple rule works: if the sensor is on the line, curve one way; else curve the other way. Wrap it in a repeat-until loop that ends when the robot reaches the finish mark.

Follow a taped line with a light or color sensor

You need: A robot kit with a light or color sensor, Dark tape line on a light floor, Block program planning sheet

Steps

  1. 1Lay a dark tape line on a light floor with a clear start and end mark.
  2. 2Point the light or color sensor down at the floor and check its reading on the line versus off the line.
  3. 3Build a repeat-until (until the end mark) loop with an if inside: if the sensor is off the line, steer back toward it; else keep curving along the edge.
  4. 4Run it and watch the robot wiggle along the line and stop at the end.

What success looks like: The robot follows the taped line, correcting side to side as it goes, and stops at the end mark.

Check for:
  • Program reads the light or color sensor in a loop
  • Program uses an if (or if/else) to steer based on the reading
  • Robot stays roughly on the line for most of its length
  • Robot stops at the end mark

Safety: Keep fingers clear of the wheels during runs. Tape the line flat so no one trips.

If it doesn't work

The robot loses the line right away
- Re-check your on-line and off-line sensor readings and set the threshold between them; the two must be clearly different.
The robot spins in circles
- Make the correction turns small; big turns overshoot the line every time.

Go further: Add a curve or a fork to the line and adjust the steering rule so the robot still follows it.

Program the robot

Build a program from blocks, then run it on the simulator. On the kit path, use the same steps in your robot's app; unplugged, act the blocks out on a floor grid. Your program saves on this device.

Obstacle-avoidance program

A forever loop that reads the distance sensor and uses if/else to turn when something is close and drive forward when the path is clear.

Ready

Add a block

Events & execution

Movement

Control

Output

Variables & counters

None yet.

Your program

  • Move
Column 1, row 1Column 2, row 1 — wallColumn 3, row 1Column 1, row 2Column 2, row 2Column 3, row 2Goal zone at column 3, row 1Robot at column 1, row 1
  • Robot (arrow points where it faces)
  • Goal zone (dashed outline)
  • Wall (grey, X mark)

Map: A 3 by 2 grid. The robot starts at column 1, row 1, facing right. The goal zone is at column 3, row 1. There is 1 wall.

Ready.

Ready to run. · 0 steps

Robot state

Column
1
Row
1
Facing
right
Distance ahead
0 cells
Touch
pressed
On a line
no
Light
80
In goal zone
no
Collisions
0
Steps
0

Robot at column 1, row 1, facing right. Distance ahead: 0 cells. Touch: pressed. Over a line: no. Light: 80. Not in the goal zone.

Log output

Nothing logged yet.

  • Heads up: This program has no safe stop behavior. Add a safe stop so the robot stops when it is done or blocked.

Line-following program

A repeat-until loop that reads the light or color sensor each step and uses an if to steer back onto the line, stopping at the end mark.

Ready

Add a block

Events & execution

Movement

Control

Output

Variables & counters

None yet.

Your program

  • Move
Column 1, row 1 — lineColumn 2, row 1 — lineColumn 3, row 1 — lineColumn 4, row 1 — lineColumn 5, row 1 — lineGoal zone at column 5, row 1Robot at column 1, row 1
  • Robot (arrow points where it faces)
  • Goal zone (dashed outline)
  • Line (dashed stripe)

Map: A 5 by 1 grid. The robot starts at column 1, row 1, facing right. The goal zone is at column 5, row 1. A line runs across 5 cells.

Ready.

Ready to run. · 0 steps

Robot state

Column
1
Row
1
Facing
right
Distance ahead
4 cells
Touch
clear
On a line
yes
Light
20
In goal zone
no
Collisions
0
Steps
0

Robot at column 1, row 1, facing right. Distance ahead: 4 cells. Touch: clear. Over a line: yes. Light: 20. Not in the goal zone.

Log output

Nothing logged yet.

  • Heads up: This program has no safe stop behavior. Add a safe stop so the robot stops when it is done or blocked.

Predict

Commit to a guess before you test - then see how close you were. Your predictions save automatically.

How to check: Run the program at a wall three times and compare what the robot actually does to your prediction.

How to check: Remove the sensor read from the loop, run it, and watch whether the robot still reacts to obstacles that appear.

Test & improve

Obstacle-avoidance reliability test

Place the robot in front of an obstacle and run the program. Record whether it turned away in time. Repeat three times from the same start.

Measure: How many of the three tries the robot successfully avoided the obstacle

TryDid it avoid the obstacle? (Y/N)What it did

Knowledge check

Answer these to check that you understand loops, conditions, and how robots react.

  1. 1. Decide whether this statement is true or false.

    A forever loop stops on its own after a few seconds.

  2. 2. What does a loop do in a program?

  3. 3. What is the difference between a forever loop and a repeat-until loop?

  4. 4. A condition like 'is the wall close?' can only be:

  5. 5. What does an if/else block do?

  6. 6. How does obstacle avoidance work?

0 of 6 answered

Reflect

Your reflections save automatically.

Coming up next week

Next week your reacting robot won't always behave - so we learn to debug, and to make it work reliably every time.

  • Save your obstacle-avoidance and line-following programs; we'll break and fix versions of them next week.
  • Notice one time your robot reacted wrong today - that's a bug to bring to Week 6.
  • Charge your kit or bookmark the simulator.

Finish Week 5

Complete these to mark the week done and unlock the next one:

  • Build an obstacle-avoidance program with a loop, a sensor read, and an if/else (not done yet)
  • Explore line following with a light/color sensor and a loop (not done yet)
  • Run three reaction tries and record the results (not done yet)
  • Score at least 4 of 5 on the knowledge check (not done yet)
  • Write your reflection (not done yet)