Skip to content

Instantly share code, notes, and snippets.

@pingswept
Created January 8, 2023 21:43
Show Gist options
  • Save pingswept/d20c5164679df64fb7a599eb61dc3b87 to your computer and use it in GitHub Desktop.
Save pingswept/d20c5164679df64fb7a599eb61dc3b87 to your computer and use it in GitHub Desktop.
Python code to light up a dragon's tail made of LEDs that attaches to the back of my bike.
import time
import board
import neopixel
import random
# The number of NeoPixels
num_pixels = 240
# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed!
# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW.
ORDER = neopixel.GRB
strip_one = neopixel.NeoPixel(
board.D4, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
strip_two = neopixel.NeoPixel(
board.D5, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
strip_three = neopixel.NeoPixel(
board.D6, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
strip_four = neopixel.NeoPixel(
board.D9, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
strip_five = neopixel.NeoPixel(
board.D10, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
def fire_animation():
while True:
rgb = (255, 50, 12)
delay = random.choice(range(10, 30))/1000
for p in range(num_pixels):
flicker = random.choice(range(40))
rgb_r = tuple([x - flicker if x-flicker >= 0 else 0 for x in rgb ])
pixels[p] = rgb_r
pixels.show()
time.sleep(delay)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
r = g = b = 0
elif pos < 85:
r = int(pos * 3)
g = int(255 - pos * 3)
b = 0
elif pos < 170:
pos -= 85
r = int(255 - pos * 3)
g = 0
b = int(pos * 3)
else:
pos -= 170
r = 0
g = int(pos * 3)
b = int(255 - pos * 3)
return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
strip_one[i] = wheel(pixel_index & 255)
strip_one.show()
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
strip_two[i] = wheel(pixel_index & 255)
strip_two.show()
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
strip_three[i] = wheel(pixel_index & 255)
strip_three.show()
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
strip_four[i] = wheel(pixel_index & 255)
strip_four.show()
for i in range(num_pixels):
pixel_index = (i * 256 // num_pixels) + j
strip_five[i] = wheel(pixel_index & 255)
strip_five.show()
#time.sleep(wait)
#fire_animation()
rainbow_cycle(0.001)
while True:
rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment