// Keyboard variables
long keys[13]; // Contains latest capacitive sense reading fo
r each key
int threshold = 25; // Threshold for key press readings, a ke
y plays sound if its equal to or greater than the threshold
float octave = 1.0; // Stores what octave/multiplier the key p
resses use, change with potentiometer
// Declaring a capactive sensor for each key on the keyboard
CapacitiveSensor CapSensors[13] =
{
CapacitiveSensor(2,3),
CapacitiveSensor(2,4),
...
...
The variable array
keys
is used to store the latest capacitive touch reading
for each key on the keyboard. The higher the number for a key, the harder
it’s being pressed. If a particular key exceeds the
threshold
value (found
experimentally), then the corresponding note for that key is played (unless
another key takes precedence, explained later). Each time a note is played,
the frequency is multiplied by the
octave
variable, which is set by the
potentiometer.
CapSensors
is an array that declares a capacitive touch
sensor for each of the keys. All the keys use the same send pin, but each
key’s pad is connected to a different pin.
CapacitiveSensor(2,3)
indicates
2 is the “send pin,” while 3 is the pin connected to the key’s pad. This
particular sensor is for the low C key, and all the keys are listed in order
from lowest to highest frequency. We will use each sensor in this array to
detect if a key is being pressed.
Our
setup()
function is quite simple:
void setup()
{
// Setup button pin (PB6) as input (not a default Arduino pi
n)
DDRB &= ~(1 << 6); // Set bit six of the data direction regi
ster b to 0
// Blink LED to indicate keyboard is ready for use
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
delay(500);
digitalWrite(led,LOW);
}
We initialize a button using AVR style code, since the button is not on an
official Arduino digital pin. We then set the LED as an output and blink it for
half a second to indicate to the user that the program is ready.
Our
loop()
function is where the core of the sketch lives.
Page 6 of 8