Skip to content

Instantly share code, notes, and snippets.

@brysonian
Created February 19, 2021 02:02
Show Gist options
  • Save brysonian/b8b7c4ed19ee152c48956a6d03f655c9 to your computer and use it in GitHub Desktop.
Save brysonian/b8b7c4ed19ee152c48956a6d03f655c9 to your computer and use it in GitHub Desktop.
MPR121
#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;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
// connect to the sensor at the proper address (defined in the documentation)
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 & bit(i)) && !(lasttouched & bit(i)) ) {
Serial.print(i); Serial.println(" touched");
}
// if it *was* touched and now *isnt*, alert!
if (!(currtouched & bit(i)) && (lasttouched & bit(i)) ) {
Serial.print(i); Serial.println(" released");
}
}
lasttouched = currtouched;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment