Circuit Playground Express Rainbow - Adafruit Feather M4 Express Manual

Table of Contents

Advertisement

The last example shows how to do a rainbow animation on the internal RGB LED.
Copy and paste the code into code.py using your favorite editor, and save the file. Remember to comment and
uncomment the right lines for the board you're using, as
import time
import board
# For Trinket M0, Gemma M0, ItsyBitsy M0 Express and ItsyBitsy M4 Express
import adafruit_dotstar
led = adafruit_dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1)
# For Feather M0 Express, Metro M0 Express, Metro M4 Express and Circuit Playground Express
# import neopixel
# led = neopixel.NeoPixel(board.NEOPIXEL, 1)
def wheel(pos):
# Input a value 0 to 255 to get a color value.
# The colours are a transition r - g - b - back to r.
if pos < 0 or pos > 255:
return 0, 0, 0
if pos < 85:
return int(255 - pos * 3), int(pos * 3), 0
if pos < 170:
pos -= 85
return 0, int(255 - pos * 3), int(pos * 3)
pos -= 170
return int(pos * 3), 0, int(255 - (pos * 3))
led.brightness = 0.3
i = 0
while True:
i = (i + 1) % 256
led.fill(wheel(i))
time.sleep(0.1)
We add the
function in after setup but before our main loop.
wheel
And right before our main loop, we assign the variable
The main loop contains some math that cycles
cycle
through the rainbow!
wheel()
The
determines the speed at which the rainbow changes. Try a higher number for a slower rainbow or a
time.sleep()
lower number for a faster one!

Circuit Playground Express Rainbow

Note that here we use
led.fill
one. So why bother with
NeoPixel LEDs built in. The examples so far have only turned on the first one. If you'd like to do a rainbow on all ten
LEDs, change the
in:
1
© Adafruit Industries
# run from 0 to 255
i
instead of
. This means it turns on all the LEDs, which in the current code is only
led[0]
? Well, you may have a Circuit Playground Express, which as you can see has TEN
fill
https://learn.adafruit.com/adafruit-feather-m4-express-atsamd51
explained above
, so it's ready for use inside the loop.
i = 0
from
to
and around again repeatedly. We use this value to
0
255
(https://adafru.it/Bel).
Page 131 of 183

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the Feather M4 Express and is the answer not in the manual?

Subscribe to Our Youtube Channel

Table of Contents

Save PDF