Skip to content

Commit 0ac963a

Browse files
committed
Adding basic animation code.
1 parent 3124c4f commit 0ac963a

File tree

1 file changed

+103
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)