Rainbowcomet and rainbowsparkle!

The CircuitPython LED Animation library includes a series of rainbow animations: rainbow, rainbowchase, rainbowcomet, and rainbowsparkle. This section will cover these animations and the available customisations for each of them. Let's make some rainbows!

Most animations will run individually on the SAMD21 (M0) microcontroller boards, but some combinations of animations and the most complex animations will not. Check out the FAQ for details. If you're interested in running all the animations, or many animations together, consider using at least a SAMD51 (M4) microcontroller.

Rainbow

This animation displays a shifting rainbow across all the pixels with a number of customization options.

First, you import the Rainbow module. See Import and Setup for the rest of the necessary imports and pixel object creation.

Download: file
from adafruit_led_animation.animation.rainbow import Rainbow

Next you create the Rainbow animation. Rainbow requires two arguments, and has an optional three more.

Required:

  • pixel_object: The pixel object, e.g. pixels.
  • speed: The refresh rate of the rainbow in seconds, e.g. 0.1.

Optional:

  • period: The period over which to cycle the rainbow in seconds, e.g. 2. Defaults to 5 if no period is provided.
  • step: The steps to take through the colorwheel (0-255). A step of 1 means cycling through the entire colorwheel, a step of 2 means it cycles through every other possible value. Defaults to 1 if no step is provided.
  • precompute_rainbow: Precompute the rainbow which increases its speed, but uses more memory. Set to False to disable if you are running into memory limitations. Defaults to True.

Once created, you display the animation.

Download: file
rainbow = Rainbow(pixels, speed=0.1, period=2)

while True:
    rainbow.animate()

That's all there is to displaying a rainbow using the LED Animation library! Let's take a look at the next animation.

RainbowChase

This is a rainbow version of the theatre marquee type chase animation, with definable length of lit LEDs and the dark gap between lit LEDs.

First, you import the RainbowChase module. See Import and Setup for the rest of the necessary imports and pixel object creation.

Download: file
from adafruit_led_animation.animation.rainbowchase import RainbowChase

Next you create the RainbowChase animation. RainbowChase requires two arguments, and has an optional five more.  You'll likely want to at least specify size and spacing.

Required:

  • pixel_object: The pixel object, e.g. pixels.
  • speed: The refresh rate of the rainbow in seconds, e.g. 0.1.

Optional:

  • size: The number of pixels to turn on in a row, e.g. 3. Defaults to 2 if no size is provided.
  • spacing: The number of pixels to turn off in a row, e.g. 6. Defaults to 3 if no size is provided.
  • reverse: Optionally reverses the movement of the animation. Set to True to enable. Defaults to False.
  • step: The steps to take through the colorwheel (0-255). A step of 1 means cycling through the entire colorwheel, a step of 2 means it cycles through every other possible value. Defaults to 8 if no step is provided.

Once created, you display the animation.

Download: file
rainbow_chase = RainbowChase(pixels, speed=0.1, size=5, spacing=3)

while True:
    rainbow_chase.animate()

That's all there is to displaying a rainbow theatre chase animation using the LED Animation library! Let's take a look at the next animation.

RainbowComet

This is a rainbow version of a comet of a specified speed, with a dimming tail of specified length.

First, you import the RainbowComet module. See Import and Setup for the rest of the necessary imports and pixel object creation.

Download: file
from adafruit_led_animation.animation.rainbowcomet import RainbowComet

Next you create the RainbowComet animation. RainbowComet requires two arguments, and has an optional four more. You'll likely want to specify at least tail_length.

Required:

  • pixel_object: The pixel object, e.g. pixels.
  • speed: The speed of the comet in seconds, e.g. 0.1.

Optional:

  • tail_length: The length of the comet in pixels. Defaults to 25% of the length of the pixel_object if no length is provided. Automatically compensates for a minimum length of 2 and a maximum of the length of the pixel_object.
  • reverse: Optionally reverses the movement of the animation. Set to True to enable. Defaults to False.
  • bounce: Optionally "bounces" the comet along the strip by displaying it from the beginning of the strip to the end, and then reversing the movement once it reaches the end of the strip. Set to True to enable. Defaults to False.
  • colorwheel_offset: Offset from the start of the colorwheel. Provide a value of 0-255 where 0 is red, 85 is blue and 170 is green, wrapping back to 255 being red. Defaults to 0.

Once created, you display the animation.

Download: file
rainbow_comet = RainbowComet(pixels, speed=0.1, tail_length=7, bounce=True)

while True:
    rainbow_comet.animate()

RainbowSparkle

This is a shifting rainbow that sparkles.

First, you import the RainbowSparkle module. See Import and Setup for the rest of the necessary imports and pixel object creation.

Download: file
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle

Next you create the RainbowSparkle animation. RainbowSparkle requires two arguments, and has an optional five more.

Required:

  • pixel_object: The pixel object, e.g. pixels.
  • speed: The refresh rate of the rainbow in seconds, e.g. 0.1.

Optional:

  • period: The period over which to cycle the rainbow in seconds, e.g. 2. Defaults to 5 if no period is provided.
  • num_sparkles: The number of sparkles. Defaults to 5% of the length of the pixel_object.
  • step: The steps to take through the colorwheel (0-255). A step of 1 means cycling through the entire colorwheel, a step of 2 means it cycles through every other possible value. Defaults to 1 if no step is provided.
  • precompute_rainbow: Precompute the rainbow which increases its speed, but uses more memory. Set to False to disable if you are running into memory limitations. Defaults to True.

Once created, you display the animation.

Download: file
rainbow_sparkle = RainbowSparkle(pixels, speed=0.1, num_sparkles=15)

while True:
    rainbow_sparkle.animate()

That's how to display a rainbow sparkle animation using the LED Animation library! Next up: sparkles!

Full Example Code

This example runs all the rainbow animations in sequence.

"""
This example uses AnimationsSequence to display multiple animations in sequence, at a five second
interval.

For NeoPixel FeatherWing. Update pixel_pin and pixel_num to match your wiring if using
a different form of NeoPixels.

This example does not work on SAMD21 (M0) boards.
"""
import board
import neopixel

from adafruit_led_animation.animation.rainbow import Rainbow
from adafruit_led_animation.animation.rainbowchase import RainbowChase
from adafruit_led_animation.animation.rainbowcomet import RainbowComet
from adafruit_led_animation.animation.rainbowsparkle import RainbowSparkle
from adafruit_led_animation.sequence import AnimationSequence

# Update to match the pin connected to your NeoPixels
pixel_pin = board.D6
# Update to match the number of NeoPixels you have connected
pixel_num = 32

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)

rainbow = Rainbow(pixels, speed=0.1, period=2)
rainbow_chase = RainbowChase(pixels, speed=0.1, size=5, spacing=3)
rainbow_comet = RainbowComet(pixels, speed=0.1, tail_length=7, bounce=True)
rainbow_sparkle = RainbowSparkle(pixels, speed=0.1, num_sparkles=15)


animations = AnimationSequence(
    rainbow,
    rainbow_chase,
    rainbow_comet,
    rainbow_sparkle,
    advance_interval=5,
    auto_clear=True,
)

while True:
    animations.animate()

This guide was first published on May 27, 2020. It was last updated on May 27, 2020.

This page (Rainbows) was last updated on Nov 06, 2020.