5% OFF Over $19, Code: Elephant5; 10% OFF Over $59, Code: Elephant10



Microbit Python Tutorials 13: Colorful breathing light

Posted by SuFiona on

Course13-Colorful breathing light

Learning goals:

This lesson learns to use Python programming to make the water light slowly turn off and to achieve the effect of breathing light.

Code

from microbit import *

import neopixel

display.show(Image.HAPPY)

np = neopixel.NeoPixel(pin16, 3)

while True:

for num in range(0, 255):

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

            np[pixel_id] = (num, 0, num)

            np.show()

            sleep(10)

import neopixel is means to import the neopixel library function, first let the robot display a smiley face, then define the pin of the flow light as pin16, the number is 3, iterate between 0 and 255, and display in the water lights. np[pixel_id] = (num, 0, num) means that the purple color is displayed, and their brightness values are superimposed from 0 every 10 milliseconds to stop at 255.

Programming and downloading

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

image.png 

Figure 13-1

2.As shown in Figure 13-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.

image.png 

Figure 13-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 13-3.

image.png 

Figure 13-3

4.The schematic diagram of the robot's water lamp is shown in Figure 13-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 the running light slowly lights up, as shown in Figures 13-5 to 13-8.

image.png 

Figure 13-4

image.png  image.png

Figures 13-5                                         Figures 13-6

image.png  image.png    

Figures 13-7                                          Figures 13-8         

The code of the experiment: 13.Colorful breathing light.rar

 

0 comments

Leave a comment

Please note, comments must be approved before they are published