20. Case 10 Smart Crashproof Car#

20.1. Purpose#

  • Make a smart crashproof car with a Sonar:bit.

20.2. Materials#

20.3. Hardware Connection#

  • Connect the left servo to P1, right servo to P2 and Sonar:bit to P0 of the Ring:bit.

https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/ring_bit_v2/images/case_10_01.png

20.4. Software Programming#

Microsoft makecode online coding https://makecode.microbit.org/#

20.5. Software#

Step 1#

  • Click “Advanced” in the drawer of MakeCode to see more choices.

../../_images/2qCyzQ71.png

  • We need to add a package for programming the kit. Click “Extensions” in the drawer and search “ringbit” in the dialogue box to download it.

../../_images/1Wq2Mov1.jpg

  • We also need to add a package for programming the Sonar:bit. Click “Extensions” in the drawer and search “https://github.com/elecfreaks/pxt-sonarbit” in the dialogue box to download it.

Note: if you are informed that it will be deleted due to incompatibility of the codebase, you can go on with the tips or create a new project in the menu.

Step 2#

  • Drag the pins selection bricks for servos in the On start brick, the pins numbers are set according to the actual connection port.

  • Move forward at full speed.

https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/ring_bit_v2/images/case_10_02.png

Step 3#

  • Set variate sonar in the forever brick and read the detected value from the Sonar:bit to it.

  • Drag the if brick and judge if the value given by sonar is below 10 and not equal to 0.

  • If yes, set the speed of right wheel to 100 and the left to 0 to turn left with 500ms for obstacle avoidance.

  • If not, move forward at full speed.

https://raw.githubusercontent.com/elecfreaks/learn-cn/master/microbitKit/ring_bit_v2/images/case_10_03.png

Reference#

Links: https://makecode.microbit.org/_RTwFcMeA1MMY

You can also download it below:

20.6. Result#

  • The Ring:bit car turns left automatically when detecting any obstacle 10cm in front of it.

20.7. Exploration#

  • Question: Why we need to judge if the value is not 0 ?

  • Answer: The detection value is also 0 if beyond the detection scope of Sonar:bit.

20.8. FAQ#