Skip to content

Instantly share code, notes, and snippets.

@DavesCodeMusings
Last active March 28, 2022 02:07
Show Gist options
  • Select an option

  • Save DavesCodeMusings/3aab5fd827c18d0f832dba5f2f20e968 to your computer and use it in GitHub Desktop.

Select an option

Save DavesCodeMusings/3aab5fd827c18d0f832dba5f2f20e968 to your computer and use it in GitHub Desktop.
Micropython NeoPixel LED strip lighting effect for beat drops
# A short sequence of LED flashes that doubles in frequency with each repetition, ending with a strobe.
import machine, neopixel
from time import ticks_ms, sleep_ms
bpm = 134 # 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
np = neopixel.NeoPixel(machine.Pin(data_pin), num_leds)
trigger = 1 # ideally, this would be triggered by an input rather than hard coded
while (trigger > 0):
for i in range(0, 3): # cycle through the four colors
multiplier = 2 ** i # used to double the frequency with each repetition
for j in range(0, multiplier):
start_time = ticks_ms() # used to calculate execution time of LED chase sequence
np.fill((64, 0, 0)) # red
np.write()
delay = round(60 / bpm * 1000) # 60 sec / bpm * 1000 gives milliseconds between beats
delay = round(delay / multiplier)
execution_time = ticks_ms() - start_time # figure out how much time was spent in chase sequence
delay = round(delay - execution_time) # subtract time spent calculating for better synchronization
sleep_ms(delay)
np.fill((0, 64, 0)) # green
np.write()
sleep_ms(delay)
np.fill((0, 32, 32)) # blue (azure)
np.write()
sleep_ms(delay)
np.fill((32, 0, 32)) # violet
np.write()
sleep_ms(delay)
# Finish with a strobe effect.
delay = delay >> 1 # a fast way to divide by two
for i in range(1, 16):
np.fill((255, 255, 255))
np.write()
sleep_ms(1)
np.fill((0, 0, 0))
np.write()
sleep_ms(delay)
trigger = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment