Skip to content

Instantly share code, notes, and snippets.

@gallaugher
Created July 24, 2024 17:14
Show Gist options
  • Save gallaugher/e5909c741d01bb132d105f8d9a354537 to your computer and use it in GitHub Desktop.
Save gallaugher/e5909c741d01bb132d105f8d9a354537 to your computer and use it in GitHub Desktop.
Capacitive Touch CircuitPython
# capacitive-touch.py
import board, neopixel, time, digitalio, touchio
from adafruit_led_animation.color import RED, YELLOW, ORANGE, \
GREEN, TEAL, CYAN, BLUE, PURPLE, MAGENTA, GOLD, PINK, AQUA, \
JADE, AMBER, OLD_LACE, WHITE, BLACK
from audiocore import WaveFile
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
print("This board does not support audio")
# Set up our 10 NeoPixels on our board and call them "pixels"
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10)
colors = [RED, YELLOW, ORANGE, GREEN, TEAL, CYAN, BLUE, PURPLE,
MAGENTA, GOLD, PINK, AQUA, JADE, AMBER, OLD_LACE, WHITE, BLACK]
# Setup capacitive touch pads
touchpad_A1 = touchio.TouchIn(board.A1)
touchpad_A2 = touchio.TouchIn(board.A2)
touchpad_A3 = touchio.TouchIn(board.A3)
# Audio setup for CircuitPlayground boards - can skip if only using external speaker
speaker = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker.direction = digitalio.Direction.OUTPUT
speaker.value = True
# use speaker location to creat an AudioOut object named audio
audio = AudioOut(board.SPEAKER)
# Setup path & filenames
path = "drumSounds/"
def play_sound(filename):
with open(path + filename, "rb") as wave_file: # creates variable wave_file with data from file
wave = WaveFile(wave_file) # converts data into a playable WaveFile
audio.play(wave) # use audio object to play the WaveFile
while audio.playing:
pass # wait 'til audio is done
while True:
if touchpad_A1.value:
pixels.fill(RED)
play_sound("drum_cowbell.wav")
pixels.fill(BLACK) # will turn off after sound is done playing
if touchpad_A2.value:
pixels.fill(ORANGE)
play_sound("bd-zome.wav")
pixels.fill(BLACK) # will turn off after sound is done playing
if touchpad_A3.value:
pixels.fill(YELLOW)
play_sound("elec_hi_snare.wav")
pixels.fill(BLACK) # will turn off after sound is done playing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment