Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // touch includes
- #include <MPR121.h>
- #include <Wire.h>
- #define MPR121_ADDR 0x5A
- #define MPR121_INT 4
- // mapping and filter definitions
- #define LOW_DIFF 0
- #define HIGH_DIFF 10
- #define filterWeight 0.3f // 0.0f to 1.0f - higher value = more smoothing
- float lastProx = 0;
- // the electrode to monitor for proximity
- #define PROX_ELECTRODE 0
- // define LED_BUILTIN for older versions of Arduino
- #ifndef LED_BUILTIN
- #define LED_BUILTIN 13
- #endif
- void setup() {
- Serial.begin(57600);
- pinMode(LED_BUILTIN, OUTPUT);
- if (!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
- MPR121.setInterruptPin(MPR121_INT);
- MPR121.setRegister(MPR121_NHDF, 0x01); //noise half delta (falling)
- MPR121.setRegister(MPR121_FDLF, 0x3F); //filter delay limit (falling)
- }
- void loop() {
- readTouchInputs();
- }
- void readTouchInputs() {
- MPR121.updateAll();
- // only make an action if we have one or fewer pins touched
- // ignore multiple touches
- for (int i = 1; i < 12; i++) { // Check which electrodes were pressed
- if (MPR121.isNewTouch(i)) {
- //pin i was just touched
- Serial.print("pin ");
- Serial.print(i);
- Serial.println(" was just touched");
- digitalWrite(LED_BUILTIN, HIGH);
- } else {
- if (MPR121.isNewRelease(i)) {
- Serial.print("pin ");
- Serial.print(i);
- Serial.println(" is no longer being touched");
- digitalWrite(LED_BUILTIN, LOW);
- }
- }
- }
- // read the difference between the measured baseline and the measured continuous data
- int reading = MPR121.getBaselineData(PROX_ELECTRODE) - MPR121.getFilteredData(PROX_ELECTRODE);
- // Serial.print("Proximity electrode: ");
- // Serial.println(reading);
- unsigned int prox = constrain(reading, LOW_DIFF, HIGH_DIFF);
- lastProx = (filterWeight * lastProx) + ((1 - filterWeight) * (float)prox);
- uint8_t thisOutput = (uint8_t)map(lastProx, LOW_DIFF, HIGH_DIFF, 0, 254);
- Serial.println(thisOutput);
- analogWrite(9, thisOutput);
- //delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement