Problem when programming touch sensor (MPR121)

Hello,
I want to programm a midi controller with a touch sensor.
I can’t get the touch sensor (adafruit mpr121) to work properly in my code.
Thus I added a few lines of code in the MPR121test sketch.
This is the sketch without my code:

Designed specifically to work with the MPR121 Breakout in the Adafruit shop 
  ----> https://www.adafruit.com/products/

These sensors use I2C communicate, at least 2 pins are required 
to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.  
BSD license, all text above must be included in any redistribution
**********************************************************/

#include <Wire.h>
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;

void setup() {
  Serial.begin(9600);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }
  
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  // Get the currently touched pads
  currtouched = cap.touched();
  
  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
    }
  }

  // reset our state
  lasttouched = currtouched;

  // comment out this line for detailed data from the sensor!
  return;
  
  // debugging info, what
  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  Serial.print("Filt: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();
  
  // put a delay so it isn't overwhelming
  delay(100);
}

Here it is with my code:

/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor

Designed specifically to work with the MPR121 Breakout in the Adafruit shop 
  ----> https://www.adafruit.com/products/

These sensors use I2C communicate, at least 2 pins are required 
to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.  
BSD license, all text above must be included in any redistribution
**********************************************************/

#include <Wire.h>
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
boolean zero = 0; 
boolean four = 0;
boolean five = 0;


void setup() {
  Serial.begin(9600);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }
  
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  // Get the currently touched pads
  currtouched = cap.touched();
  
  
for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      if(i==0){zero = 1;}
      if(i==4){four = 1;}
      if(i==5){five = 1;}}
      if (zero == 1){Serial.println("zero is touched");}
      //if(four == 1 && zero == 1 && five ==1){Serial.println("one four five touched");}
       if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
      if(i==0){zero = 0;}
      if(i==4){four = 0;}
      if(i==5){five = 0;}
    }
    
}
  


  // reset our state
  lasttouched = currtouched;

  // comment out this line for detailed data from the sensor!
  return;
  
  // debugging info, what
  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  Serial.print("Filt: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();
  
  // put a delay so it isn't overwhelming
  delay(100);
}

Now when I press the touch pin 0, it stops working (I don’t get any text in the serial monitor, it says a couple times “zero is touched”) then 0 is released and if I touch any pin I don’t get any message in the serial monitor anymore".

Later I want to output a different midi note, depending on the combination of pins which are touched.
But it doesn’t work with any code I write for this sensor.

Can anybody help me?
Thanks

musicianist:
Now when I press the touch pin 0, it stops working (I don't get any text in the serial monitor, it says a couple times "zero is touched") then 0 is released and if I touch any pin I don't get any message in the serial monitor anymore".

It either doesn't print something in the serial monitor or it does. Please clarify the actual behaviour and the behaviour you expect. Feel free to post the output of the serial monitor (wrapped in code tags of course).
How is your hardware connected up?

Adafruit MPR121 Capacitive Touch sensor test
MPR121 found!
6 touched
6 released
7 touched
7 released
8 touched
8 released
5 touched
5 released
5 touched
5 released
4 touched
4 released
3 touched
3 released
2 touched
2 released
1 touched
1 released
1 touched
1 released
0 touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
zero is touched
0 released

After touching the 0 pin, the serial monitor doesn’t show if I press or release any pin, I get no text after (0 pin is touched when the serial monitor is saying 0 is touched, it looks like it freezes).

I’ve attached a circuit how my hardware is connected right now. I’ve tried it also with only connecting my ARDUINO NANO to the touch sensor. I also tried it with two different MPR121s.

I still shows text, after 0 pin is touched with this code:

/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor

Designed specifically to work with the MPR121 Breakout in the Adafruit shop 
  ----> https://www.adafruit.com/products/

These sensors use I2C communicate, at least 2 pins are required 
to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.  
BSD license, all text above must be included in any redistribution
**********************************************************/

#include <Wire.h>
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
boolean zero = 0; 
boolean four = 0;
boolean five = 0;


void setup() {
  Serial.begin(9600);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }
  
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  // Get the currently touched pads
  currtouched = cap.touched();
  
  
for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      if(i==0){zero = 1;}
      if(i==4){four = 1;}
      if(i==5){five = 1;}}
      if (zero == 1){Serial.println("zero is touched");
      delay(1000);
      Serial.println("it still shows text");
      delay(5000);
      Serial.println("it still shows text, but not which pins are touched"); }
      //if(four == 1 && zero == 1 && five ==1){Serial.println("one four five touched");}
       if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
                                     Serial.print(i); Serial.println(" released");
                                     if(i==0){zero = 0;}
                                      if(i==4){four = 0;}
                                      if(i==5){five = 0;}
                                      
    }
    
}
  


  // reset our state
  lasttouched = currtouched;

  // comment out this line for detailed data from the sensor!
  return;
  
  // debugging info, what
  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  Serial.print("Filt: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();
  
  // put a delay so it isn't overwhelming
  delay(100);
}

The for loop is seemingly repeated, but it isn’t shown which touch pins are touched:

