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.

# Importing Libraries and Defining Variables:

 Import `time, board, touchio, digitalio,` and `cpx`. 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 from `Touch`. Via the drop down menus change to `touch1, touch2, touch3`, ... etc. Each block should = `(board.A1), (board.A2), (board.A3)`, ... etc.

# Trigger the Sounds

 Drag in a `while: True` loop. Next drag in an `if` statement with the condition set as `touch1.value` (this block can be found in `Touch`). From `CPX Easy`, drag in `cpx.play_file()`and type bd_tek.wav in for the value. Drag in`time.sleep()` block. Drag variable `BPM` in as `time.sleep()` value. Repeat above steps until all 7 sound files are accounted for.

# Attach Conductive Materials with Alligator Clips to Capacitive Touch Pads:

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.

# CircuitPython Code:

```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 *
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)```

This guide was first published on Feb 05, 2019. It was last updated on Feb 05, 2019.

This page (Lesson 3: Drum Machine!) was last updated on Dec 04, 2021.