Skip to content

Instantly share code, notes, and snippets.

@helgibbons
Last active October 26, 2023 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgibbons/59cddf448bbe22b2242d9ac22ae75f9e to your computer and use it in GitHub Desktop.
Save helgibbons/59cddf448bbe22b2242d9ac22ae75f9e to your computer and use it in GitHub Desktop.
Bike lights 2022
import plasma
from plasma import plasma2040
import time
# Random functions! We'll be using randrange for picking integers from a range, and uniform for floats.
from random import randrange, uniform
from pimoroni import RGBLED, Button
from machine import Pin
led = RGBLED(plasma2040.LED_R, plasma2040.LED_G, plasma2040.LED_B)
led.set_rgb(0, 0, 0)
button_a = Pin(plasma2040.BUTTON_A, Pin.IN, Pin.PULL_UP)
button_b = Pin(plasma2040.BUTTON_B, Pin.IN, Pin.PULL_UP)
button_boot = Pin(plasma2040.USER_SW, Pin.IN, Pin.PULL_UP)
# Set how many LEDs you have
NUM_LEDS = 100
# Set up speed (wait time between updates, in seconds.)
SPEED = 0.3
HUE_START = 0 # orange
HUE_END = 360 # green
# how many modes
MODES = 6
def button_handler(pin):
global pressed, mode
if not pressed:
pressed=True
print(pin)
if pin == button_a:
mode += 1
if mode > MODES:
mode = MODES
elif pin == button_b:
mode -= 1
if mode < 1:
mode = 1
pressed = False
def spooky_rainbows(HUE_START, HUE_END, SPEED):
global distance, direction
for i in range(NUM_LEDS):
# generate a triangle wave that moves up and down the LEDs
j = max(0, 1 - abs(distance - i) / (NUM_LEDS / 3))
hue = HUE_START + j * (HUE_END - HUE_START)
led_strip.set_hsv(i, hue / 360, 1.0, 0.8)
# reverse direction at the end of colour segment to avoid an abrupt change
distance += direction
if distance > NUM_LEDS:
direction = - SPEED
if distance < 0:
direction = SPEED
time.sleep(0.01)
def just_red():
for i in range(NUM_LEDS):
led_strip.set_rgb(i, 200, 0, 0)
def fire():
from random import random, uniform
# fire effect! Random red/orange hue, full saturation, random brightness
for i in range(NUM_LEDS):
led_strip.set_hsv(i, uniform(0.0, 50 / 360), 1.0, random())
time.sleep(0.2)
def static_rainbow():
offset = 0.001
for i in range(NUM_LEDS):
hue = float(i) / NUM_LEDS
led_strip.set_hsv(i, hue + offset, 1.0, 1.0)
# Pick *one* LED type by uncommenting the relevant line below:
# APA102 / DotStar™ LEDs
# led_strip = plasma.APA102(NUM_LEDS, 0, 0, plasma2040.DAT, plasma2040.CLK)
# WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma2040.DAT)
# Start updating the LED strip
led_strip.start()
pressed = False
mode = 3
distance = 0.0
direction = SPEED
button_a.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
button_b.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
button_boot.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
while True:
if mode == 1:
spooky_rainbows(40, 150, 0.4)
elif mode == 2:
spooky_rainbows(0, 360, 0.3)
elif mode == 3:
just_red()
elif mode == 4:
fire()
elif mode == 5:
spooky_rainbows(230, 330, 0.4)
elif mode == 6:
static_rainbow()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment