7. case 04 Avoid Barrier#

7.1. Our Goal#

Make our motor:bit car avoid barriers using ultrasonic wave.

7.2. Material#

1 x motor:bit smart car kit

7.3. Background Knowledge#

What is ultrasonic wave?#

Ultrasonic Wave is a kind of sound wave with frequency higher than 20,000 Hz. It has good direction sense and strong penetration. It is easy to obtain comparatively concentrated sound energy and travel long distance underwater. It can be used to measure distance or speed, rinse, weld, break stones, or sterilize, etc.. Ultrasonic wave is named for its lower frequency limit is higher than the upper limit of human audible sound.

Principle of Ultrasonic Distance Measurement#

The transmitter emits ultrasonic wave to a certain direction. At the same time, it starts timing when emitting. Once encountered with a barrier, the ultrasonic wave spread in the air will be reflected back immediately. When the receiver has received the reflected wave, it will stop timing immediately. We can calculate the distance according to the time since the receiver has received the reflected wave, which is similar to radar ranging principle.

../../_images/8K6u2ce.jpg

7.4. Hardware Connection#

Connect sonar:bit to P5 on motor:bit. See picture below.

../../_images/t4vFZ0y.jpg

../../_images/kzPngGo.jpg

7.5. Software#

Microsoft MakeCode

7.6. Programming#

Step 1#

Click Advanced in the code drawer of MakeCode to see more options.

../../_images/LjMR5IU1.png

To program for motor:bit, we have to add a package. Find Add Package in the bottom of code drawer and click it. This will pop up a dialogue box. Search for “motorbit” and then click to download this package.

../../_images/XDlSfIS.png

Note: If you get a hint that some packages will be deleted due to the problem of incompatibility, you can either follow the prompts, or create a new project in the project menu.

Step 2#

At the beginning of our program.

Set the speed of both motors to 100.

../../_images/DaYNrl0.png

Step 3#

create a forever loop to read the return value of sonar:bit in real time and assign its value to away.

../../_images/Gfg2LZo.png

Stop to Prevent Crash#

When the distance detected (i.e. away) is under 8 cm, stop the car and delay time for 300 ms. After that, set the speed of both motors to a negative value(like -80) and delay time for 600 ms.

../../_images/QbwndAz.png

Steer to Avoid Barrier#

When away is under 15 cm, then generate a random number among 0 to 99.

If the random number is under 50, assign a negative value to the right wheel. So the right wheel will reverse and the car will turn right.

If the random number is beyond 50, assign a negative value to the left wheel. So the left wheel will reverse and the car will turn left.

../../_images/F5WjT9e.png

If away is beyond 15 cm, set the speed of both motors to 100 and the car will move forward.

../../_images/WRmK7A5.png

Program#

Link of the whole program: https://makecode.microbit.org/_cRgUy3h7VApT

You can also download it from the page below.

7.7. Result#

After powered on, the car will move forward at the speed of 100. When the barrier distance(i.e. away) is under 8 cm but not equal to 0 cm, the car will stop for 0.3 second and then start reversing. When the barrier distance is under 15 cm, the car will turn left or right randomly to avoid barrier. For other situations, the car will move forward at the speed of 100.

7.8. Think#

7.9. FAQ#

7.10. Relative Readings#