|
| 1 | +# CircuitPython demo - NeoPixel |
| 2 | + |
| 3 | +import time |
| 4 | +import board |
| 5 | +import neopixel |
| 6 | + |
| 7 | + |
| 8 | +# On CircuitPlayground Express -> Board.NEOPIXEL |
| 9 | +# Otherwise choose an open pin connected to the Data In of the NeoPixel strip, |
| 10 | +# such as board.D1 |
| 11 | +pixpin = board.NEOPIXEL |
| 12 | + |
| 13 | +# The number of pixels in the strip |
| 14 | +numpix = 10 |
| 15 | + |
| 16 | +# number of colors in each pixel, =3 for RGB, =4 for RGB plus white |
| 17 | +BPP = 3 |
| 18 | + |
| 19 | +strip = neopixel.NeoPixel(pixpin, numpix, bpp=BPP, brightness=0.3, auto_write=False) |
| 20 | + |
| 21 | +def format_tuple(r, g, b): |
| 22 | + if BPP == 3: |
| 23 | + return (r, g, b) |
| 24 | + return (r, g, b, 0) |
| 25 | + |
| 26 | +def wheel(pos): |
| 27 | + # Input a value 0 to 255 to get a color value. |
| 28 | + # The colours are a transition r - g - b - back to r. |
| 29 | + if (pos < 0) or (pos > 255): |
| 30 | + return format_tuple(0, 0, 0) |
| 31 | + if pos < 85: |
| 32 | + return format_tuple(int(pos * 3), int(255 - (pos*3)), 0) |
| 33 | + elif pos < 170: |
| 34 | + pos -= 85 |
| 35 | + return format_tuple(int(255 - pos*3), 0, int(pos*3)) |
| 36 | + #else: |
| 37 | + pos -= 170 |
| 38 | + return format_tuple(0, int(pos*3), int(255 - pos*3)) |
| 39 | + |
| 40 | +def rainbow_cycle(wait): |
| 41 | + for j in range(255): |
| 42 | + for i in range(strip.n): |
| 43 | + idx = int((i * 256 / len(strip)) + j) |
| 44 | + strip[i] = wheel(idx & 255) |
| 45 | + strip.show() |
| 46 | + time.sleep(wait) |
| 47 | + |
| 48 | +while True: |
| 49 | + strip.fill(format_tuple(255, 0, 0)) |
| 50 | + strip.show() |
| 51 | + time.sleep(1) |
| 52 | + |
| 53 | + strip.fill(format_tuple(0, 255, 0)) |
| 54 | + strip.show() |
| 55 | + time.sleep(1) |
| 56 | + |
| 57 | + strip.fill(format_tuple(0, 0, 255)) |
| 58 | + strip.show() |
| 59 | + time.sleep(1) |
| 60 | + |
| 61 | + rainbow_cycle(0.001) # rainbowcycle with 1ms delay per step |
0 commit comments