EasyManua.ls Logo

Adafruit ESP32-S3 - Page 186

Adafruit ESP32-S3
263 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Then, you specify the button pin, the number of LEDs in each NeoPixel ring, and the
LED brightness.
button_pin = board.BUTTON
num_pixels = 16
brightness = 0.2
Next you set up the two NeoPixel rings on pins A1 and A2 , using the number of
pixels and brightness specified above, and setting auto_write=False .
ring_one = neopixel.NeoPixel(board.A1, num_pixels, brightness=brightness,
auto_write=False)
ring_two = neopixel.NeoPixel(board.A2, num_pixels, brightness=brightness,
auto_write=False)
Following set up, you create a class called AnimationControls . This class provides
ways to control the animations with asyncio.
class AnimationControls:
def __init__(self):
self.reverse = False
self.wait = 0.0
self.delay = 0.5
Then, you have the rainbow and blink animation code. This is where the asyncio-
specific code begins.
In terms of the animation parts of the code, the first function is the rainbow cycle
animation code. This is pretty standard except for the second line of code. In this
example, the line beginning with for j in includes non-standard code for the
rainbow cycle in reverse: range(255, -1, -1) if controls.reverse , followed
by the standard forward rainbow cycle code - range(0, 256, 1) .
async def rainbow_cycle(controls):
"""Rainbow cycle animation on ring one."""
while True:
for j in range(255, -1, -1) if controls.reverse else range(0, 256, 1):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
ring_one[i] = colorwheel(rc_index & 255)
ring_one.show()
await asyncio.sleep(controls.wait)
The second function is the blink animation code. This is typical. You fill all the
NeoPixel LEDs blue, delay for a specified amount of time, then turn all of the LEDs off,
and delay for the same specified amount of time.
©Adafruit Industries Page 186 of 263

Table of Contents

Related product manuals