/ NeoPixel_Frankenstein / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Liz Clark for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import touchio 8 import neopixel 9 from adafruit_led_animation.animation.pulse import Pulse 10 from adafruit_led_animation.color import ( 11 RED, 12 YELLOW, 13 ORANGE, 14 GREEN, 15 TEAL, 16 CYAN, 17 BLUE, 18 PURPLE, 19 MAGENTA, 20 GOLD, 21 PINK, 22 AQUA, 23 JADE, 24 AMBER 25 ) 26 27 # NeoPixel pin 28 pixel_pin = board.A3 29 # number of NeoPixels 30 pixel_num = 68 31 32 # NeoPixels setup 33 pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False) 34 35 # animation setup 36 pulse = Pulse(pixels, speed=0.1, color=RED, period=5) 37 38 # two cap touch pins 39 touch_left = board.A1 40 touch_right = board.A2 41 42 # cap touch setup 43 bolt_left = touchio.TouchIn(touch_left) 44 bolt_right = touchio.TouchIn(touch_right) 45 46 # NeoPixel colors for animation 47 colors = [RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, 48 PURPLE, MAGENTA, GOLD, PINK, AQUA, JADE, AMBER] 49 50 # variable for color array index 51 c = 0 52 53 # debounce states for cap touch 54 bolt_left_state = False 55 bolt_right_state = False 56 57 while True: 58 # run animation 59 pulse.animate() 60 61 # debounce for cap touch 62 if not bolt_left.value and not bolt_left_state: 63 bolt_left_state = True 64 if not bolt_right.value and not bolt_right_state: 65 bolt_right_state = True 66 67 # if the left bolt is touched... 68 if bolt_left.value and bolt_left_state: 69 print("Touched left bolt!") 70 # increase color array index by 1 71 c += 1 72 # reset debounce state 73 bolt_left_state = False 74 # if the right bolt is touched... 75 if bolt_right.value and bolt_right_state: 76 print("Touched right bolt!") 77 # decrease color array index by 1 78 c -= 1 79 # reset debounce state 80 bolt_right_state = False 81 # if the color array index is bigger than 13... 82 if c > 13: 83 # reset it to 0 84 c = 0 85 # if the color array index is smaller than 0... 86 if c < 0: 87 # reset it to 13 88 c = 13 89 # update animation color to current array index 90 pulse.color = colors[c] 91 time.sleep(0.01)