This lesson is a spin off of John Park's Drum Machine.
We can use the capacitive touch pads on the Circuit Playground Express as triggers and small .wav files for our drum sounds!
Until now, we needed to code in CircuitPython to play sound files. Now with EduBlocks, all we need to do is drag a couple of blocks and we're good to go!
First, download the .zip file below, which contains all of the drum samples we'll be using. Save the file to your desktop or somewhere else easy to find, and then unzip it.
You can plug in your Circuit Playground Express, and then drag the drum files onto it. It shows up as the CIRCUITPY drive.
- Import
time, board, touchio, digitalio,
andcpx
. - Create a variable called
BPM
and set it to.125
. This is how fast the drum sounds will play in between each other. In John Park's project the BPM is set to 120 then later on divided by 960 to get the time in seconds that 120 beats per minute is. Unfortunately some math functions are not available in EduBlocks as it is in beta, so we must hard code the value of 120/960 in for our BPM variable. This happens to be .125. Feel free to mess around with this number to try out different drum speeds.
- Create 7 variables named
touch1, touch2, touch3
, ... etc. - Drag in 7
=touchioTouchIn()
blocks fromTouch
. - Via the drop down menus change to
touch1, touch2, touch3
, ... etc. - Each block should =
(board.A1), (board.A2), (board.A3)
, ... etc.
- Drag in a
while: True
loop. - Next drag in an
if
statement with the condition set astouch1.value
(this block can be found inTouch
). - From
CPX Easy
, drag incpx.play_file()
and type bd_tek.wav in for the value. - Drag in
time.sleep()
block. - Drag variable
BPM
in astime.sleep()
value. - Repeat above steps until all 7 sound files are accounted for.
Download the main.py file and drag to the CPX.
Try it out!
Sounds should be triggering when each coin is touched or held!
If you want to use your own sound files, you can! Record, sample, remix, or simply download files from a sound file sight, such as freesample.org. Then, to make sure you have the files converted to the proper specifications, check out this guide here that'll show you how! Spoiler alert: you'll need to make a small, 22Khz (or lower), 16 bit PCM, mono .wav file!
Want to listen to your Drum Machine at body movin' volumes? No problem! Hook up an 1/8" or 1/4" phono output to the GND and A0 pads, then plug in to an amp or speaker! I tried it on a small speaker with an aux input and it sounded great!
Here are the code files if you want to upload directly to EduBlocks or the CPX.
touch1 = None touch2 = None touch3 = None touch4 = None touch5 = None touch6 = None touch7 = None BPM = None import time import board import touchio from digitalio import * from adafruit_circuitplayground.express import cpx BPM = .125 touch1 = touchio.TouchIn(board.A1) touch2 = touchio.TouchIn(board.A2) touch3 = touchio.TouchIn(board.A3) touch4 = touchio.TouchIn(board.A4) touch5 = touchio.TouchIn(board.A5) touch6 = touchio.TouchIn(board.A6) touch7 = touchio.TouchIn(board.A7) while True: if touch1.value: cpx.play_file("bd_tek.wav") time.sleep(BPM) elif touch2.value: cpx.play_file("elec_hi_snare.wav") time.sleep(BPM) elif touch3.value: cpx.play_file("elec_cymbal.wav") time.sleep(BPM) elif touch4.value: cpx.play_file("elec_blip2.wav") time.sleep(BPM) elif touch5.value: cpx.play_file("bd_tek.wav") time.sleep(BPM) elif touch6.value: cpx.play_file("bass_hit_c.wav") time.sleep(BPM) elif touch7.value: cpx.play_file("drum_cowbell.wav") time.sleep(BPM)