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