import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
pixels[2] = (10,0,0)
Looking through the code line by line:Lines 1 and 2:
import board
import neopixel
Import two modules that we need to light the LED's.
Line 4:
pixels = neopixel.NeoPixel(board.D18, 20)
This activates the LED strip. Specifically it indicates that:
Line 6:
pixels[2] = (10,0,0)
This makes the third light red.
(10,0,0)
indicates a low level of red, but no green or blue. Thus the third light shows a faint red.
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
pixels[2] = (10,0,0)
Looking through the code line by line:Lines 1 and 2:
import board
import neopixel
Import two modules that we need to light the LED's.
Line 4:
pixels = neopixel.NeoPixel(board.D18, 20)
This activates the LED strip. Specifically it indicates that:
Line 6:
pixels[2] = (10,0,0)
This makes the third light red.
(10,0,0)
indicates a low level of red, but no green or blue. Thus the third light shows a faint red.Solution: Thus, to light up the 5th light just change the index of pixels to get:
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
pixels[4] = (10,0,0)
Solution: For the brightest, purest yellow:
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
pixels[3] = (255,255,0)
for i in range(5):
Don't forget the colon (:), otherwise this will not work.
In python, you indicate what gets repeated by tabbing it over like so:
for i in range(5):
pixels[i] = (10,0,0)
Notice how the index in pixels[i] is the variable i. The variable is created in the for loop (you can use any variable name you like, it does not have to be i)
Solution: Making the first 5 lights red
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
for i in range(5):
pixels[i] = (10,0,0)
for i in range(4, 9):
Notice that the second number ("9"), is one more than the last number we want.
Solution: Make the first 5 lights red and the rest blue
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
# red
for i in range(6):
pixels[i] = (10,0,0)
# blue
for i in range(5, 20):
pixels[i] = (0,0,10)
First import time:
import time
Then use the sleep function like so:
time.sleep(0.25)
Solution: Lighting up all lights with a delay
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.D18, 20)
for i in range(20):
pixels[i] = (10,0,0)
time.sleep(0.25)