doubt about arrays

i had a problem with this code, in which i read what pins are pressed and trying to save them into an array, but it always exits(the array) 000000000000(pines_MPR121), and what i want to do it is to give me an array in which if buttons 1, 3 and 11 are pressed, i get an array like this: 101000000010 (pines_MPR121*), what am i doing wrong?
_
(this is just the part that uses all refering to the reading buttons, my sketch has many tabs, but this is the one with the array problem)_
sry for my bad english
_
```_
#include “Adafruit_MPR121.h”

Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
int pines_MPR121[12];

bool siendo_pulsado;
//byte pin_MPR121;

void accion_MPR121() {
  for (uint8_t i = 0; i < 12; i++) {

if (pines_MPR121[i] == 1) {

switch (i) {
        case 1:
          Serial.println(“aaaaaaaaaaaaaaaaaa”);
          break;

case 2:
          Serial.println(“bbbbbbbbbbbbbbbbb”);

break;

case 3:
          Serial.println(“cccccccccccccccccccc”);

break;

}
    }

}
}

void pulsacion_MPR121() {
  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");
      pines_MPR121[i] = 1;
Serial.print(pines_MPR121[i]);
    }else
    // if it was touched and now isnt, alert!
    if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" released");
      pines_MPR121[i] = 0;
    }

//pin_MPR121 = i;
    pines_MPR121[i] = siendo_pulsado;
    Serial.print(pines_MPR121[i]);
  }
  lasttouched = currtouched;
Serial.println();
}

void begin_MPR121() {
  if (!cap.begin(0x5A)) {
    Serial.println(“MPR121 not found, check wiring?”);
    while (1);
  }
  //Serial.println(“MPR121 found!”);
}
_
```*_

Post your WHOLE sketch and then we can really get a fix on the problem.

Shot in the dark: make it an "if - else if" statement instead of an "if - if". That might work.

It helps to see all of your code. Before you post it, press Ctrl-T in the IDE to format to a common C style. We also need to see how BV() is coded and where nad how MPR121[] is defined.

ok, i put the declarations of the array and all the referent to the mpr121, but the BV() thing, came in the example of how to use it, and i didnt understand well how it worked, so i just copied it :frowning: and tried t owork with it

the example code of the mpr121 library, maybe you can understand what does that Bv() function do

/*********************************************************
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;

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);
}

Diablillowilly:
i had a problem with this code, in which i read what pins are pressed and trying to save them into an array, but it always exits(the array) 000000000000(pines_MPR121), and what i want to do it is to give me an array in which if buttons 1, 3 and 11 are pressed, i get an array like this: 101000000010 (pines_MPR121*), what am i doing wrong?
_
(this is just the part that uses all refering to the reading buttons, my sketch has many tabs, but this is the one with the array problem)_
sry for my bad english
_
```_
#include “Adafruit_MPR121.h”

Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
int pines_MPR121[12];

bool siendo_pulsado;
//byte pin_MPR121;

void accion_MPR121() {
 for (uint8_t i = 0; i < 12; i++) {

if (pines_MPR121[i] == 1) {

switch (i) {
       case 1:
         Serial.println(“aaaaaaaaaaaaaaaaaa”);
         break;

case 2:
         Serial.println(“bbbbbbbbbbbbbbbbb”);

break;

case 3:
         Serial.println(“cccccccccccccccccccc”);

break;

}
   }

}
}

void pulsacion_MPR121() {
 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");
     pines_MPR121[i] = 1;
Serial.print(pines_MPR121[i]);
   }else
   // if it was touched and now isnt, alert!
   if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
     Serial.print(i); Serial.println(" released");
     pines_MPR121[i] = 0;
   }

//pin_MPR121 = i;
   pines_MPR121[i] = siendo_pulsado;
   Serial.print(pines_MPR121[i]);
 }
 lasttouched = currtouched;
Serial.println();
}

void begin_MPR121() {
 if (!cap.begin(0x5A)) {
   Serial.println(“MPR121 not found, check wiring?”);
   while (1);
 }
 //Serial.println(“MPR121 found!”);
}*

```
[/quote]
the whole sketch, not just the refering things to the mpr121 are now attached to the reply, it longer then 9000 characters, so the forum does not let me post it
checkeo.ino (248 Bytes)
comandos.ino (2.37 KB)
interaccion_012.ino (2.81 KB)
MPR121.ino (1.22 KB)
tira_leds.ino (2.65 KB)

Diablillowilly:
the whole sketch, not just the refering things to the mpr121 are now attached to the reply, it longer then 9000 characters, so the forum does not let me post it

In general, a person who cannot make arrays work should not be attempting to write massive sketches with five source files.

For instance, does the OP understand what this line:

(!(currtouched & _BV(i)) && (lasttouched & _BV(i)) )

Does? And who the heck decided
a) that the expression (1<<i) needed a macro
b) not to use the standard arduino macro BitSet (or whatever it’s called)
c) to mix bit arrays and regular arrays.

For the more advanced:

cap.touched() gives you a bit vector, then (touched & ~lasttouched) is a fine way to sense presses. For bonus hilarity, you can always get the leftmost bit of a number x with the expression (x & (x ^ (x-1))), allowing you to loop directly though the recently pressed bits.

memset(pines_MPR121, 0, sizeof(pines_MPR121));
int lasttouched = currtouched;
currtouched = cap.touched();
int changed = (currtouched & ~lasttouched);
while(changed) {
  const int onechange =  changed & (changed ^ (changed-1)));
  const int bit = 
    ((onechange & 0xFF00) ? 8 : 0) +
    ((onechange & 0xF0F0) ? 4 : 0) +
    ((onechange & 0xCCCC) ? 2 : 0) +
    ((onechange & 0xAAAA) ? 1 : 0);
  pines_MPR121[bit] = 1;
  changed &= ~onechange;
}

PaulMurrayCbr:
a) that the expression (1<<i) needed a macro

It’s a AVR thing :wink: