This one is really bizarre to my eyes...
If I create a global CapacitiveSensor object the library detects touches just fine.
Bit if I create an object on the stack as a function parameter the library fails completely and returns 0.
WHY?
I can't see anything in the constructor for CapacitiveSensor that would obviously cause this bizarre behavior!
Sketch:
#include <CapacitiveSensor.h>
#include "Manager.h"
#include "Buttons.h"
#include "Debug.h"
CManager Manager;
void setup()
{
Serial.begin(115200);
while (!Serial);
Manager.begin();
Serial.println(F("Setup complete!"));
}
CapacitiveSensor csButton(A1, A0);
void loop()
{
//CCapacitiveButton cbInc(A1, A0, "INC");
//CapacitiveSensor csButton(A1, A0);
//cbInc.poll();
debug.log(csButton.capacitiveSensor(30));
delay(500);
//Manager.poll();
}
Constructor:
CapacitiveSensor::CapacitiveSensor(uint8_t sendPin, uint8_t receivePin)
{
// initialize this instance's variables
// Serial.begin(9600); // for debugging
error = 1;
loopTimingFactor = 310; // determined empirically - a hack
CS_Timeout_Millis = (2000 * (float)loopTimingFactor * (float)F_CPU) / 16000000;
CS_AutocaL_Millis = 20000;
// Serial.print("timwOut = ");
// Serial.println(CS_Timeout_Millis);
// get pin mapping and port for send Pin - from PinMode function in core
#ifdef NUM_DIGITAL_PINS
if (sendPin >= NUM_DIGITAL_PINS) error = -1;
if (receivePin >= NUM_DIGITAL_PINS) error = -1;
#endif
pinMode(sendPin, OUTPUT); // sendpin to OUTPUT
pinMode(receivePin, INPUT); // receivePin to INPUT
digitalWrite(sendPin, LOW);
sBit = PIN_TO_BITMASK(sendPin); // get send pin's ports and bitmask
sReg = PIN_TO_BASEREG(sendPin); // get pointer to output register
rBit = PIN_TO_BITMASK(receivePin); // get receive pin's ports and bitmask
rReg = PIN_TO_BASEREG(receivePin);
// get pin mapping and port for receive Pin - from digital pin functions in Wiring.c
leastTotal = 0x0FFFFFFFL; // input large value for autocalibrate begin
lastCal = millis(); // set millis for start
}