In this tutorial section, we write and run the first program on the Playground Express. Copy-paste the following code to the [code.py](<http://code.py>)
file you opened.
import time
import board
import neopixel
leds = neopixel.NeoPixel(board.NEOPIXEL, 10)
while True:
leds[0] = (255,0,0)
time.sleep(1)
leds[0] = (0,0,0)
time.sleep(1)
Next, click save.
By clicking save
, the code is saved to the board and starts running immediately. One of the LEDs should start flashing red. Below, we analyze the code.
The lines below load some of the libraries and make them available for use. For the purpose of this tutorial, we can ignore the import
statements.
import time
import board
import neopixel
The following line generates the variable leds
. This variable can be used to refer to each of the LEDs on the board.
leds = neopixel.NeoPixel(board.NEOPIXEL, 10)
The next lines are the main part of the program. They start an infinite loop using while True
. Next, zeroth LED (see next image) is set to light up red. This is done by assigning it the RGB value (250, 0, 0)
. After this, the program waits 1 second using the sleep command. Next, the zeroth LED is switched off by setting its value to (0,0,0)
.
while True:
leds[0] = (255,0,0)
time.sleep(1)
leds[0] = (0,0,0)
time.sleep(1)
Each LED has a number (index) from 0 to 9. The image below shows the numbering of the LEDs.
The colors of the LEDs are set as RGB values. If you are not familiar with this way of selecting colors, you can refer to this chart:
https://www.rentaliowa.com/pa_system_rentals/rgbcolorchart.htm.
This chart gives the RBG values for a range of colors. The chart shows that changing the line leds[0] = (255,0,0)
to leds[0] = (225,140,0)
will make the LED turn orange. A screenshot of part of the chart is shown below.
Before continuing with the rest of this tutorial, you should try to edit the program to do the following.