3.29 Car Backing Radar
3.29.1 Overview
When a car is in reverse, it will alarm the distance of the obstacles behind the parking space. In this project, we build a mini car backing radar with an ultrasonic sensor for distance detection, a speaker to alarm, and a traffic light module as an indicator.
3.29.2 Test Code
3.29.2.1 Code Flow

3.29.2.2 Build Code
There are two ways to upload the code: directly open the code file we provide; or manually build blocks.
Directly open the code file we provide:
Click
and choose Load from your computer

We have already downloaded the codes on computer desktop, so open the file and choose
3-29-parkingSensor.sb3
Manually build blocks:
Build the two basic blocks:

Declare a variable named distance
Assign the detected value of the ultrasonic sensor to distance

Add a
to determine the distance. If distance > 40, code blocks in it will be executed: the speaker stays quiet, the red LED is off while the green one is ON.
Add another
. If distance < 40, code blocks in it will be executed: the speaker alarms, the red LED is ON while the green one is off.
Complete Test Code

3.29.2.3 Test Result
After uploading code, when the the detected distance value is greater than 40cm, the green LED is on and the amplifier does not emit sound. If the value is less than 40cm, the red LED will light up and the amplifier will alarm.