code:

#include <MPR121.h>
#include <MPR121_defs.h>
#include <Wire.h> // Wiring two-wire library for I2C
#include “mpr121.h” // register definitions
int notes[]={239,213,190,179,159,142};
char touch_previous;

void setup()
{

Serial.begin(9600);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS22);
OCR2B = 10;

// MPR121 CONFIG
Wire.begin();
mpr121QuickConfig();

touch_previous=0;
}

void loop()
{
char buffer[32];
int i=0;
int note=0;

Wire.beginTransmission(0x5A);
Wire.write(0);

Wire.requestFrom(0x5A, 18);

while(Wire.available())
{
buffer[i] = Wire.read();
i++;
}
Wire.endTransmission();

if (buffer[0] != touch_previous){
touch_previous=buffer[0];
Serial.print(“TOUCH STATUS “);
Serial.println(buffer[0], DEC);
tone(9,i*100,500);

note=0;
for(i=0;i<8;i++){
if ((buffer[0]>>i)&1){
note=i;
}
}
if (buffer[0]){
OCR2A = notes[note];
}else{
OCR2A = 0;
}
}

}

char mpr121Read(unsigned char address)
{
char data;

Wire.beginTransmission(0x5A); // begin communication with the MPR121 on I2C address 0x5A
Wire.write(address); // sets the register pointer
Wire.requestFrom(0x5A, 1); // request for the MPR121 to send you a single byte

// check to see if we’ve received the byte over I2C
if(1 <= Wire.available())
{
data = Wire.read();
}

Wire.endTransmission(); // ends communication

return data; // return the received data
}

void mpr121Write(unsigned char address, unsigned char data)
{
Wire.beginTransmission(0x5A); // begin communication with the MPR121 on I2C address 0x5A
Wire.write(address); // sets the register pointer
Wire.write(data); // sends data to be stored
Wire.endTransmission(); // ends communication
}

// MPR121 Quick Config
// This will configure all registers as described in AN3944
// Input: none
// Output: none

void mpr121QuickConfig(void)
{
// Section A
// This group controls filtering when data is > baseline.
mpr121Write(MHD_R, 0x01);
mpr121Write(NHD_R, 0x01);
mpr121Write(NCL_R, 0x00);
mpr121Write(FDL_R, 0x00);

// Section B
// This group controls filtering when data is < baseline.
mpr121Write(MHD_F, 0x01);
mpr121Write(NHD_F, 0x01);
mpr121Write(NCL_F, 0xFF);
mpr121Write(FDL_F, 0x02);

// Section C
// This group sets touch and release thresholds for each electrode
mpr121Write(ELE0_T, TOU_THRESH);
mpr121Write(ELE0_R, REL_THRESH);
mpr121Write(ELE1_T, TOU_THRESH);
mpr121Write(ELE1_R, REL_THRESH);
mpr121Write(ELE2_T, TOU_THRESH);
mpr121Write(ELE2_R, REL_THRESH);
mpr121Write(ELE3_T, TOU_THRESH);
mpr121Write(ELE3_R, REL_THRESH);
mpr121Write(ELE4_T, TOU_THRESH);
mpr121Write(ELE4_R, REL_THRESH);
mpr121Write(ELE5_T, TOU_THRESH);
mpr121Write(ELE5_R, REL_THRESH);

mpr121Write(ELE6_T, TOU_THRESH);
mpr121Write(ELE6_R, REL_THRESH);
mpr121Write(ELE7_T, TOU_THRESH);
mpr121Write(ELE7_R, REL_THRESH);
mpr121Write(ELE8_T, TOU_THRESH);
mpr121Write(ELE8_R, REL_THRESH);
mpr121Write(ELE9_T, TOU_THRESH);
mpr121Write(ELE9_R, REL_THRESH);
mpr121Write(ELE10_T, TOU_THRESH);
mpr121Write(ELE10_R, REL_THRESH);
mpr121Write(ELE11_T, TOU_THRESH);
mpr121Write(ELE11_R, REL_THRESH);

// Section D
// Set the Filter Configuration
// Set ESI2
//mpr121Write(FIL_CFG, 0x04);
mpr121Write(FIL_CFG, 0x00);

// Section E
// Electrode Configuration
// Enable 6 Electrodes and set to run mode
// Set ELE_CFG to 0x00 to return to standby mode
mpr121Write(ELE_CFG, 0x0C); // Enables all 12 Electrodes
//mpr121Write(ELE_CFG, 0x06); // Enable first 6 electrodes

// Section F
// Enable Auto Config and auto Reconfig
mpr121Write(ATO_CFG0, 0x0B);
mpr121Write(ATO_CFGU, 0xC9); // USL = (Vdd-0.7)/vdd*256 = 0xC9 @3.3V
mpr121Write(ATO_CFGL, 0x82); // LSL = 0.65*USL = 0x82 @3.3V
mpr121Write(ATO_CFGT, 0xB5); // Target = 0.9*USL = 0xB5 @3.3V

}