The sample code comes with seven different animations:

  1. Reading Light: a warm yellowish white light
  2. Fire - a flickering torch light effect
  3. Rainbow - a spread-out animated, shifting rainbow
  4. Rainbow Fade - The entire strip fades very slowly through the spectrum
  5. Starry Night - a pulsing dark blue with a white comet
  6. Bright White: Lights on white at 50% brightness (you can make this brighter but watch your power consumption)
  7. Off

You can add more animations or reorder them however you'd like. Check out the CircuitPython LED Animations Guide to see what other animations this library offers. There are several different Rainbow animations, Pulse, Chase, and some Sparkle variations that are a lot of fun to play with. 

Adding a New Animation

You'll need to change the code in three or four places:

  1. Import the animation at the very top. This defines which animation you're using (i.e. Rainbow, Pulse, Sparkle, etc).
  2. Create a definition for each mode under "Animation Setup". Here's where you decide what kind of Pulse or Sparkle you want to make (color, speed, etc).
  3. Add your mode to the Animations playlist. This determines which animation plays first, second, etc.
  4. For your top four favorite modes, tell the mode to trigger with one of the four numbered buttons in the BlueFruit app so you can jump straight to it.

1. Import the Animation

Near the top of the code you'll see a few lines importing various animations from the LED Animations Library. Each animation only needs to be imported once. If you want to use one of the animations we aren't using in the sample code (see them all here), you'll need to import the one you want in this section.

from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation.animation.comet import Comet
from adafruit_led_animation.animation.rainbow import Rainbow

2. Create a Definition for each Animation Mode

This is where you decide what solid colors you'd like, or how spread out your rainbows should be, or how quickly they move. You can define as many animations as you want, including multiples of each type. It's fine to have a slow, spread-out rainbow and also a fast and tight rainbow. Just call them by different names.

You can declare colors in a variety of ways including RGB tuples, hex codes, and HSV, or import from a selection of predefined colors up near the top of the code in the import section. 

You can also group animations together so they play at the same time. Look at the "fire" and "starry night" animations for examples of this.

One more thing to note is the "startup" animation. This is sort of a meta-animation, which calls the "powerup" animation I defined a little earlier, and runs it just once, using the AnimateOnce feature.

readingLight = Solid(pixels, color=0xFF7D13) #warm white color HEX code
brightWhite = Solid(pixels, color=(150, 150, 150))
rainbow = Rainbow(pixels, speed=0.1, period=10, step=0.5)
rainbowfade = RainbowFade(pixels, speed=0.4, name="rainbowfade")
powerup = RainbowComet(pixels, speed=0, tail_length=50, bounce=False)
off = Solid(pixels, color=BLACK)
 
#startup animation will play just once
startup = AnimateOnce(powerup)
 
#starrynight and fire are animation groups with layered effects.
starrynight = AnimationGroup(
    SparklePulse(pixels, speed=0.01, color=(0, 0, 150), period=1),
    Comet(pixels, speed=0, tail_length=8, color=(150, 150, 150), bounce=False),)
 
fire = AnimationGroup(
    Comet(pixels, speed=0, tail_length=1, color=BLACK),
    Sparkle(pixels, speed=0.05, num_sparkles=10, color=AMBER),
    Sparkle(pixels, speed=0.05, num_sparkles=10, color=RED),
    Sparkle(pixels, speed=0.05, num_sparkles=20, color=ORANGE),
    Sparkle(pixels, speed=0.05, num_sparkles=5, color=0xFF7D13),
    Sparkle(pixels, speed=0.05, num_sparkles=10, color=BLACK),
    )

3. Animations Playlist

Under the definitions you'll see an Animations Playlist section. The right arrow in the Bluetooth app will scroll through all the animations in this list, in the order you list them. You can define animations right in the playlist if you want, or just call the animations you defined earlier.

# Here is the animation playlist where you set the order of modes
 
animations = AnimationSequence(
        readingLight,
        fire,
        rainbow,
        starrynight,
        rainbowfade,
        brightWhite,
        auto_clear=True,
        )

4. Customize the Buttons

Finally, you can pre-program the numbered 1-4 buttons in the app to play specific animations - so you can always jump to your favorites. Just change the number of the animation within the playlist. Remember that the first animation (readingLight in our case) is numbered 0, and so these 4 buttons will play fire, rainbow, starry night, and rainbow fade since I've assigned them to 1, 2, 3, and 4.

elif isinstance(packet, ButtonPacket):
                if packet.pressed:
                    if packet.button == ButtonPacket.BUTTON_1:
                        MODE = 1
                        animations.activate(1)
                    elif packet.button == ButtonPacket.BUTTON_2:
                        MODE = 1
                        animations.activate(2)
                    elif packet.button == ButtonPacket.BUTTON_3:
                        MODE = 1
                        animations.activate(3)
                    elif packet.button == ButtonPacket.BUTTON_4:
                        MODE = 1
                        animations.activate(4)
                    # change the mode with right arrow
                    elif packet.button == ButtonPacket.RIGHT:
                        MODE = 1
                        animations.next()
                    elif packet.button == ButtonPacket.LEFT:
                        MODE = 4
                        off.animate()

The right arrow scrolls through all the modes, stepping forward. There's no easy way to scroll backwards through the modes, so I've made the right arrow into the "off" button -- always nice to have within one tap.

This guide was first published on Sep 29, 2020. It was last updated on Sep 29, 2020.

This page (Customizing Your Code) was last updated on Oct 15, 2021.

Text editor powered by tinymce.