Using MPR121 .setThreshold command in Arduino Sketch

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

I've been sifting through every post on the forum (pretty sure I have some gray hairs now) and still can't find anyone who has gotten the cap.setThresholds( touch, release) command to work in the arduino sketch.

The usual response is to simply write cap.setThresholds(12, 6) or whatever you choose, but the values set in the .cpp file still dominate. I've modified the .h and .cpp files and even used these files to make a secondary library to set threshold values in separate from the Adafruit_MPR121.cpp file. I currently am not running into any issues modifying the .cpp file, but considering that the library should remain static for the most part, it would be nice to know how to have the arduino sketch, set the touch sensitivity.

For example, let's say I want to set lower thresholds. In the .cpp file I modify the line:

Code: Select all

setThreshholds(12, 6);
to read:

Code: Select all

setThreshholds(5, 1);
I have tested this already and it indeed works just fine. Now let's say I comment this line out of the .cpp file and wish to set the threshold in the arduino sketch. I use the example code provided by adafruit:

Code: Select all

Adafruit_MPR121 cap = Adafruit_MPR121();
meaning that to set the threshold in the void setup function I would write:

Code: Select all

cap.setThresholds(5, 1);
Yes, I am aware that this reads different from "setThreshholds"; however, this may have been a typo in the cpp file from adafruit, or something intentional but either iteration of setThresholds or setThreshholds does not seem to set the desired sensitivity. Instead, with the line commented out of the cpp file it seems that the sensor behaves as if the thresholds were set to 0 for both touch and release and false triggers. If the line is uncommented in the cpp file, then it upholds the threshold values set in the cpp file and ignores those present in the sketch meaning the cpp rules them all (sort of like the one ring).

Ultimately, this item is being used under layer of faux fur. We've determined the thresholds for setThreshholds to be (1, 0). This is the only setting in which the sensor is triggered and gives no false positives. Hopefully this gives a better idea. It is a work project, so I'm not allowed to disclose too much information, but I assure you it's a really cool project.

Any help would be greatly appreciated, and though I'd like to send cookies, they'd probably all wind up in my stomach before I get to UPS.

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

Additionally, I'm wondering now how exactly the library receives the input from cap.setThresholds and if there is a way for me to trouble shoot the code to print out what values are being received by the library.

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

Looks like there are two functions, one with two h's and the other with one h. The one with two h's is marked as deprecated:
https://github.com/adafruit/Adafruit_MP ... 121.h#L101
so you should only use setThresholds.

It looks like you did that. This is generally correct:

Code: Select all

// do this to create the object
Adafruit_MPR121 cap = Adafruit_MPR121();

// somewhere later in code, do this to set thresholds
cap.setThresholds(5, 1);
When you say:
I've modified the .h and .cpp files and even used these files to make a secondary library to set threshold values in separate from the Adafruit_MPR121.cpp file.
were you modifying the library files? If so, return to the unmodified version and try again.
Additionally, I'm wondering now how exactly the library receives the input from cap.setThresholds and if there is a way for me to trouble shoot the code to print out what values are being received by the library.
It all happens here:
https://github.com/adafruit/Adafruit_MP ... 1.cpp#L122

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

were you modifying the library files? If so, return to the unmodified version and try again.
Modifying the library and changing certain hex values improved the performance. I reverted them back and no improvement on the sensor occurred. In fact, it just got worse. Reverting the Adafruit_MPR121 library to its defaults, and calling the line

Code: Select all

cap.setThresholds(1,0); 
yielded no changes in the sensitivity of the sensor or its performance. Changing the setThreshholds line (which is deprecated as you say) is the only thing that alters the performance of the sensor. I need a way around this or some means to get the arduino sketch, to set these values in the library, so that the parameter can be modified through the sketch and not through the .cpp file. I've been trying to hunt this very thing down, but am yet to discover a solution. Thoughts?

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

