for i in range([start], stop, [step]):
The range function can be used in three ways.
for i in range(5):
print(i)
will output:
0
1
2
3
4
for i in range(2, 6):
print(i)
will output:
2
3
4
5
for i in range(4, 10, 2):
print(i)
will output:
4
6
8
import board
import neopixel
nPix = 20
pixels = neopixel.NeoPixel(board.D18, nPix)
for i in range(0, nPix, 2):
pixels[i] = (0, 0, 255)
Line 8: It uses a for loop with a starting value (0), ending value (nPix), and step value (2). nPix is defined on Line 4 with a value of 20.
Solution: To light up the last 5 lights (green) use:
import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 20)
for i in range(15, 20):
pixels[i] = (0, 20, 0)
for j in range(5):
for j in range(5):
for i in range(20):
print(i,j)
Solution: To repeat the lighting sequence 5 times use:
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.D18, 20)
for j in range(5):
for i in range(19, 0, -1):
pixels[i] = (0, 20, 0)
time.sleep(0.25)