Skip to content

Name: ______________________________

Date: ____________________

Week 5

Making Robots React

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

70-85 minutes

Key ideas

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.

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'.

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.

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.

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.

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.

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.

Your activity

Obstacle-avoidance program

Goal: 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.

  1. The pattern is always the same: forever loop, read the distance sensor, then if/else - if something is close, turn; else drive forward.
  2. 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.

Write what happened:

Line-following explore

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

  1. Line following is a keep-checking behavior: over and over, read the light or color sensor and steer to stay on the line.
  2. 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.

Write what happened:

Predict

Write your guess before you test.

Before you run the obstacle program: when the robot meets a wall, what do you predict it will do, and why?

Predict: if you take the read-sensor out of the loop, how will the robot's reactions change?

Test and record

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.

Record: 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 each question. For choice questions, circle the best answer.

  1. Decide whether this statement is true or false.

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

    Circle one:   True   /   False

  2. What does a loop do in a program?

    • A.It makes the robot move faster
    • B.It repeats one or more steps instead of writing them out again and again
    • C.It stops the robot
    • D.It adds a new sensor
  3. What is the difference between a forever loop and a repeat-until loop?

    • A.A forever loop runs once; a repeat-until loop runs twice
    • B.A forever loop repeats until you stop the program; a repeat-until loop repeats until a condition becomes true
    • C.They are exactly the same
    • D.A forever loop needs a sensor and a repeat-until loop does not
  4. A condition like 'is the wall close?' can only be:

    • A.A number in centimeters
    • B.True or false (a boolean)
    • C.A color
    • D.A motor speed
  5. What does an if/else block do?

    • A.It runs one set of steps when the condition is true and a different set when it is false
    • B.It repeats a step ten times
    • C.It always runs both sets of steps
    • D.It reads a sensor
  6. How does obstacle avoidance work?

    • A.The robot drives a fixed path it was told once
    • B.A loop reads the distance sensor and, if something is close, the robot turns; else it drives forward
    • C.The robot waits for a person to steer it around each obstacle
    • D.The robot turns off when it sees an obstacle

Reflect

What decision does your robot make repeatedly, and what information does it use?

Why does a reacting robot need a loop around its sensor check instead of checking just once?

Describe one real robot that reacts to the world, and name the condition it checks.