Skip to content

Commit e49e384

Browse files
authored
Merge pull request adafruit#1267 from kattni/qt-py-neopixels
Basic animations for NeoPixels on QT Py.
2 parents 3124c4f + fbe306b commit e49e384

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""Basic NeoPixel LED animations for the QT Py."""
2+
import time
3+
import board
4+
import neopixel
5+
import adafruit_pypixelbuf
6+
7+
# Update this to match the pin to which you connected the NeoPixels
8+
pixel_pin = board.A3
9+
# Update this to match the number of NeoPixels connected
10+
num_pixels = 30
11+
12+
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, auto_write=False)
13+
# Set to 0-1 to change the brightness of the NeoPixels
14+
pixels.brightness = 0.2
15+
16+
17+
def blink(color, wait):
18+
"""Blink animation. Blinks all pixels."""
19+
pixels.fill(color)
20+
pixels.show()
21+
time.sleep(wait)
22+
pixels.fill((0, 0, 0))
23+
pixels.show()
24+
time.sleep(wait)
25+
26+
27+
def chase(color, spacing=3, iteration_step=1):
28+
"""Theatre chase animation. Chases across all pixels."""
29+
if spacing < 2:
30+
raise ValueError("Spacing must be greater than 1 to show chase pattern.")
31+
32+
# Use modulo division to create the spacing between pixels.
33+
chase_pixel = iteration_step % spacing
34+
# Loop over pixels and turn on expected pixels to provided color.
35+
for pixel in range(0, len(pixels), spacing):
36+
# If the pixel is outside the total pixel range, break.
37+
if pixel + chase_pixel > len(pixels) - 1:
38+
break
39+
pixels[pixel + chase_pixel] = color
40+
pixels.show()
41+
42+
# Loop over pixels and turn off expected pixels.
43+
for pixel in range(0, len(pixels), spacing):
44+
# If the pixel is outside the total pixel range, break.
45+
if pixel + chase_pixel > len(pixels) - 1:
46+
break
47+
pixels[pixel + chase_pixel] = (0, 0, 0)
48+
49+
50+
def color_wipe(color, wait):
51+
"""Color wipe animation. Wipes across all pixels."""
52+
for pixel in range(num_pixels):
53+
pixels[pixel] = color
54+
time.sleep(wait)
55+
pixels.show()
56+
time.sleep(0.5)
57+
58+
59+
def rainbow_cycle(wait):
60+
"""Rainbow cycle animation. Cycles across all pixels."""
61+
for color_index in range(255):
62+
for pixel in range(num_pixels):
63+
pixel_index = (pixel * 256 // num_pixels) + color_index
64+
pixels[pixel] = adafruit_pypixelbuf.colorwheel(pixel_index & 255)
65+
pixels.show()
66+
time.sleep(wait)
67+
68+
69+
RED = (255, 0, 0)
70+
YELLOW = (255, 150, 0)
71+
GREEN = (0, 255, 0)
72+
CYAN = (0, 255, 255)
73+
BLUE = (0, 0, 255)
74+
PURPLE = (180, 0, 255)
75+
76+
while True:
77+
# Blink 5 times. Increase or decrease the range for more or less blinking.
78+
for blinks in range(5):
79+
blink(RED, 0.5) # Increase number to slow down blinking, decrease to speed up.
80+
81+
# Chase. Increase or decrease the range for longer or shorter chase animation.
82+
for step in range(50):
83+
chase(PURPLE, spacing=4, iteration_step=step)
84+
time.sleep(0.05)
85+
86+
# Fill all pixels.
87+
pixels.fill(RED)
88+
pixels.show()
89+
# Increase or decrease the time to change the speed of the solid color change in seconds.
90+
time.sleep(0.5)
91+
pixels.fill(GREEN)
92+
pixels.show()
93+
time.sleep(0.5)
94+
pixels.fill(BLUE)
95+
pixels.show()
96+
time.sleep(0.5)
97+
98+
# Color wipe.
99+
color_wipe(YELLOW, 0.01) # Increase the number to slow down the color chase.
100+
color_wipe(CYAN, 0.01)
101+
color_wipe(PURPLE, 0.01)
102+
103+
# Rainbow cycle.
104+
rainbow_cycle(0) # Increase the number to slow down the rainbow.

0 commit comments

Comments
 (0)