Import the Libraries

The code begins by importing the libraries and the colors that Frank's NeoPixels will be able to switch between.

import time
import board
import touchio
import neopixel
from adafruit_led_animation.animation.pulse import Pulse
from adafruit_led_animation.color import (
    RED,
    YELLOW,
    ORANGE,
    GREEN,
    TEAL,
    CYAN,
    BLUE,
    PURPLE,
    MAGENTA,
    GOLD,
    PINK,
    AQUA,
    JADE,
    AMBER
)

Setup the NeoPixels

The NeoPixels are setup, along with the Pulse LED animation.

#  NeoPixel pin
pixel_pin = board.A3
#  number of NeoPixels
pixel_num = 68

#  NeoPixels setup
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False)

#  animation setup
pulse = Pulse(pixels, speed=0.1, color=RED, period=5)

Setup Capacitive Touch

Capacitive touch is setup for pins A1 and A2.

#  two cap touch pins
touch_left = board.A1
touch_right = board.A2

#  cap touch setup
bolt_left = touchio.TouchIn(touch_left)
bolt_right = touchio.TouchIn(touch_right)

Array of Colors

The colors array is created to hold all of the NeoPixel color options. This array will be accessed in the loop to change the NeoPixel color.

#  NeoPixel colors for animation
colors = [RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE,
          PURPLE, MAGENTA, GOLD, PINK, AQUA, JADE, AMBER]

Variables and States

c is setup to be the index variable for the colors array. Its value will be updated in the loop to change the NeoPixel color.

bolt_left_state and bolt_right_state are setup to debounce the capacitive touch inputs.

#  variable for color array index
c = 0

#  debounce states for cap touch
bolt_left_state = False
bolt_right_state = False

The Loop

The loop begins by running the Pulse animation and setting up debouncing for the capacitive touch inputs. The adafruit_led_animation library allows for the animation to run uninterrupted in the background in the loop while other processes can take place.

while True:
    #  run animation
    pulse.animate()

    #  debounce for cap touch
    if not bolt_left.value and not bolt_left_state:
        bolt_left_state = True
    if not bolt_right.value and not bolt_right_state:
        bolt_right_state = True

Capacitive Touch Inputs

The capacitive touch inputs affect the value of c. If Frank's left bolt is touched, then c increases by 1. If Frank's right bolt is touched, then c decreases by 1.

#  if the left bolt is touched...
    if bolt_left.value and bolt_left_state:
        print("Touched left bolt!")
        #  increase color array index by 1
        c += 1
        #  reset debounce state
        bolt_left_state = False
    #  if the right bolt is touched...
    if bolt_right.value and bolt_right_state:
        print("Touched right bolt!")
        #  decrease color array index by 1
        c -= 1
        #  reset debounce state
        bolt_right_state = False

Tracking the Color

c is tracking the index of the colors array. There are 14 colors total. If c goes above that range, it is reset to 0. If c goes below 0, it is reset to 13. This allows you to loop through the colors continuously.

#  if the color array index is bigger than 13...
    if c > 13:
        #  reset it to 0
        c = 0
    #  if the color array index is smaller than 0...
    if c < 0:
        #  reset it to 13
        c = 13

Update the Color

Finally, the color of the NeoPixels is updated with pulse.color. You can access the animation's other parameters in the loop by using pulse.[parameter], for example: pulse.speed, pulse.period, etc.

#  update animation color to current array index
    pulse.color = colors[c]
    time.sleep(0.01)

This guide was first published on Oct 27, 2020. It was last updated on Oct 27, 2020.

This page (CircuitPython Code Walkthrough) was last updated on Oct 27, 2020.

Text editor powered by tinymce.