The key feature of Operation is that the lights and buzzers alarm whenever your tweezers touch the edge of a hole. We've connected the copper tape at each hole to a capacitive touch pad on the Circuit Playground Express -- now we want to code the board in CircuitPython to react.
CircuitPython Setup
To get started, you'll want to set up the Circuit Playground Express for use with CircuitPython by following this guide. When you're ready, and can upload code to the board return here.
Adafruit really likes using the Mu editor to edit the CircuitPython code. See this guide on loading and using Mu.
Code
You can copy the code here and then paste it into Mu. Save it to your Circuit Playground Express as code.py
# SPDX-FileCopyrightText: 2018 John Park for Adafruit Industries # # SPDX-License-Identifier: MIT # # Adabot Operation Game # CPX, alligator clips, copper tape, tweezers, surgery, and fun! import board import touchio from adafruit_circuitplayground.express import cpx # import time # uncomment if testing raw read values cap_pins = (board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.A7) touch_pads = [] for i in range(7): touch_pads.append(touchio.TouchIn(cap_pins[i])) for touch_pad in touch_pads: touch_pad.threshold = 3500 # adjust value to fine-tune touch threshold MAGENTA = (10, 0, 10) VIOLET = (5, 0, 15) BLUE = (0, 0, 20) CYAN = (0, 10, 10) GREEN = (0, 20, 0) YELLOW = (10, 10, 0) ORANGE = (15, 5, 0) RED = (20, 0, 0) WHITE = (3, 3, 3) COLORS = [MAGENTA, VIOLET, BLUE, CYAN, GREEN, YELLOW, ORANGE, RED, WHITE] cpx.pixels.fill(WHITE) while True: for i in range(7): # uncomment block to check the raw touch pad values # print("raw %s value for pad " % i) # print(touch_pads[i].raw_value) # time.sleep(.5) if touch_pads[i].value: # print("Touched %s" % i) # uncomment for debugging cpx.pixels.fill(RED) cpx.play_tone(660, 0.7) cpx.pixels.fill(COLORS[i])
Code Breakdown
Libraries
Here's what's going on in the code. First, we import the board, touchio,
and adafruit_circuitplayground.express
libraries. These give us the commands we need to address the board and it's capacitive touch pads, as well as the NeoPixels by using the cpx
commands.
(Note that the time
library import is commented out, you will uncomment this if later you decide to check your raw touch values, which is covered later.)
import board import touchio from adafruit_circuitplayground.express import cpx # import time # uncomment if testing raw read values
Lists and Touchpads
Next, we'll create a list of the board's cap touch pins and initialize them as touchio touch pads. And, we'll set the touch threshold to 3500. This can be tuned later.
cap_pins = (board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.A7) touch_pads = [] for i in range(7): touch_pads.append(touchio.TouchIn(cap_pins[i])) for touch_pad in touch_pads: touch_pad.threshold = 3500
Color Lists
We'll create a set of variables for different colors and their values. Then, we'll create a list of those colors called ... COLORS. This way we can associate an index number for each touch pad to a color in the list.
MAGENTA = (10, 0, 10) VIOLET = ( 5, 0, 15) BLUE = ( 0, 0, 20) CYAN = ( 0, 10, 10) GREEN = ( 0, 20, 0) YELLOW = (10, 10, 0) ORANGE = (15, 5, 0) RED = (20, 0, 0) WHITE = ( 3, 3, 3) COLORS = [MAGENTA, VIOLET, BLUE, CYAN, GREEN, YELLOW, ORANGE, RED, WHITE]
NeoPixel Fill
We'll then fill all of the on-board NeoPixels white with this command:
cpx.pixels.fill(WHITE)
The Loop
Now that everything is set up, we have the main loop of the program. What happens here is that we'll iterate through each of the seven pads, checking to see if their value is above the threshold. If it is, we fill the NeoPixels red, play a buzzer sound, and then fill the pixels to the color associated with that pad.
while True: for i in range(7): # uncomment this block to check the raw touch pad values # print("raw %s value for pad " % i) # print(touch_pads[i].raw_value) # time.sleep(.5) if touch_pads[i].value: print("Touched %s" % i) cpx.pixels.fill(RED) cpx.play_tone(660, 0.7) cpx.pixels.fill(COLORS[i])
Threshold Tuning
Note the section in the code above that is commented out. If you uncomment it by removing the leading pound symbols (#
) you can watch the raw touch values being printed in the Mu REPL or other serial terminal connected to your Circuit Playground Express. Watch for the typical values when not being touched and then the values when they are touched.
If, say, the pads typically read around 2000 but then shoot up to 4000 when you touch them with your tweezers, you could set the threshold to 3500.
With the code uploaded to the board, you're ready to play!
Page last edited January 22, 2025
Text editor powered by tinymce.