4. case 02 Keep Distance

4.1. Our Goal


  • Make motor:bit smart car keeps distance with another car.

4.2. Material


  • 1 x motor:bit smart car kit

4.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

4.4. Hardware Connection


Connect sonar:bit to P10 on motor:bit.

../../_images/t4vFZ0y.jpg

../../_images/kzPngGo.jpg

4.5. Software


Microsoft MakeCode

4.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

  • Insert a move forward with speed block below on start, and set its parameter to 80, which means the car moves forward at the speed of 80 once powered on.

  • The unit of ultrasonic wave is centimeter(cm). Read the value of P10 and assign its return value to variable item.

  • If the return value is above 5 but under 10, set the speed of motors to 0 and stop the car.

  • If the return value is under 5, set the motor speed to 80 and make the car reverse.

  • If none of these values meet, set the motor speed to 80 and make the car move forward.

../../_images/gcR99Lj.png

Program

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

You can also download it from the page below.

---

4.7. Result


  • Once powered on, the car moves forward at the speed of 80. Place your hand in front of the car at a distance among 5~10cm, the car stops moving forward. Again, place your hand closer to the car but at a distance less than 5 cm, the car will move back and then stop when the distance is beyond 5 cm.

4.8. Think


4.9. FAQ


4.10. Relative Readings