Adafruit MPR121 Capacitive Touch sensor test
MPR121 found!
8 touched
8 released
7 touched
7 released
6 touched
6 released
5 touched
5 released
6 touched
6 released
7 touched
7 released
6 touched
6 released
5 touched
5 released
4 touched
4 released
4 touched
3 touched
3 released
4 released
3 touched
3 released
2 touched
2 released
1 touched
1 released
1 touched
1 released
0 touched
zero is touched
it still shows text
it still shows text, but not which pins are touched
zero is touched
it still shows text
it still shows text, but not which pins are touched
zero is touched
it still shows text
it still shows text, but not which pins are touched
zero is touched
it still shows text
it still shows text, but not which pins are touched
zero is touched
it still shows text

It also surprsingly stops working when adding a simple Serial.println() command (

Serial.println("got in the second if, but no new info which pins are touched from now on");

) like shown below:

/*********************************************************
This is a library for the MPR121 12-channel Capacitive touch sensor

Designed specifically to work with the MPR121 Breakout in the Adafruit shop 
  ----> https://www.adafruit.com/products/

These sensors use I2C communicate, at least 2 pins are required 
to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.  
BSD license, all text above must be included in any redistribution
**********************************************************/

#include <Wire.h>
#include "Adafruit_MPR121.h"

// You can have up to 4 on one i2c bus but one is enough for testing!
Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
boolean zero = 0; 
boolean four = 0;
boolean five = 0;


void setup() {
  Serial.begin(9600);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }
  
  Serial.println("Adafruit MPR121 Capacitive Touch sensor test"); 
  
  // Default address is 0x5A, if tied to 3.3V its 0x5B
  // If tied to SDA its 0x5C and if SCL then 0x5D
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  // Get the currently touched pads
  currtouched = cap.touched();
  
  
for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      if(i==0){zero = 1;}
      if(i==4){four = 1;}
      if(i==5){five = 1;}}
      if (zero == 1){Serial.println("zero is touched");
      delay(1000);
      Serial.println("it still shows text");
      delay(5000);
      Serial.println("it still shows text, but not which pins are touched"); }
      //if(four == 1 && zero == 1 && five ==1){Serial.println("one four five touched");}
       if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
                                     Serial.print(i); Serial.println(" released");
                                     if(i==0){zero = 0;}
                                      if(i==4){four = 0;}
                                      if(i==5){five = 0;}
                                      Serial.println("got in the second if, but no new info which pins are touched from now on");
                                      
    }
    
}
  


  // reset our state
  lasttouched = currtouched;

  // comment out this line for detailed data from the sensor!
  return;
  
  // debugging info, what
  Serial.print("\t\t\t\t\t\t\t\t\t\t\t\t\t 0x"); Serial.println(cap.touched(), HEX);
  Serial.print("Filt: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();
  
  // put a delay so it isn't overwhelming
  delay(100);
}

The serial monitor output then is:

Adafruit MPR121 Capacitive Touch sensor test
MPR121 found!
8 touched
5 touched
6 touched
4 touched
1 touched
1 released
got in the second if, but no new info which pins are touched from now on
4 released
got in the second if, but no new info which pins are touched from now on
5 released
got in the second if, but no new info which pins are touched from now on
6 released
got in the second if, but no new info which pins are touched from now on
8 released
got in the second if, but no new info which pins are touched from now on

The example sketch works fine until my lines of codes are touched.

The OP’s circuit inline - although he has already stated that he has also tested unsuccessfully with just the Nano and the touch sensor:

98231dbd0f31655cab6cf213867d894ab39c75c6.jpg

Maybe there is something obviously wrong, but I’m not seeing it yet.

Try this tidied up version will a little extra debug, but please provide all the serial output if possible:

#include <Wire.h>
#include "Adafruit_MPR121.h"

Adafruit_MPR121 cap = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
bool zero;
bool four;
bool five;


void setup() {
  Serial.begin(9600);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }

  Serial.println("Adafruit MPR121 Capacitive Touch sensor test");

  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop() {
  currtouched = cap.touched();

  Serial.print("IN: "); Serial.print(lasttouched, HEX); Serial.print(" "); Serial.println(currtouched, HEX);

  for (uint8_t i = 0; i < 12; i++) {
 
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      if (i == 0) {
        zero = true;
      }
      if (i == 4) {
        four = true;
      }
      if (i == 5) {
        five = true;
      }
    }
    
    if (zero) {
      Serial.println("zero is touched");
    }
    
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
      if (i == 0) {
        zero = false;
      }
      if (i == 4) {
        four = false;
      }
      if (i == 5) {
        five = false;
      }
    }

  }

  Serial.print("OUT: "); Serial.print(lasttouched, HEX); Serial.print(" "); Serial.println(currtouched, HEX);

  lasttouched = currtouched;

  delay(100); // slow down the debug output
}

I've run the code:
The only output on serial monitor:

Adafruit MPR121 Capacitive Touch sensor test
MPR121 found!
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0
OUT: 0 0
IN: 0 0

etc.

Well, I was expecting more than that. I am at a loss to explain the symptoms you are seeing. Hopefully a fresh set of eyes will have a look at it and provide some suggestions...

Ok, thank you for efforts.
Even when just adding a delay it stops working:

 if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      delay(100);
    }

I don't get it.