neopixel_simpletest.py
1 import time 2 import board 3 import neopixel 4 5 6 # On CircuitPlayground Express, and boards with built in status NeoPixel -> board.NEOPIXEL 7 # Otherwise choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D1 8 pixel_pin = board.NEOPIXEL 9 10 # On a Raspberry pi, use this instead, not all pins are supported 11 # pixel_pin = board.D18 12 13 # The number of NeoPixels 14 num_pixels = 10 15 16 # The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! 17 # For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. 18 ORDER = neopixel.GRB 19 20 pixels = neopixel.NeoPixel( 21 pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER 22 ) 23 24 25 def wheel(pos): 26 # Input a value 0 to 255 to get a color value. 27 # The colours are a transition r - g - b - back to r. 28 if pos < 0 or pos > 255: 29 r = g = b = 0 30 elif pos < 85: 31 r = int(pos * 3) 32 g = int(255 - pos * 3) 33 b = 0 34 elif pos < 170: 35 pos -= 85 36 r = int(255 - pos * 3) 37 g = 0 38 b = int(pos * 3) 39 else: 40 pos -= 170 41 r = 0 42 g = int(pos * 3) 43 b = int(255 - pos * 3) 44 return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0) 45 46 47 def rainbow_cycle(wait): 48 for j in range(255): 49 for i in range(num_pixels): 50 pixel_index = (i * 256 // num_pixels) + j 51 pixels[i] = wheel(pixel_index & 255) 52 pixels.show() 53 time.sleep(wait) 54 55 56 while True: 57 # Comment this line out if you have RGBW/GRBW NeoPixels 58 pixels.fill((255, 0, 0)) 59 # Uncomment this line if you have RGBW/GRBW NeoPixels 60 # pixels.fill((255, 0, 0, 0)) 61 pixels.show() 62 time.sleep(1) 63 64 # Comment this line out if you have RGBW/GRBW NeoPixels 65 pixels.fill((0, 255, 0)) 66 # Uncomment this line if you have RGBW/GRBW NeoPixels 67 # pixels.fill((0, 255, 0, 0)) 68 pixels.show() 69 time.sleep(1) 70 71 # Comment this line out if you have RGBW/GRBW NeoPixels 72 pixels.fill((0, 0, 255)) 73 # Uncomment this line if you have RGBW/GRBW NeoPixels 74 # pixels.fill((0, 0, 255, 0)) 75 pixels.show() 76 time.sleep(1) 77 78 rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step