In this lesson, we will draw three different colors at three different locations on the LED strip. We will then repeat this pattern down the strip.
Our first task is to draw the red, green and blue in the first three pixels of the LED strip.
Our main code will be the following
12345678
# make the fist location redstrip[0]=(255,0,0)# make the second location greenstrip[1]=(0,255,0)# make the third location bluestrip[2]=(0,0,255)# update the stripstrip.write()
Full Program
1 2 3 4 5 6 7 8 91011121314151617
frommachineimportPinfromneopixelimportNeoPixelfromutimeimportsleep,ticks_msfromurandomimportrandintNEOPIXEL_PIN=0NUMBER_PIXELS=60strip=NeoPixel(Pin(NEOPIXEL_PIN),NUMBER_PIXELS)# make the fist location redstrip[0]=(255,0,0)# make the second location greenstrip[1]=(0,255,0)# make the third location bluestrip[2]=(0,0,255)# update the stripstrip.write()
Repeating the Pattern
Now, let's repeat this pattern over the entire strip. We can do this by wrapping the three lines in a for loop like this:
1 2 3 4 5 6 7 8 91011121314151617
frommachineimportPinfromneopixelimportNeoPixelfromutimeimportsleep,ticks_msfromurandomimportrandintNEOPIXEL_PIN=0NUMBER_PIXELS=60strip=NeoPixel(Pin(NEOPIXEL_PIN),NUMBER_PIXELS)# count 0 to the end skipping every 3foriinrange(0,NUMBER_PIXELS-1,3):strip[i]=(255,0,0)# make the second location greenstrip[i+1]=(0,255,0)# make the third location bluestrip[i+2]=(0,0,255)# update the stripstrip.write()
Note here that the range function has a third parameter that tells us how many to skip in each iteration of the loop. Since we have three colors, we can skip every three values of i. I will get values of 0, 3, 6, 9, 12... etc.