Skip to content

Instantly share code, notes, and snippets.

@DavesCodeMusings
Created March 27, 2022 22:54
Show Gist options
  • Select an option

  • Save DavesCodeMusings/973e993fc35b6746170500f1ff1b3ea8 to your computer and use it in GitHub Desktop.

Select an option

Save DavesCodeMusings/973e993fc35b6746170500f1ff1b3ea8 to your computer and use it in GitHub Desktop.
Micropython NeoPixel LED strip DJ lighting effect
# LED chase that pulses to the beat of the music and features a color changing background.
import machine, neopixel
from time import ticks_ms, sleep_ms
bpm = 128 # Tempo of your mix in beats per minute
num_leds = 42 # Number of NeoPixel LEDs in the strip
data_pin = 16 # Data pin for the NeoPixel strip
bg_colors = [ # Series of changing background colors
(16, 0, 0), # red
(12, 4, 0), # orange
(9, 7, 0), # yellow
(0, 16, 0), # green
(0, 4, 12), # blue
(6, 0, 10), # violet
]
np = neopixel.NeoPixel(machine.Pin(data_pin), num_leds)
np.fill(bg_colors[0])
np.write()
bg_index = 0
for j in range(1, 8 * len(bg_colors) + 1): # run through the sequence for 8 beats
start_time = ticks_ms() # used to calculate execution time of LED chase sequence
for i in range(0, num_leds): # send a bright white pixel down the line of LEDs
np[i] = (255, 255, 255)
if (i > 0):
np[i - 1] = bg_colors[bg_index]
np.write()
sleep_ms(1)
np[num_leds - 1] = bg_colors[bg_index] # turn off the last LED after a sequence
np.write()
if (j % 8 == 0): # change the background color after every 8 beats
bg_index += 1
if (bg_index >= len(bg_colors)):
bg_index = 0
delay = round(60 / bpm * 1000) # 60 sec / bpm * 1000 gives milliseconds between beats
execution_time = ticks_ms() - start_time # figure out how much time was spent in chase sequence
delay -= execution_time # subtract execution time from time between beats for better synchronization
sleep_ms(delay)
np.fill((0,0,0))
np.write()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment