CTPB Test Script

The following Arduino Sketch is used to test the CTPB and is based on example code from the Adafruit MPR121 Capacitive Touch Breakout Tutorial:

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

modifications:
aks      : added capability to accommodate for up to 4 Adafruit MPR121
Breakout boards (0x5A, 0x5B, 0x5C and 0x5D);
boards are searched in that order (i.e. if less thatn 4 boards are
used, the unused ones start at 0x5D down to 0x5B).

works with version Arduino IDE 1.6.5 (no Keyboard nor Mouse libraries needed)
**********************************************************/

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

#define DEBUG      1

int numBoards = 0; // will set to maximum number of 0x5A, 0x5B, 0x5C 0x5D found

// You can have up to 4 on one i2c bus
Adafruit_MPR121 cap0 = Adafruit_MPR121();
Adafruit_MPR121 cap1 = Adafruit_MPR121();
Adafruit_MPR121 cap2 = Adafruit_MPR121();
Adafruit_MPR121 cap3 = Adafruit_MPR121();

// Keeps track of the last pins touched
// so we know when buttons are 'released'
uint16_t lasttouched0 = 0;
uint16_t lasttouched1 = 0;
uint16_t lasttouched2 = 0;
uint16_t lasttouched3 = 0;

uint16_t currtouched0 = 0;
uint16_t currtouched1 = 0;
uint16_t currtouched2 = 0;
uint16_t currtouched3 = 0;

void setup() {
  Serial.begin(9600);
  if (DEBUG) {
    Serial.println("Adafruit MPR121 Capacitive Touch sensor test");
  }

  // Default address is 0x5A, if ADDR tied to 3.3V it's 0x5B
  // If ADDR tied to SDA it's 0x5C and ifADDR tied to SCL then it's 0x5D
  if (cap0.begin(0x5A)) {
    numBoards = 1;
    if (DEBUG) {
    Serial.println ("MPR121(0) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(0) not found, check wiring?");
    }
  }
  if (cap1.begin(0x5B)) {
    numBoards = 2;
    if (DEBUG) {
      Serial.println ("MPR121(1) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(1) not found, check wiring?");
    }
  }
  if (cap2.begin(0x5C)) {
    numBoards = 3;
    if (DEBUG) {
      Serial.println ("MPR121(2) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(2) not found, check wiring?");
    }
  }
  if (cap3.begin(0x5D)) {
    numBoards = 4;
    if (DEBUG) {
      Serial.println ("MPR121(3) found");
    }
  } else {
    if (DEBUG) {
      Serial.println("MPR121(3) not found, check wiring?");
    }
  }
  if (DEBUG) {
    Serial.print ("finish setup: using ");
    Serial.print (numBoards);
    Serial.println (" MPR121 boards");
  }
}

void loop() {
  // Get the currently touched pads
  currtouched0 = cap0.touched();
  if (numBoards > 1) {
    currtouched1 = cap1.touched();
  }
  if (numBoards > 2) {
    currtouched2 = cap2.touched();
  }
  if (numBoards > 3) {
    currtouched3 = cap3.touched();
  }

  for (uint8_t i=0; i<12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched0 & _BV(i)) && !(lasttouched0 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i); Serial.println(" touched");
      }
    }
    if ((currtouched1 & _BV(i)) && !(lasttouched1 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+12); Serial.println(" touched");
      }
    }
    if ((currtouched2 & _BV(i)) && !(lasttouched2 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+24); Serial.println(" touched");
      }
    }
    if ((currtouched3 & _BV(i)) && !(lasttouched3 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+36); Serial.println(" touched");
      }
    }
    // if it *was* touched and now *isnt*, alert!
    if (!(currtouched0 & _BV(i)) && (lasttouched0 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i); Serial.println(" released");
      }
    }
    if (!(currtouched1 & _BV(i)) && (lasttouched1 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+12); Serial.println(" released");
      }
    }
    if (!(currtouched2 & _BV(i)) && (lasttouched2 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+24); Serial.println(" released");
      }
    }
    if (!(currtouched3 & _BV(i)) && (lasttouched3 & _BV(i)) ) {
      if (DEBUG) {
        Serial.print(i+36); Serial.println(" released");
      }
    }
  }

  // reset our state
  lasttouched0 = currtouched0;
  lasttouched1 = currtouched1;
  lasttouched2 = currtouched2;
  lasttouched3 = currtouched3;
}