18. Case 10 Smart Crashproof Car¶
Make a smart crashproof car with a Sonar:bit.
1 x Ring:bit Car kit
1 x micro:bit
1 x Sonar:bit
18.3. Hardware Connection¶
Connect the left servo to P1, right servo to P2 and Sonar:bit to P0 of the Ring:bit.
18.4. Software Programming¶
Click “Advanced” in the drawer of MakeCode to see more choices.
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.
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.
Drag the pins selection bricks for servos in the
On startbrick, the pins numbers are set according to the actual connection port.
Move forward at full speed.
foreverbrick and read the detected value from the Sonar:bit to it.
ifbrick and judge if the value given by
sonaris 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.
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.