Go Down

Topic: 6 capacitance sensors-- problem with sample code? (Read 546 times) previous topic - next topic


I am working on making a keypad with 6 capacitance sensors. I am using an arduino diecimilia. I tried using the sample code found here: arduino.cc/playground/Code/CapacitiveSensor

The problem is no matter what is touching it, input digital 13 always returns a value of 16 in the serial output.

Does anyone have any ideas why this might be the case?  

Thank you so much!!!!
Note: I'm also named Mario, but I'm not the person who wrote the sample code below.

Mario Becker, Fraunhofer IGD, 2007

// sensor key
#define KEYDDR  DDRB
#define KEYPIN  PINB
#define KEY0    PB0        // capture input - digital 8
#define KEY1    PB1        // capture input - digital 9
#define KEY2    PB2        // capture input - digital 10
#define KEY3    PB3        // capture input - digital 11
#define KEY4    PB4        // capture input - digital 12
#define KEY5    PB5        // capture input - digital 13

void setup()
 Serial.begin(9600);      // connect to the serial port

// returns capacity on one input pin
// pin must be the bitmask for the pin e.g. (1<<PB0)
char getcap(char pin)
 char i = 0;
 KEYDDR &= ~pin;          // input
 KEYPORT |= pin;          // pullup on
 for(i = 0; i < 16; i++)
   if( (KEYPIN & pin) ) break;
 KEYPORT &= ~pin;         // low level
 KEYDDR |= pin;           // discharge
 return i;

void loop ()
 char capval[6];
 char pinval[6] = {1<<KEY0,1<<KEY1,1<<KEY2,1<<KEY3,1<<KEY4,1<<KEY5};
 for(char i = 0; i < 6; i++)
   capval = getcap(pinval);
   Serial.print("digital ");
   Serial.print(i+8, DEC);
   Serial.print(": ");
   Serial.println(capval, DEC);



I'm trying to do the same and keep getting the same problem. Unfortunately, I've had to delete pin13 and lower the array to five. It works with five sensors but the problem is niggling me. I don't suppose you have any idea of how to get this code to communicate with max/msp? I've downloaded the Arduino2Max patch from the Arduino site and technically it should be reading the values outputted from Arduino but alas, no.

Any ideas or suggestions?


Go Up