Spring Sale: 5% OFF Over \$89, Code: Elephant5; 10% OFF Over \$149, Code:Elephant10

# Microbit Python Tutorials 12: Colorful marquee

Posted by SuFiona on

Course12-Colorful marquee

Learning goals:

This lesson learns to use Python programming to turn the micro:bit robot's water light from left to right.

Code

from microbit import *

import neopixel

display.show(Image.HAPPY)

# The water lamp is connected to pin pin16, the number is 3

np = neopixel.NeoPixel(pin16, 3)

while True:

for pixel_id in range(0, len(np)):

np[0] = (255, 0, 0)

np.show()

sleep(200)

np.clear()

np[1] = (0, 255, 255)

np.show()

sleep(200)

np.clear()

np[2] = (0, 0, 255)

np.show()

sleep(200)

np.clear()

np[0] = (255, 255, 0)

np.show()

sleep(200)

np.clear()

np[1] = (0, 255, 0)

np.show()

sleep(200)

np.clear()

np[2] = (255, 0, 255)

np.show()

sleep(200)

np.clear()

import neopixel is means to import the neopixel library function, first let the robot display a smile, then define the pin of the flow lamp as pin16, the number is 3, iterate each LED in the water lights. np[0] = ( 255, 0, 0) means that the first water light is red, and the delay is 200 milliseconds after lighting, clearing the display, lighting the second light, and so on.

1.You should open the Mu software, and enter the code in the edit window, , as shown in Figure 12-1.

Figure 12-1

2.As shown in Figure 12-2, you need to 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.

Figure 12-2

3.You need to connect the micro data cable to micro:bit and the computer, then click the Flash button to download the program to micro:bit as shown in Figure 12-3.

Figure 12-3

4. The schematic diagram of the robot's water lamp is shown in Figure 12-4. As you can see, the robot's flow lamp is connected to the micro:bit pin16. Therefore, we set the pin of the flow lamp to pin16 in the program. After downloading the program to micro:bit, you can see a smiley face on the robot's dot matrix and start running the marquee, as shown in Figures 12-5 to 12-7.

Figures 12-4

Figures 12-5                 Figures 12-6                Figures 12-7

The code of the experiment: 12.Colorful marquee.rar