Course 7 / Lecture 4:

# Surround sensing using linear distance sensor

In this lecture you will learn a smarter way to avoid obstacles. You will learn to use the ultrasonic distance sensor to slow the robot down once an obstacle gets near, and to stop the robot once it is too close. You will also learn an algorithm that can be used to go around the detected obstacles without actually hitting them.

Figure 1 - Avoiding obstacles with linear distance sensors

## Instruction video

Lecture video - How you can control your robot using Ultrasonic sensor

## Write code to move your robot forward!

You have to use the 'when green flag clicked' entry point, choose it from the Control menu. That means the program starts when you press the green flag in the top right corner of SNAP. Add the 'Drive [motors] [direction] at [speed] %' block from the Lego motors menu, where motors should be the A+D motors, select the forward direction and set the robot's motor speed to 25%. (Figure 2)

Figure 2 - Start your code by turning on both robot motors

## Go forward your robot until to close to an obstacle!

You have to drag 'wait until' block from Control menu to examine the Ultrasonic Sensor's value. Add the '<' block from the Operators menu, choose the 'Distance in cm' block from Lego category to left side and write 20 to right side. Don't forget select the Ultrasonic Sensor, because it sends an information about measurements. The robot will go forward with 25% speed until the distance is greater than 20 cm between the robot and an object, what infront of the robot. (Figure 3)

Figure 3 - Detects if an object is closer than 20 cm

## Slow down the robot!

If the distance is less than 20cm, the robot should reduce the motor speed to 10%. Drag the 'Drive [motors] [direction] at [speed] %' block from the Lego motors menu, where motors are A+D motors, choose the forward option to direction and please set the motor speed to 10%. The robot approaches the obstacle slower as you can see it on Figure 4 below.

Figure 4 - Makes the robot go slower than before

## Upgrade code to go forward slowly until the distance is less than 10cm!

You add the 'wait until' block to check the distance again. Please, drag the '<' block, where set the 'Distance in cm' block to left side and select the Ultrasonic Sensor. Write '10' to right side as you can see it on Figure 5 below. The robot should go forward slowly until the distance between the robot and the object is greater than 10 cm but it less than 20 cm.

Figure 5 - Examine the Ultrasonic Sensor's value if it is less than 10cm

## Turn right the robot when it can not go forward due to an obstacle!

If the distance is less than 10 cm, the robot should turn right to avoid the object and go forward again. Drag the 'Drive [motors] [direction] [time] sec at [speed]%' block from the Lego motors menu, where choose A+D option to motors, select the right direction, set the 0.5 sec and set the motor speed to 25%. This demonstrate in Figure 6 below.

Figure 6 - Turn your robot right just before the obstacle

## Add the programcode to the loop and test it! The robot goes straight forward if the road is free!

Now, drag the whole program to the 'Forever' block. Select it from the Control menu and put it after the 'when green flag clicked' block. The robot goes forward slowly when the distance is less than 20cm between the robot and the object. When the distance is already less than 10cm, the robot should turn right. Finally, go forward constatly to the next obstacle. (Figure 7)

## Program code

Figure 7 - The program code slows robot before obstacle then makes a right turn