Welcome to Tiny:bit Pro repository
13.LED light
Learning goal:
This lesson learns how to drive two LED lights of Tiny:bit car by Python programming.
Code:
1) Import the microbit library, neopixel for RGB lights, and random for generating random numbers.
2) display.show (Image.HAPPY): show smiling faces;
3) np = neopixel.NeoPixel (pin12, 2): Initialize the RGB programming lamp library. The first parameter is the pins of the RGB lamp, and the second parameter is the number of RGB lamps.
4) for pixel_id in range (0, len (np)): for loop, starting from 0, automatically +1 for each loop, ending the loop when len (np) is reached;
5) R = random.randint (0, 255): Generate a random number from 0 to 255 and save it to the R variable;
6) np.clear (): Clear RGB light display;
7) np [pixel_id] = (R, G, B): Pass the color RGB value of the RGB lamp to the pixel_id lamp;
8) np.show (): refresh the colorful lights display, if you do not run this function, the above set color will not work;
9) sleep (500): 500 milliseconds delay.
According to the hardware interface reference manual, the sound sensor connect to P12 of micro:bit.
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 dot matrix will display a smile. Two LED light will light up in turn, time interval is 0.5 seconds, and the color of each light is random.
