Welcome to Tiny:bit Pro repository
7.Car Headlights
Learning goal:
This lesson learns how to drive car headlights by Python programming.
Code:
1) Import the libraries needed for this routine: display is used to display the dot matrix, Image is used to display the built-in pattern, sleep delay time, and tinybit is used to drive the car.
2) display.show (Image.HAPPY) Make the micro:bit display a smiling face;
3) buildingbit.car_HeadRGB (255, 0, 0) Makes the front searchlight display. The first parameter is a red value with a range of 0 to 255. The second parameter is a green value with a range of 0 to 255. The third parameter is the blue value, which ranges from 0 to 255.
4) sleep (500) is a delay of 500 milliseconds. You can modify the corresponding value as needed. The smaller the value, the faster the color of the lamp can be switched.
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. A smile face appears on the micro:bit dot matrix. The headlight on the front of the car lights become red --> green --> blue --> white, time interval. Keep the loop in this state.