Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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 dumpSensors(int n = 12, boolean base = false, boolean touch = false);
- boolean myBegin(Adafruit_MPR121 & d);
- void dumpAllRegisters(Adafruit_MPR121 & d);
- void setup() {
- while (!Serial); // needed to keep leonardo/micro from starting too fast!
- Serial.begin(115200);
- 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!");
- myBegin(cap);
- }
- void loop() {
- //dumpTouch();
- // comment out this line for detailed data from the sensor!
- //dumpSensors(2);
- dumpSensors(13);
- //dumpSensors(13, true); // with baseline
- // put a delay so it isn't overwhelming
- delay(100);
- }
- void dumpTouch() {
- // 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;
- }
- void dumpSensors(int n = 12, boolean base=false, boolean touch=false) {
- // debugging info, what
- if (touch) {
- 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 < n; i++) {
- Serial.print(cap.filteredData(i)); Serial.print("\t");
- }
- Serial.println();
- if (base) {
- Serial.print("Base: ");
- for (uint8_t i = 0; i < n; i++) {
- Serial.print(cap.baselineData(i)); Serial.print("\t");
- }
- Serial.println();
- }
- }
- void dumpAllRegisters(Adafruit_MPR121 & d) {
- for (uint8_t i = 0; i <= 0x7F; i++) {
- Serial.print("$"); Serial.print(i, HEX);
- Serial.print(": 0x"); Serial.print( d.readRegister8(i), HEX );
- Serial.print(", "); Serial.println(d.readRegister8(i));
- }
- }
- boolean myBegin(Adafruit_MPR121 & d) {
- // soft reset
- d.writeRegister(MPR121_SOFTRESET, 0x63); // 0x80
- delay(1);
- // dump all registers
- //dumpAllRegisters(d);
- d.writeRegister(MPR121_ECR, 0x0); // 05E, stop mode
- // check online
- //uint8_t c = d.readRegister8(MPR121_CONFIG2); // 0x5D
- //if (c != 0x24) return false;
- // Touch Status Registers (0x00~0x01)
- // Electrode Data Register (0x04~0x1D)
- // Baseline Value Register (0x1E~0x2A)
- // Baseline Filtering Control Register (0x2B~0x40)
- d.writeRegister(MPR121_MHDR, 0x01); // 0x2B
- d.writeRegister(MPR121_NHDR, 0x01); // 0x2C
- d.writeRegister(MPR121_NCLR, 0x0E); // 0x2D
- d.writeRegister(MPR121_FDLR, 0x00); // 0x2E
- d.writeRegister(MPR121_MHDF, 0x01); // 0x2F
- d.writeRegister(MPR121_NHDF, 0x05); // 0x30
- d.writeRegister(MPR121_NCLF, 0x01); // 0x31
- d.writeRegister(MPR121_FDLF, 0x00); // 0x32
- d.writeRegister(MPR121_NHDT, 0x00); // 0x33
- d.writeRegister(MPR121_NCLT, 0x00); // 0x34
- d.writeRegister(MPR121_FDLT, 0x00); // 0x35
- d.setThreshholds(12, 6); // 0x41 and 0x42 based ranges
- d.writeRegister(MPR121_DEBOUNCE, 0); // 0x5B, disable debounce
- d.writeRegister(MPR121_CONFIG1, 0x10); // 0x5C, default, 16uA charge current
- //d.writeRegister(MPR121_CONFIG2, 0x20); // 0x5D, 0.5uS encoding, 1ms period
- d.writeRegister(MPR121_CONFIG2, 0x80); // 0x5D, 4uS encoding, 1ms period
- // Individual Charge Current Register(0x5F~0x6B)
- // Individual Charge Time Register(0x6C~0x72)
- // Auto Configuration Registers (0x7B~0x7F)
- // The AUTO-CONFIG runs only once at first transition from Stop Mode to Run Mode.
- d.writeRegister(MPR121_AUTOCONFIG0, 0x8F); // 0x7B
- // default adafruid lib ranges (good for 1.8v)
- //d.writeRegister(MPR121_UPLIMIT, 150); // 0x7D
- //d.writeRegister(MPR121_LOWLIMIT, 50); // 0x7E
- //d.writeRegister(MPR121_TARGETLIMIT, 100); // 0x7F, should be ~400 (100 shifted)
- // ranges per documentation for 3.3v
- d.writeRegister(MPR121_UPLIMIT, 200); // 0x7D
- d.writeRegister(MPR121_LOWLIMIT, 130); // 0x7E
- d.writeRegister(MPR121_TARGETLIMIT, 180); // 0x7F, should be ~400 (100 shifted)
- // enable all electrodes
- d.writeRegister(MPR121_ECR, 0x8F); // 0x5E, start with first 5 bits of baseline tracking
- //d.writeRegister(MPR121_ECR, 0xA8); // 0x5E, disable sensors 8,9,10,11. enable proximity 0-3
- // dump all registers
- delay(10);
- dumpAllRegisters(d);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment