Making decisions with sensors
Now that you’ve learned how to measure sensor values, you’ll make your robot respond to them in this section. You can make your robot respond by comparing measured values to a threshold value. Then you can trigger an action if your threshold is crossed.
Comparing values to a threshold
The Ultrasonic Sensor measures distances between 40 mm and 2000 mm. It would be impractical to say what the robot should do for each value (40 mm, 41 mm, 42 mm, and so on). It is more practical to say that the robot should do one thing for “big” distances (like driving) and do something else for “small” distances (like stopping).
You determine what is big and small by comparing the measurement to a threshold value that you choose. You do this with the Compare block from the Data category, as shown below.

2. Add a Measure Distance block.
3. Choose less than (<) 500.
In this example, the Compare block turns any distance value (from 40 mm to 2000 mm) into one of the following two values:
- True (if it sees something nearby, closer than 500 mm).
- False (if it sees something farther away or nothing at all).
True and False are precisely the values we need for the Wait Until block, the Conditional Repeat block, and the If-else block. We’ll cover these next.
Challenge #5.2.A: Comparing the other way ⸺ You can change how the Compare block compares the two numbers using the dropdown menu on the block. For example, if you choose equals (==), then it will say True if the distance is exactly 500 mm but False for any other distance. How can you get the same results as in the example above but now without using the less than (<) option? Hint ⸺ You don’t need to add any more blocks, but you may need to move one or more blocks. There are two solutions.
Using the Wait Until block
As its name implies, the Wait Until block waits until something happens. More precisely, it waits until it gets the value True. This value is often called the condition. Combined with what you’ve just learned, you can make your program wait until a sensor measurement crosses a threshold, as follows:

The robot starts driving in forever mode. This is useful because we don’t know the distance in advance (see Chapter 3.3). Then it waits until the Compare block gives True. In other words, it waits until the measured distance is 500 mm or less. Then the robot stops because of the Drive Base Stop block.
Challenge #5.2.B: Don’t move ⸺ Under some conditions, the program above won’t do anything at all. Why and when is that the case? Support your theory by adding a Print block to reveal the value of the Compare block just before and after the Wait Until block.
Challenge #5.2.C: On the threshold ⸺ Change the example to wait until the sensor measures exactly 500 mm and try it out. Discuss ⸺ What will the program do now? Does it always do that in practice, or does it run into the wall more often? Why is that?
You can expand this program to do something more useful, as shown below. Instead of stopping when an obstacle is detected, we’ll back up and turn around. By repeating this forever, the robot drives around the room while avoiding obstacles indefinitely.

Challenge #5.2.D: Left and right turns ⸺ This example always makes the robot turn right after seeing an obstacle. Can you make it alternate between taking left and right turns? Discuss ⸺ Is this better or worse in terms of getting stuck in corners?
Using the Conditional Repeat block
The Conditional Repeat block repeats the blocks you place within it while its condition value is True. For example, you can make it repeatedly play a sound while the sensor sees something up close. Alternatively, you can configure it to repeat until the condition is True. Both cases are illustrated here:

Whether you choose until or while is a matter of preference. You can
achieve the same results with either one by picking the right condition.
For example, repeating while distance < 500
is the same as repeating until distance ≥ 500
.
What does while True mean?
If you don’t pass your own condition value to the Conditional Repeat block, you can see that it has a default value of True. If you choose to repeat while the condition is True, it will repeat over and over. Now you can see why you’ve already used this block in many examples in this guide!

Challenge #5.2.E: Till dead batteries do us part ⸺ Previously, you have
explored using the Wait Forever block to prevent a program from ending when its
last blocks finished running. Can you replicate its behavior with a Conditional
Repeat block? Hint ⸺ For comparison, create a separate program with just
the Wait Forever block directly underneath the Start block and have a peak at
the code by clicking on the </>
icon. Even if you’ve never coded before, many
of the things you see should look familiar based on what you’ve just learned.
Try to achieve the same result with the Conditional Repeat block. You should
now see exactly the same underlying code!
When is the condition checked?
The Conditional Repeat block checks the condition value each time before it runs the blocks inside it. In the sound example above, it checks if the distance is less than 500 mm and plays a beep sound if so. Then it checks the distance again, and so on. Otherwise, it won’t repeat again, and instead your program proceeds with the blocks after it. If the distance was 500 mm or bigger to begin with, the sound doesn’t play at all.
It won’t check the value as it is running the blocks, so it won’t stop the sound halfway through if the sensor value changes. That’s fine for this example because the sound is only short, but it can give unwanted results if the blocks in the loop take a long time to run.
Consider the following program. It makes the robot move straight and turn to drive in a square as in the previous chapters. The Conditional Repeat block keeps doing this while the sensor value is less than 500 mm. Crucially, it only checks this condition each time the blocks in it start, so just before the straights. If you wave your hand in front of the sensor while driving or turning, the robot won’t see it (because it’s not even looking).

If you want it to stop repeating based on a condition that holds only briefly (like waving your hand), you need to make sure you are looking all the time. This can be done with a Multitask block configured to run until one task is done (see Chapter 4.4 if you haven’t tried this block before).
The following example illustrates this. One task keeps the robot driving in a square indefinitely. The other task monitors the sensor all the time and stops waiting once the distance exceeds 500 mm. This task is then complete, so the driving task is stopped as well.

Using the If-else block
Thus far, you have used conditions to decide when to stop waiting or repeating. You can also use such a condition to decide what to do, with the If-else block. It runs one stack of blocks if the condition value is True, or else the other stack of blocks.
Try it out with the following example, which uses the If-else block to start going forward if the distance measurement is bigger than 300 mm or else start going backward. The If-else block is placed inside a Conditional Repeat block to keep making this decision over and over. If you hold a book in front of the robot, it should keep a fixed distance to it and follow you as you move it back and forth.

Challenge #5.2.F: If, else, or both? ⸺ The If-else block and the Multitask block look a bit alike. They both contain two or more stacks of blocks but they work quite differently. Discuss ⸺ Describe the key differences between them and explain when either of them are useful.
If .. then .. else if .. else ..
The previous program makes the robot keep a fixed distance to an object, but you’ll notice that it is a bit restless. It keeps alternating between back and forth around the 300 mm distance point. That’s because we never told it to stop. The motors are always on, one way or another.
It would work better if we made it go forward if the wall is far (> 320 mm), backward if it was close (< 280 mm), and otherwise just stop. The following program implements this strategy. Click the v-symbol on the If-else block to add this extra case.

Challenge #5.2.G: Air guitar ⸺ Create a program that plays different notes for different measured distances. Use the table below to determine the frequencies for each note. For a distance bigger than 300 mm, play C4; else if the distance is bigger than 270 mm, play D4, and so on. Can you play music on your new instrument? Discuss ⸺ Why is the order of the distance comparisons important? Try mixing them up and determine if you can still hit all notes. Why not?
C4 | D4 | E4 | F4 | G4 | A4 | B4 | |
---|---|---|---|---|---|---|---|
Hz | 261.63 | 293.66 | 329.63 | 349.23 | 392.00 | 440.00 | 493.88 |
Putting it all together
The Wait Until block, Conditional Repeat block and If-else blocks all have different use cases. You can use and combine them as needed to achieve your goals. You’ll get more familiar with this as you continue to follow this guide.
To get some practice, create the following program. First it starts driving and waits until it sees something at 500 mm or nearer. Once it sees anything at all, it decides how close the object is. If it is close, it will back up and turn left. Otherwise, it will back up and turn right. This means you can control its movement by waving your hand in front of the sensor up close or further away.

Challenge #5.2.H: Uphill both ways ⸺ In the example above, it reverses in both cases before turning left or right. Could you use just a single block to reverse by placing it before the If-else block? Try this out. Discuss ⸺ You should find that it is not quite the same. It will mostly turn right if you wave your hand in front of the sensor, no matter how close. Why is that?