Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active January 17, 2023 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save todbot/8c31150b373c8d2eb5d6c5f8fcafca12 to your computer and use it in GitHub Desktop.
Save todbot/8c31150b373c8d2eb5d6c5f8fcafca12 to your computer and use it in GitHub Desktop.
Trinkey Dance Party : fun pulsing colors for NeoTrinkey
# Trinkey dance party
# 2021 @todbot
import time
import board
import neopixel
import random
leds = neopixel.NeoPixel(board.NEOPIXEL, 4, brightness=0.3, auto_write=False)
bpm = 130
ms_per_beat = int(60e3 / bpm / 16)
i=0
while True:
leds[0:] = [[max(i-8,0) for i in l] for l in leds] # fadeToBlackBy(8)
leds.show()
i = (i+1) % ms_per_beat
if i==0:
leds[0:] = [int(random.random() * 2**24) for l in leds] # each LED gets new random color
time.sleep(0.01)
@todbot
Copy link
Author

todbot commented Jan 17, 2023

Awesome, let me know if you have any other questions.
If you're just getting started, the above is using a Python trick called "list comprehensions" to do things in one line.
So for instance this line:

    leds[0:] = [rainbowio.colorwheel(random.random()*255) for l in leds]

is the same as:

    for i in range(len(leds)):
      leds[i] = rainbowio.colorwheel(random.random()*255)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment