Welcome to Tiny:bit Robot Car repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
5.Distance control light
Learning goal:
In this lesson, we will learn how to obtain ultrasonic distance, and use the detected distance data to switch the color of the lights through Python programming.
Code:
Please use the MU software to open the Distance control light.py file we provided.
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.
tinybit.car_HeadRGB (0, 0, 0): Clear the head RGB searchlight.
Code as shown below:
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 0 and less than or equal to 4, let the front RGB searchlights and body colorful lights be red, and if the distance is greater than 4 and less than or equal to 8, let the front RGB searchlight and body colorful lights be green; analogy. Finally, when it is over 20cm, clear all lights.
...
Programming and downloading:
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]
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:
After download is complete, open the power switch. Micro:bit will display a smile on dot matrix, color of RGB light will change with distance different.