6. Case 04: Obstacle-avoidance Driving¶
Programme to set the TPBot avoiding an obstacle automatically.
Click “Advanced” to see more choices in the MakeCode drawer.
We need to add a package for programming. Click “Extensions” in the bottom of the drawer and search with “tpbot” in the dialogue box to download it.
Set a triangle in the micro:bit and the TPBot moving forward at the speed of 50%; Set the LED headlights in green.
Save the variable of the distance from the TPBot to the obstacle in the forever brick, judge if the items of
i≠0are both true, if yes, set TPBot stopping immediately and the headlights flashing three times; set it reversing for 1 second at the speed of 30% and then turning left for 0.5 second at the speed of 30%; finally set the headlights showing green and the TPBot moving forward at the speed of 50%.
Power up the TPBot to move forward and it stops immediately with the headlights flashing three times if an obstacle was detected, then it reverses and turns left to keep moving forward with the headlights in green.
Q: The car does not work with the code in the wiki.
A: It should be the batteries that are lack of power, please try to fix it by adding the value of the speed in the code.