Ultrasonic Avoid
Ultrasonic Avoid
Learning goal:
In this lesson, we will learn how to obtain ultrasonic distance. According to the detected
distance data, the direction of Tiny:bit operation is changed to achieve obstacle avoidance.
Code:
1) Import the library needed for this lesson from micro:bit, display is used for dot matrix
display, Image calls the built-in image, pin12 is the pin of the body colorful lights, neopixel
drives the body colorful lights, and tinybit controls the car.
display.show (Image.HAPPY): Display a smile on micro:bit dot matrix.
np = neopixel.NeoPixel (pin12, 2): Initialize the body colorful lights. The first parameter
is the pins connected to the lights, and the second parameter is the total number of RGB
lights.
np.clear (): Clear the body colorful lights.
www.yahboom.com
tinybit.car_HeadRGB (0, 0, 0): Clear the head RGB searchlight.
2) The distance of the ultrasonic wave is read cyclically and stored in the distance variable.
tinybit.ultrasonic () returns the current distance data of the ultrasonic wave, the unit is
centimeter (cm);
3) Judging the distance, if it is less than 20cm, confirm whether it is less than 20cm with a
delay of 10 milliseconds to avoid the impact of data fluctuations;
After confirming that the distance is less than 20cm, stop the car and display an 'X'
character on the micro:bit dot matrix. The headlight will display a red light, buzzer will sound,
then the car back and spin the right to achieve ultrasonic avoidance.
If the distance is greater than 20cm, front RGB searchlight become green, the dot matrix
shows an arrow pointing forward, and car will keep going forward.
1.You should open the Mu software, and enter the code in the edit window, , as shown
below.
Note! All English and symbols should be entered in English, and the last line must be
a space.
2.You can click the “Check” button to check if our code has an error. If a line appears with a
cursor or an underscore, the program indicating this line is wrong.
3.Click “REPL” button,check whether the tinybit library has been downloaded. If not, please
refer to the [preparation before class]---> [Python programming]
www.yahboom.com
4.Click the “Flash” button to download the program to micro:bit board.
If the program is wrong or the experimental phenomenon is wrong after downloading, please
confirm whether you have downloaded the Buildingbit libraryhex file we provided to the
micro: bit board.
For the specific method of adding library files, please refer to 【1.Preparation before class】
---【Python programming】
Experimental phenomena
If the distance is greater than 20cm, RGB headlight become green, the dot matrix shows an
arrow pointing forward, and car will keep going forward. If it is less than 20cm, stop the car
and display an 'X' character on the micro:bit dot matrix. The headlight will display a red light,
buzzer will sound, then the car back and spin the right to achieve ultrasonic avoidance.
www.yahboom.com