Robot Logic in Scratch
Senior 3 · ICT
Robot Logic in Scratch
Lesson 5 of 5
60 min
A robot vacuum cleaner carries bumpers to feel furniture, sensors underneath to notice the top of a staircase, sensors in its wheels to count how far it has gone, and often more. No single sensor could keep it safe: the bumper cannot see a staircase, and the stair sensor cannot feel a chair. The robot's program has to combine all of them, decide which matters most, and sometimes merge several readings into one better estimate. This lesson does both: combined conditions with if, if-else and nested if, and sensor fusion.
Definition
Compound condition: two or more conditions joined with and, or or not, such as “left beam sees a wall AND right beam sees a wall”.
Nested if: an if block placed inside another if (or if-else) block, so a second question is asked only after the first has been answered.
Priority: the order in which conditions are checked; the most urgent goes first.
Sensor fusion: combining the readings of several sensors to get information that is more accurate, more reliable or more complete than any one sensor gives alone.
| Block | Meaning | Robot example |
|---|---|---|
| if … then | Do something only when the condition is true; otherwise do nothing extra. | If the loudness is over 50, say “Heard you!”. |
| if … then … else | Choose between exactly two actions. | If the beam sees a wall, turn; else move forward. |
| nested if | Ask further questions inside one branch, giving three or more outcomes. | If the bumper is pressed, reverse; else, if both beams see walls, turn round; else … |
The green and, or and not blocks in Operators combine true-or-false conditions. Their results:
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| false | false | false | false | true |
| false | true | false | true | true |
| true | false | false | true | false |
| true | true | true | true | false |
and is true only when both parts are true; or is true when at least one part is true; not flips true and false.
Give the robot costume two short beams, a cyan one angled to the front-left and a pink one to the front-right, and keep the bumper (touching the Wall sprite). The script stores each beam's reading in a variable, then decides in order of priority:
Block diagram drawn by Ganzaa. In Scratch, a true-or-false block can be dropped into the set block; the variable then holds the word true or false.
| Bumper | Left beam | Right beam | Branch | Action |
|---|---|---|---|---|
| pressed | any | any | ① | Reverse 10 steps, turn round |
| not pressed | wall | wall | ② | Turn round: dead end |
| not pressed | wall | clear | ③ | Turn right 15°, away from the left wall |
| not pressed | clear | wall | ④ | Turn left 15° |
| not pressed | clear | clear | ⑤ | Move forward 3 steps |
The bumper is checked first because touching means the other sensors have already failed: it is the most urgent. Two sensors give 4 combinations; with the bumper, the nested ifs sort all 8 possible states into 5 actions.
Worked example 1: tracing the decisions
| Moment | Bumper | left | right | Tests in order | Action |
|---|---|---|---|---|---|
| A | no | false | false | ① no; ② no; ③ no; ④ no | ⑤ move forward |
| B | no | true | false | ① no; ② no (right is false); ③ yes | turn right 15° |
| C | no | true | true | ① no; ② yes | turn round |
| D | yes | true | true | ① yes: nothing else is checked | reverse and turn round |
Notice moment D: both beams also see walls, but the bumper wins because it is tested first. If ② were tested before ①, the robot would turn on the spot while pressed against the wall, scraping it. Order is part of the logic.
Every sensor has weaknesses. An ultrasonic sensor can miss soft curtains, which absorb sound, or a wall met at a steep angle, which reflects the echo away. An infrared distance sensor can be confused by bright sunlight or very dark surfaces. A camera sees little in the dark. Fusing sensors whose weaknesses differ gives a better picture.
| Method | How it works | Example |
|---|---|---|
| Average | Add the readings and divide by how many there are | Two distance sensors pointing the same way |
| Weighted average | Trust the more accurate sensor more | 0.75 × ultrasonic + 0.25 × infrared |
| Voting or median | With three or more sensors, take the middle value, so one faulty sensor cannot spoil the result | Three thermometers in a greenhouse |
| Agreement (and) | Act only when two different sensors agree | Fire alarm: heat and smoke |
| Coverage (or) | Act when any sensor detects something | Stop if the bumper or the stair sensor triggers |
Fusion is everywhere: a phone combines its accelerometer, gyroscope and compass to know which way it points; self-driving cars combine cameras, radar and lidar.
Block diagrams drawn by Ganzaa. In a simulation, sonar, infrared, temperature and smoke are slider variables.
Worked example 2: fusing readings
Weighted average. The ultrasonic sensor reads 52 cm and the infrared sensor 60 cm. The ultrasonic one is known to be more accurate, so it gets weight 0.75 and the infrared 0.25 (the weights add up to 1):
Median. Three thermometers read 31 °C, 32 °C and 75 °C; the third has a fault. The mean is °C, which would wrongly switch on every fan. The median (the middle value when sorted) is 32 °C, which is right. With three sensors, the median ignores one bad reading.
Agreement. A kitchen gets hot when cooking (55 °C) but there is no smoke (10): temperature > 50 and smoke > 30 is false, so no false alarm. With or, the alarm would ring at every meal.
Warning
Common logic mistakes
MATLAB: Understanding Sensor Fusion and Tracking, Part 1: What Is Sensor Fusion? An engineer explains why autonomous systems combine sensors, with clear examples. Watch at least the first half.
Practice
Further reading
Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.