# CircuitPython Neopixel with button switch example by DJDevon3
# On button press runs through neopixels animations then stops
import board
import time
import keypad
from digitalio import DigitalInOut, Direction, Pull
from rainbowio import colorwheel
import neopixel
# Configure your switch GPIO pin here
keys = keypad.Keys((board.GP13,), value_when_pressed=False, pull=True)
# Other leg of the key switch should go to GND
led1_pin = board.GP2 # COMMON CATHODE LED not a neopixel visual notification of keypress
# Other leg of the LED should go to GND
pixel_pin = board.GP27 # Neopixel(s) data on this pin
led1 = DigitalInOut(led1_pin)
led1.direction = Direction.OUTPUT
num_pixels = 16
pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=1, auto_write=False)
def color_chase(color, wait):
for i in range(num_pixels):
pixels[i] = color
time.sleep(wait)
pixels.show()
time.sleep(0.5)
def rainbow_cycle(wait):
for j in range(255):
for i in range(num_pixels):
rc_index = (i * 256 // num_pixels) + j
pixels[i] = colorwheel(rc_index & 255)
pixels.show()
time.sleep(wait)
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
INDEX = 0 # counter
while True:
event = keys.events.get()
# event will be None if nothing has happened.
time.sleep(.125)
led1.value = False
INDEX = 0
if event:
if not event.pressed: # False in it's on state
print(event)
led1.value = True
time.sleep(.125)
led1.value = False
INDEX += 1
if INDEX is 1:
print("Animation 1")
pixels.fill(RED)
pixels.show()
time.sleep(1) # Increase to slow down solid color changes
pixels.fill(GREEN)
pixels.show()
time.sleep(1)
pixels.fill(BLUE)
pixels.show()
time.sleep(1)
INDEX += 1
if INDEX is 2:
print("Animation 2")
color_chase(RED, 0.1) # Increase to slow down color chase
color_chase(YELLOW, 0.1)
color_chase(GREEN, 0.1)
color_chase(CYAN, 0.1)
color_chase(BLUE, 0.1)
color_chase(PURPLE, 0.1)
INDEX += 1
if INDEX is 3:
print("Animation 3")
rainbow_cycle(0) # Increase to slow down rainbow
INDEX += 1
if INDEX > 3:
INDEX = 0