Touch Sensors For Keyboard

This is a very long post and I apologize for its length.

I am trying to make a keyboard of sorts. Initially I tryed to use the code for the fabric piano in the Sew Electric book which uses a LilyPad Arduino Simple Snap. Last year I used the code to make a sound game and it worked great. Instead of the LilyPad Simple Snap, I used the LilyPad mainboard. Conductive thread is used to make the connections and the “key” or what I call touch pads are made that a person touches to make a tone sound. The YouTube video link for this game is Sound Game Part 1 - YouTube. Start at about the 3 ½ minute mark to hear the tones and skip the explanation.

I tried using the same code with the Arduino Uno for a college project, but it buzzes rather than make tones. The sound does change for each key/touch pad according the changes in the frequencies, but they are not tones as in the sound game, they are buzz sounds like a buzzer. One of the professors looked at the project and coding. We found that when only one key/touch pad is coded, it makes a tone sound and not a buzzer sound. As keys are added to the code, the tone becomes more buzzer sounding with each additional key. He didn’t know why and suggested using an array. The Sew Electric book starts with the long version of code and then simplifies to an array. The array didn’t make a difference. Here the code which also uses a capacitive sensing code which I downloaded from Capacitive Sensing Code

/* #include "pitches.h */

const int numberOfKeys = 8;
int keys[numberOfKeys] = {4, 5, 6, 7, 8, 9, 10, 11};
int notes[numberOfKeys] {1046,1175,1319, 1397, 1568, 1760, 1976, 2093};

int speaker2=2;   //name of the speaker key
int speaker3=3;   //name of the speaker key
int touchValue;  //will store sensor reading


void setup() {
  pinMode(keys[0], INPUT);      //set key4 to input - NOTE_C4
  pinMode(keys[1], INPUT);      //set key5 to input - NOTE_D4
  pinMode(keys[2], INPUT);      //set key6 to input - NOTE_E4
  pinMode(keys[3], INPUT);      //set key7 to input - NOTE_F4
  pinMode(keys[4], INPUT);     //set key8 to input - NOTE_G4
  pinMode(keys[5], INPUT);     //set key9 to input - NOTE_A4
  pinMode(keys[6], INPUT);      //set key10 to input - NOTE_B4
  pinMode(keys[7], INPUT);      //set key11 to input - NOTE_C5
  
  pinMode(speaker2, OUTPUT);  //set speaker to be output
//  pinMode(speaker3, OUTPUT);  //set speaker to be output
  //Serial.begin(9600);        //initialize the communication
}  //end setup


void loop() 
{
  for (int i = 0; i < numberOfKeys; i++)
  {
        checkPianoKey(keys[i], notes[i]);
  } //end for statement
}  //end loop


void checkPianoKey (int key, int note)
{
  touchValue = readCapacitivePin(key);  //read the touch sensor value
  Serial.print("Key ");
  //Serial.println(touchValue);            //send touchValue to the computer
  //Serial.println(key);
//  delay(50);                           //delay for 1/10 of a second 
  if (touchValue > 3)                   //if the key is pressed
  {
    tone(speaker2, note);                      //play the note C
  //  delay(1000);                           //wait for one second      
  }  //end if for key
  else
  {
    noTone(speaker2);                       //stop playing note
  } //end else for key 

} //end checkPianoKey

Someone in one of my classes suggested using the pitches.h file rather than putting the frequencies directly in the sketch, but that didn’t help either.

Why would the Arduino Uno act differently than the LilyPad Mainboard. Is there a way to make the tones instead of buzzes. Will adding resistors help? I have not had time to try this yet. I was going to use 10K resistors. Or would a different size be better? I’m using wire for the connections not conductive thread. I also made the touch sensors with copper wire.

Would that make a difference?

In searching for help on the forum I found this post http://forum.arduino.cc/index.php?topic=202233.0 which has a link to this tutorial http://www.instructables.com/id/Capacitive-Touch-Arduino-Keyboard-Piano/?ALLSTEPS. So I tried making a prototype using this version, but have had problems with this too – it wouldn’t stop toning. In the comments for this tutorial someone posted changes they made to the sketch along with using a breadboard rather than connecting everything directly into the Arduino Uno. I tried their changes along with using the breadboard layout and it still doesn’t work right. When touching a sensor, it takes a few seconds to sound a tone and when removing my finger it takes a few seconds to stop toning.

