Purpose
Let’s make a simiple project: A Timid TPBot Smart Car It lights on in the colourful mode in a quiet environment, but it lights off once there is louder noise near it, and it would move forward quickly to turns and face to the back side.
Materails
micro:bit V2 x 1
TPBot Smart Car x 1
Sound Sensor x 1
Rainbow LED ring x 1
Blocks x n
Hardware Connection
Connect the sound sensor to port 1 and the rainbow LED to port 2 on the TPBot.
Software Programming
Go to MakeCode editor:(https://makecode.microbit.org/#editor)Click “Advanced” in the drawer to see more choices for adding tpbot extension.
Search with “tpbot” in the dialogue box to download it.
Search with “planetX” in the dialogue box to download it.
Coding
1.In the on start brick, initialize the neopixel to be connected to port 2 and set to show rainbow mode.
2.In forever brick, judge if the variable I is above 75, if yes, set the headlights in black and clear the screen, set the rainbow strip in black and tpbots drive forward for 2 seconds and turn left for 0.3 seconds, then forward again for 1 second and turn left for 0.3 seconds, then stop the car and pause for 3 seconds, finally save the value of the sound sensor as the variable I.
3.If the variable I is not over 75, set to show icon and show the rainbow LED mode. Furthermore, it goes to another condition statements if the variable I is below 75: save the value of the sound sensor as I and set to light up the headlights of the TPBot with random colors, set the rainbow strips rotate the pixels and refresh to display.
Complete Code:
Link:https://makecode.microbit.org/_WrREf6ePK2WW
Result
After turning on, the TPBot smart car stays in place and displays the light effects. If a louder sound is detected, the lights are turned off and it drives a certain distance quickly. If no sound exceeding the threshold is detected after parking, the light effects will be displayed again .