ianhattwick
New member
As discussed in this old thread, you can change the sensitivity of the capacitive sensing performed by touchRead. To do this, like Paul suggested, you can go in and change some defined constants in the touch.c file -> hardware/teensy/cores/teensy3/touch.c. I went ahead and wrote a new function to allow for changing those values directly from my teensy code and thought I'd share.
There are three things that need to happen to do this:
1) Replace the defined values with variables. In touch.c the defined variables which set touchRead sensitivity are CURRENT, NSCANS, and PRESCALE. I commented out each occurrence of those defines (one for each teensy core, if you look at the touch.c code) and replaced them with new variables:
uint8_t CURRENT;
uint8_t NSCAN;
uint8_t PRESCALE;
To make it easy, here is the code from the first #if to the last #endif - the only change is the new variables:
2) Add the function to change the variables from your teensy sketch:
3) Add the function prototype - this is in the hardware/teensy/cores/teensy3/core_pins.h file. I added the new prototype right below the touchRead() prototype, line 1480.
And, voila, the ability to change the sensitivity of the touchRead capacitive sensing on the fly -- just call the function: setTouchReadSensitivity(9,5,2).
I've only just done this today and it seems to work without problems, but YMMV.
There are three things that need to happen to do this:
1) Replace the defined values with variables. In touch.c the defined variables which set touchRead sensitivity are CURRENT, NSCANS, and PRESCALE. I commented out each occurrence of those defines (one for each teensy core, if you look at the touch.c code) and replaced them with new variables:
uint8_t CURRENT;
uint8_t NSCAN;
uint8_t PRESCALE;
To make it easy, here is the code from the first #if to the last #endif - the only change is the new variables:
Code:
#if defined(__MK20DX128__) || defined(__MK20DX256__)
// These settings give approx 0.02 pF sensitivity and 1200 pF range
// Lower current, higher number of scans, and higher prescaler
// increase sensitivity, but the trade-off is longer measurement
// time and decreased range.
uint8_t CURRENT = 2;
uint8_t NSCAN = 9;
uint8_t PRESCALE = 2;
//#define CURRENT 2 // 0 to 15 - current to use, value is 2*(current+1), default 2
//#define NSCAN 9 // number of times to scan, 0 to 31, value is nscan+1, default 9
//#define PRESCALE 2 // prescaler, 0 to 7 - value is 2^(prescaler+1), default 2
static const uint8_t pin2tsi[] = {
//0 1 2 3 4 5 6 7 8 9
9, 10, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 13, 0, 6, 8, 7,
255, 255, 14, 15, 255, 12, 255, 255, 255, 255,
255, 255, 11, 5
};
#elif defined(__MK66FX1M0__)
uint8_t CURRENT = 2;
uint8_t NSCAN = 9;
uint8_t PRESCALE = 2;
//#define CURRENT 2
//#define NSCAN 9
//#define PRESCALE 2
static const uint8_t pin2tsi[] = {
//0 1 2 3 4 5 6 7 8 9
9, 10, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 13, 0, 6, 8, 7,
255, 255, 14, 15, 255, 255, 255, 255, 255, 11,
12, 255, 255, 255, 255, 255, 255, 255, 255, 255
};
#elif defined(__MKL26Z64__)
uint8_t CURRENT = 9;
uint8_t NSCAN = 2;
//#define NSCAN 9
//#define PRESCALE 2
static const uint8_t pin2tsi[] = {
//0 1 2 3 4 5 6 7 8 9
9, 10, 255, 2, 3, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 13, 0, 6, 8, 7,
255, 255, 14, 15, 255, 255, 255
};
#endif
2) Add the function to change the variables from your teensy sketch:
Code:
void setTouchReadSensitivity(uint8_t t_current, uint8_t num_scans, uint8_t t_prescale){
//check the new values are in range
if(t_current>15) t_current=15;
if(num_scans>31) num_scans=31;
if(t_prescale>7) t_prescale=7;
//update the variables
CURRENT=t_current; //0 to 15 - current to use, value is 2*(current+1), default 2
NSCAN=num_scans; //number of times to scan, 0 to 31, value is nscan+1, default 9
PRESCALE=t_prescale; //prescaler, 0 to 7 - value is 2^(prescaler+1), default 2
}
3) Add the function prototype - this is in the hardware/teensy/cores/teensy3/core_pins.h file. I added the new prototype right below the touchRead() prototype, line 1480.
Code:
void setTouchReadSensitivity(uint8_t t_current, uint8_t num_scans, uint8_t t_prescale);
And, voila, the ability to change the sensitivity of the touchRead capacitive sensing on the fly -- just call the function: setTouchReadSensitivity(9,5,2).
I've only just done this today and it seems to work without problems, but YMMV.