import board
import neopixel
import time
nPix = 20
pixels = neopixel.NeoPixel(board.D18, nPix)
def turnOff(n):
for i in range(n):
pixels[i] = (0,0,0)
for i in range(0, nPix, 2):
pixels[i] = (0, 0, 255)
time.sleep(2)
turnOff(nPix)
Lines 8-10: The function
def turnOff(n):
for i in range(n):
pixels[i] = (0,0,0)
This is the function that turns off the lights. Looking at line 8 def turnOff(n):
.
len
).n
. It does not say what type of input (string, integer, etc.), nor does it have a default value (you can set a default by using (n=10)
) for example). But it's mostly up to you to send the right type of information.Lines 9 and 10 are the body of the function that just turn off the number of lights you told it to. Note that the body is indented, which is how python indicates what's inside the function.
Line 16: Calling the function.
turnOff(nPix)
This calls the function and passes it the number of pixels (nPix) which was set to 20 on line 5.
Solution:
import board
import neopixel
import time
nPix = 20
pixels = neopixel.NeoPixel(board.D18, nPix)
def turnOff(n):
for i in range(n):
pixels[i] = (0,0,0)
def turnOn(n):
for i in range(n):
pixels[i] = (0,200,0)
turnOn(5)
time.sleep(2)
turnOn(10)
time.sleep(2)
turnOn(15)
time.sleep(2)
turnOff(15)
turnOn(3)
def turnOn(n, col = (255,0,0)):
Solution:
import board
import neopixel
import time
nPix = 20
pixels = neopixel.NeoPixel(board.D18, nPix)
def turnOff(n):
for i in range(n):
pixels[i] = (0,0,0)
def turnOn(n, col = (255,0,0)):
for i in range(n):
pixels[i] = col
turnOn(5, (0,255,0))
time.sleep(2)
turnOn(10)
time.sleep(2)
turnOn(15, (255,255,0))
time.sleep(2)
turnOff(15)
turnOn(3, (0,0,255))
Note on line 16 how the color (green) is passed, while on line 18 no value is passed so the color defaults to red.
turnOn(5, (0,255,0))
time.sleep(2)
turnOn(10)