In this lesson, we will use an ultrasonic sensor to simulate the reversing device of a car.
After creating a new project, click on the Simulation icon on the left side of the page to enter the Simulation page.
Connect the Buzzer module to P0 of the Ezblock, Trig of ultrasonic sensor to D1, Echo to D0. Then connect the VCC and GND to 3.3V and GNDrespectively.
Under the Variables category, click Create variable to create a variable, distance.
Read the value of the ultrasonic sensor, and assign it to the variable distance. Moreover, print it on Debug Monitor by using print block.
Place an if do block under the print block, then click the Settings button in the top left corner, and drag two else if blocks from the if block to create multiple judgment branches.
Drag a Comparison Operator ( = ) block after the if, then change the equals sign to the less than sign; in addition, put an distance block on the left; a number block on the right, and change the value to 30.
Long press the Comparison Operator ( = ) block and choose duplicate to copy 2 more combine blocks and change the number to 80 and 120.
Add three Buzzer blocks into do block, then change the values of beat to 2, 1 and 1/8 respectively, which helps to simulate the reversing scene of a car. And the closer the car move to the obstacle, the rapider the buzzer may sound.
Drag the slider to simulate the read value of sensor. The higher the value, the slower the buzzer sounds, which indicates that the codes are correct. Now save your project, connect the Bluetooth and click Flash to compile and download the code.