Arduino_MKRIoTCarrier
                                
                                
                                
                                    Arduino_MKRIoTCarrier copied to clipboard
                            
                            
                            
                        Sensitivity not changed if `updateConfig` called after `carrier.begin()`
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.
Hey @jeniara could you please send me the sketch? Thanks
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"); } }
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.
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");
  }
}
                                    
                                    
                                    
                                
@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.
- Firstly, as you have pointed out the once you run carrier.begin(); you can no longer update the threshold of any button.  @marqdevx, it would be great if threshold could be modified after begin().  I set up a widget on a dashboard to be able to tune the threshold on the fly but because it was fixed after begin () it was ineffective.  I have got all buttons working in the case with
CARRIER_CASE = true; carrier.Buttons.updateConfig(50, TOUCH0); carrier.begin(); - Secondly, the code in the calibration article was incorrect in the implementation of the intention to set Button0 independently in the Calibration example:
 
unsigned int threshold = 98;
unsigned int threshold_btn_0 = 95;
...
  carrier.begin();
  //CARRIER_CASE = false;
  //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.Buttons.updateConfig(threshold_btn_0);** 
//this would have updated all buttons!  Should have been
carrier.Button0.updateConfig(threshold_btn_0); //or
carrier.Buttons.updateConfig(threshold_btn_0, Touch0);
                                    
                                    
                                    
                                
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
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
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!
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();
}