arduino-libraries / Arduino_MKRIoTCarrier

Use the features included with the Arduino MKR IoT Carrier
https://store.arduino.cc/mkr-iot-carrier
GNU Lesser General Public License v2.1
18 stars 11 forks source link

Sensitivity not changed if `updateConfig` called after `carrier.begin()` #21

Open jeniara opened 3 years ago

jeniara commented 3 years ago

If I modify the example "Custom_Sensitivity" and move the carrier.begin() on line 22 and put it BEFORE line 20:

carrier.Button0.updateConfig(threshold);

it no longer respond to touch. However, if I uncomment line 16:

// CARRIER_CASE = false;

then it works again, even with carrier.begin(); before the setting.

I can not verify if the updateConfig actually change the values (yet) but I think the code should be robust to allow definition of button sensitivity before or after the begin.carrier() call.

marqdevx commented 3 years ago

Hey @jeniara could you please send me the sketch? Thanks

jeniara commented 3 years ago

Thanks! See the example code from the master, Custom_Sensitivity , modified lines 20-24 and line 8-9 (I have the case mounted).


include "Arduino_MKRIoTCarrier.h"

MKRIoTCarrier carrier;

// When CARRIER_CASE is false it's set to 100 (closer) // When CARRIER_CASE is true it's set to 4 (further) // But if you use Buttons.updateConfig(value) It will not set the above values

unsigned int threshold = 4; unsigned int threshold_btn_0 = 4;

void setup() { // put your setup code here, to run once: Serial.begin(9600); while (!Serial);

// CARRIER_CASE = true; //Now we can set our custom touch threshold // First we update all the buttons with the new threshold // Then we overwrite individually one of them (they can be all set individually too) carrier.begin(); Serial.println("carrier begin"); carrier.Buttons.updateConfig(threshold); carrier.Button0.updateConfig(threshold_btn_0); Serial.println("Updated Config"); }

void loop() { // put your main code here, to run repeatedly: carrier.Buttons.update();

// Verify your thresholds if (carrier.Button0.getTouch()) { Serial.println("touching 0"); }

if (carrier.Button1.getTouch()) { Serial.println("touching 1"); }

if (carrier.Button2.getTouch()) { Serial.println("touching 2"); }

if (carrier.Button3.getTouch()) { Serial.println("touching 3"); }

if (carrier.Button4.getTouch()) { Serial.println("touching 4"); } }

marqdevx commented 3 years ago

So, hmm it doesnt matter if you change the threshold after the carrier.begin() (theorically)

Im going to debug it tomorrow and let you know if I find something.

Thanks for the feedback.

jeniara commented 3 years ago

Now I was able to check again:

This is working

#include "Arduino_MKRIoTCarrier.h"
MKRIoTCarrier carrier;

// When CARRIER_CASE is false it's set to 100 (closer)
// When CARRIER_CASE is true it's set to 4  (further)
// But if you use Buttons.updateConfig(value) It will not set the above values

unsigned int threshold = 4;
unsigned int threshold_btn_0 = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial);

  // CARRIER_CASE = true;
  //Now we can set our custom touch threshold
  // First we update all the buttons with the new threshold
  // Then we overwrite individually one of them (they can be all set individually too)

  carrier.Buttons.updateConfig(threshold);
  carrier.Button0.updateConfig(threshold_btn_0);
  Serial.println("Updated Config");
  carrier.begin();
  Serial.println("carrier begin");
}

void loop() {
  // put your main code here, to run repeatedly:
  carrier.Buttons.update();

  // Verify your thresholds
  if (carrier.Button0.getTouch()) {
    Serial.println("touching 0");
  }

  if (carrier.Button1.getTouch()) {
    Serial.println("touching 1");
  }

  if (carrier.Button2.getTouch()) {
    Serial.println("touching 2");
  }

  if (carrier.Button3.getTouch()) {
    Serial.println("touching 3");
  }

  if (carrier.Button4.getTouch()) {
    Serial.println("touching 4");
  }
}

This is not working

#include "Arduino_MKRIoTCarrier.h"
MKRIoTCarrier carrier;

