Navigation Menu

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)
@Pants84
Copy link

Pants84 commented Jan 17, 2023

How would you write this script so that pushing the buttons increases and decreases the BPM?

@todbot
Copy link
Author

todbot commented Jan 17, 2023

How would you write this script so that pushing the buttons increases and decreases the BPM?

Here's how I would do that today:

# Trinkey dance party, now with changeable BPM
# 16 Jan 2023 - @todbot / Tod Kurt
#
import time, random, board, neopixel, rainbowio, touchio

leds = neopixel.NeoPixel(board.NEOPIXEL, 4, brightness=0.3, auto_write=False)
touch1 = touchio.TouchIn(board.TOUCH1)
touch2 = touchio.TouchIn(board.TOUCH2)

bpm = 130
i=0
while True:
    ms_per_beat = int(60e3 / bpm / 16)
    leds[0:] = [[max(i-8,0) for i in l] for l in leds] # same as "fadeToBlackBy(8)"
    leds.show()
    i = (i+1) % ms_per_beat
    if i==0:  # at the beat, each LED gets new random color
        leds[0:] = [rainbowio.colorwheel(random.random()*255) for l in leds]
    # touch pads change BPM up or down
    if touch1.value:
        bpm = bpm - 1
    if touch2.value:
        bpm = bpm + 1
    if touch1.value and touch2.value:  # reset BPM if both are touched
        bpm = 130

    time.sleep(0.01)

@Pants84
Copy link

Pants84 commented Jan 17, 2023 via email

@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