It seems odd that calling setThresholds (one h) does nothing while calling setThreshholds (two h's) does something. The two h version simply calls the one h version:
https://github.com/adafruit/Adafruit_MP ... 1.cpp#L105

What version of the MPR121 library are you using? You can check through Library Manager.

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

Library is:

Adafruit MPR121 by Adafruit vers 1.0.2

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

That's the current version. Can you paste the full code listing for your sketch.

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

Sure. I'm using the example code for testing purposes. Thought it would be the best option to test this to then implement in other applications.

Code: Select all

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

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

#ifndef _BV
#define _BV(bit) (1 << (bit)) 
#endif

// 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 setup() {
  Serial.begin(9600);

  while (!Serial) { // needed to keep leonardo/micro from starting too fast!
    delay(10);
  }
  
  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!");
  cap.setThresholds(1,0);  // THIS IS THE ONLY LINE THAT I HAVE ADDED - RB4400R
}

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 & _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;

  // comment out this line for detailed data from the sensor!
  return;
  
  // debugging info, what
  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<12; i++) {
    Serial.print(cap.filteredData(i)); Serial.print("\t");
  }
  Serial.println();
  Serial.print("Base: ");
  for (uint8_t i=0; i<12; i++) {
    Serial.print(cap.baselineData(i)); Serial.print("\t");
  }
  Serial.println();
  
  // put a delay so it isn't overwhelming
  delay(100);
}

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

That looks fine. So if you change this line:

Code: Select all

  cap.setThresholds(1,0);  // THIS IS THE ONLY LINE THAT I HAVE ADDED - RB4400R
to this (add an h):

Code: Select all

  cap.setThreshholds(1,0);  // THIS IS THE ONLY LINE THAT I HAVE ADDED - RB4400R
you are getting the expected results?

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

Not quite. Calling the line:

Code: Select all

cap.setThresholds(1,0);
does nothing and adding the "h" in the arduino sketch does nothing either.

The only thing that changes anything, is going into the .cpp file of the library itself and changing line 67 from:

Code: Select all

setThreshholds(12, 6)
to

Code: Select all

setThreshholds(1, 0)

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

Thanks for the clarification. That helped narrow in on where the issue is:
https://github.com/adafruit/Adafruit_MPR121/issues/20

If you don't want to wait for the library to get updated, you can fix things now in your program by surrounding your calls to setThreshold with these register calls:

Code: Select all

  cap.writeRegister(MPR121_ECR, 0x00);
  cap.setThresholds(0x01, 0x00);
  cap.writeRegister(MPR121_ECR, 0x8F);
The first puts the MPR121 in Stop Mode so that you can actually write to the threshold registers. The second returns the MPR121 to Run Mode.

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

I've made a bit of an observation on this. So this did seem to work for a single sensor that is addressed at 0x5A, but when I try to implement a second sensor at 0x5B with different threshold values, I get a series of errors that indicated the hex values were already established in one library and not the other. Luckily, the application I'm using the sensors for right now have the same thresholds, but I think it would be beneficial to know how to get different thresholds set for sensors at different addresses. Also, if one needs to have different libraries to do so, then the previous version of the Adafruit_MPR121 library which allowed you to simply rename it, is more convenient, as the current rendition does not seem to like being renamed or reclassified (hence the errors).

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

Can you post your full code for the version where you are trying to use more than one device.

User avatar
rb4400r
 
Posts: 14
Joined: Sat Oct 28, 2017 2:49 am

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by rb4400r »

I've attached the Arduino sketch.
Attachments

[The extension ino has been deactivated and can no longer be displayed.]


User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: Using MPR121 .setThreshold command in Arduino Sketch

Post by adafruit_support_carter »

That looks fine. Can you post the complete text of the error message you are getting here:
but when I try to implement a second sensor at 0x5B with different threshold values, I get a series of errors that indicated the hex values were already established in one library and not the other.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”