/*
  Capacitive-Touch Arduino Keyboard Piano
  
  Plays piano tones through a buzzer when the user taps touch-sensitive piano "keys"
  
  Created  18 May 2013
  Modified 23 May 2013
  by Tyler Crumpton and Nicholas Jones
  
  This code is released to the public domain. For information about the circuit,
  visit the Instructable tutorial at http://www.instructables.com/id/Capacitive-Touch-Arduino-Keyboard-Piano/
*/

#include <CapacitiveSensor.h>
#include "pitches.h"

#define COMMON_PIN      2    // The common 'send' pin for all keys
#define BUZZER_PIN      A4   // The output pin for the piezo buzzer
#define NUM_OF_SAMPLES  10   // Higher number whens more delay but more consistent readings
#define CAP_THRESHOLD   80  // Capactive reading that triggers a note (adjust to fit your needs)
#define NUM_OF_KEYS     7    // Number of keys that are on the keyboard

// This macro creates a capacitance "key" sensor object for each key on the piano keyboard:
#define CS(Y) CapacitiveSensor(2, Y)

// Each key corresponds to a note, which are defined here. Uncomment the scale that you want to use:
int notes[]={NOTE_C4,NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_A4,NOTE_B4}; // C-Major scale


//int notes[]={NOTE_A4,NOTE_B4,NOTE_C5,NOTE_D5,NOTE_E5,NOTE_F5,NOTE_G5,NOTE_A5}; // A-Minor scale
//int notes[]={NOTE_C4,NOTE_DS4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_AS4,NOTE_C5,NOTE_DS5}; // C Blues scale

// Defines the pins that the keys are connected to:
CapacitiveSensor keys[] = {CS(3), CS(4), CS(5), CS(6), CS(7), CS(8), CS(9)};

void setup() { 
  // Turn off autocalibrate on all channels:
  for(int i=0; i<8; ++i) {
    keys[i].set_CS_AutocaL_Millis(0xFFFFFFFF);
  }
  // Set the buzzer as an output:
  pinMode(BUZZER_PIN, OUTPUT); 
}

void loop() {

unsigned int keysPlaying = 0;

// Loop through each key:

for (int i = 0; i < NUM_OF_KEYS; ++i) {

// If the capacitance reading is greater than the threshold, play a note:

int c = keys[i].capacitiveSensor(NUM_OF_SAMPLES);

if(c > CAP_THRESHOLD) {

tone(BUZZER_PIN, notes[i]); // Plays the note corresponding to the key pressed

keysPlaying++;

}

}

if (keysPlaying == 0) {

noTone(BUZZER_PIN);

}
}

This code required installing the capacitive sensor library and used the pitches.h file. As suggested in the comments, I have tried changing the NUM_OF_SAMPLES and CAP_THRESHOLD to various numbers and nothing seems to help. The higher the number for CAP_THRESHOLD seemed to take longer for the tone to start/stop. Using a negative number for CAP_THRESHOLD made it play through the various tones without even touching them.

Some questions I have:

  1. Does the type of metal affect the sensitivity or the tone? I am planning on making a container to hold the electronics and make something artist with it. I’m also planning on using bolts with a round top for the touch sensors rather than aluminum foil.

  2. Would the size of the resistor make a difference? I am using 2.2MΩ resistors as suggested in the tutorial.

  3. Will the source of power affect anything? Right now I’m using a USB connected to a laptop, but plan on getting a wall adaptor so the project can be plugged into a wall socket.

  4. I don’t understand the NUM_OF_SAMPLES and CAP_THRESHOLD in the sketch and how they affect what is being played.

5.I also don’t have a clear understanding of resistors and how they affect things. I am a newbie.

I looked through some of the other posts in the forum and didn’t see anyone that had these problems. Some suggested the type of metal could affect sensitivity, others indicated it wouldn’t.