Tutorial 03. Capacitive touch control
|
The next of our user input devices are the eight capacitive touch pads. These function like buttons; they can detect when you place your finger on them and when you remove your finger. This technology is actually pretty common in modern appliances! Take a look at the buttons on any recently made toaster, microwave or laundry machine. |
![]() |
- Again we will begin with our code from Tutorial 01. Now instead of using a potentiometer to control our oscillator's pitch, we will use the pads as a keyboard to play an A major scale.
- We should be pretty familiar with
my_sine.setFreq()at this point. We just need to figure out where in the code to change this frequency and what frequency to change it to. To explicitly state our goal, we want each of the touch pads to change our oscillator to play a note from the A major scale as follows: - When MEAP runs
meap.readInputs(), if it detects that a pad has been touched or released, it will callupdateTouch(). When this happens, you are given two variables that describe the event:- number tells you which pad has been pressed or released. The pads are numbered 0-7, starting with 0 in the top left, increasing across the top row and then continuing in the bottom row
- pressed is a boolean
which is
truewhen a pad was pressed andfalsewhen it is released.
The
updateTouch()function gives you an outline with sections to add code to when specific pads are pressed or released. By default, it will print to the Serial monitor, which pad was pressed or released. If you want something else to happen in that event, add the relevent code directly after the correspondingSerial.println()statement. To start with, lets have the first pad set the frequency of our sine wave to A3 which is 220Hz.
To do this, add the following code after the line that says
Serial.println("t0 pressed ");my_sine.setFreq(220);This will set the sine's frequency to 220Hz whenever touch pad 0 is pressed.
- Now we just have to fill in the rest the same way! You can use a note to frequency chart to find the frequencies of the rest of the notes listed above and set the frequency of my_sine in the corresponding block of updateControl() for each one.
- Once you have done this, upload your code. You should be able to play the touch pads like a keyboard.
____ ____ ____ ____
/ \ / \ / \ / \
| A3 | | B3 | | C#4| | D4 |
\____/ \____/ \____/ \____/
____ ____ ____ ____
/ \ / \ / \ / \
| E4 | | F#4| | G#4| | A4 |
\____/ \____/ \____/ \____/