// When CARRIER_CASE is false it's set to 100 (closer)
// When CARRIER_CASE is true it's set to 4  (further)
// But if you use Buttons.updateConfig(value) It will not set the above values

unsigned int threshold = 4;
unsigned int threshold_btn_0 = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial);

  // CARRIER_CASE = true;
  //Now we can set our custom touch threshold
  // First we update all the buttons with the new threshold
  // Then we overwrite individually one of them (they can be all set individually too)
  carrier.begin();
  Serial.println("carrier begin");
  carrier.Buttons.updateConfig(threshold);
  carrier.Button0.updateConfig(threshold_btn_0);
  Serial.println("Updated Config");
}

void loop() {
  // put your main code here, to run repeatedly:
  carrier.Buttons.update();

  // Verify your thresholds
  if (carrier.Button0.getTouch()) {
    Serial.println("touching 0");
  }

  if (carrier.Button1.getTouch()) {
    Serial.println("touching 1");
  }

  if (carrier.Button2.getTouch()) {
    Serial.println("touching 2");
  }

  if (carrier.Button3.getTouch()) {
    Serial.println("touching 3");
  }

  if (carrier.Button4.getTouch()) {
    Serial.println("touching 4");
  }
}
mhavill commented 3 years ago

@jeniara - I have to agree with you - I have spent a day trying to resolve Button0 not responding when in the case. The instructions in Calibrating-the-MKR-IoT-Carrier-capacitive-buttons just don't work for 2 reasons.

//this would have updated all buttons! Should have been carrier.Button0.updateConfig(threshold_btn_0); //or carrier.Buttons.updateConfig(threshold_btn_0, Touch0);

marqdevx commented 3 years ago

Hello @mhavill @jeniara ! Could you pleasee check if you have the latest version of the Arduino_MCHPTouch (v1.2.1) and the version of the Arduino_MKRIoTCarrier is v1.0.2

Thanks

mhavill commented 3 years ago

Hi @marqdevx, I can confirm that on my IDE I have the latest versions installed.
From Cloud Editor I presume it will pick the latest version unless I specifically include an earlier version, as we don't get to 'install' the libraries?

Thanks

marqdevx commented 3 years ago

Thanks @mhavill , yes the Online Editor uses the latest version by default, I'll try the sketch and contact you with more info. Have a nice one!

lukekarrys commented 1 year ago

I think I ran into the same issue as this. What I found is that the default value of CARRIER_CASE is false and begin() always calls Buttons.updateConfig() in that case. And since you updateConfig() wont have any effect after calling begin(), in the default case, you cant change button sensitivity.

Here are the two references to the code I could find:

https://github.com/arduino-libraries/Arduino_MKRIoTCarrier/blob/b8b931ef75fe8859fa6c7743f844d64f60e3bd87/src/Arduino_MKRIoTCarrier.cpp#L25

https://github.com/arduino-libraries/Arduino_MKRIoTCarrier/blob/b8b931ef75fe8859fa6c7743f844d64f60e3bd87/src/Arduino_MKRIoTCarrier.cpp#L41-L43

This is the same thing found in https://github.com/arduino-libraries/Arduino_MKRIoTCarrier/issues/21#issuecomment-894591366, I'm just reiterating the behavior for the latest version, and a workaround below.

As of v2.0.4 if you want to update the button sensitivity, you need to do it like this.

#include "Arduino_MKRIoTCarrier.h"
MKRIoTCarrier carrier;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // make sure withCase() is called so begin() does not reset
  // the button sensitivities
  carrier.withCase();

  // then set all the button sensitivities individually
  carrier.Buttons.updateConfig(4, TOUCH0);
  carrier.Buttons.updateConfig(30, TOUCH1);
  carrier.Buttons.updateConfig(60, TOUCH2);
  carrier.Buttons.updateConfig(100, TOUCH3);
  carrier.Buttons.updateConfig(200, TOUCH4);

  // or set them all at once
  // carrier.Buttons.updateConfig(200);

  // finally call begin
  carrier.begin();
}