I have ~90 'touch buttons' (an electrode with two cables and a 1 MOhm resistor, I need ~91 digital I/O pins) which I want to operate with the CapSense library. The issue is that I don't have enough I/O pins to do this. I did some research and found out that a chip like the MCP23S17 (library) can extend the number of I/O pins, but I am not sure if it works in combination with the CapSense library.
I know you can easily replace things like DigitalWrite, DigitalRead and PinMode with the MCP23S17 library, but I am unable to replace the code below:
(File: CapacitiveSensor.cpp, CapSense library)
(...)
sBit = digitalPinToBitMask(sendPin); // get send pin's ports and bitmask
sReg = PIN_TO_BASEREG(sendPin); // get pointer to output register
rBit = digitalPinToBitMask(receivePin); // get receive pin's ports and bitmask
rReg = PIN_TO_BASEREG(receivePin);
(...)
noInterrupts();
DIRECT_WRITE_LOW(sReg, sBit); // sendPin Register low
DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT
DIRECT_WRITE_LOW(rReg, rBit); // pin is now LOW AND OUTPUT
delayMicroseconds(10);
DIRECT_MODE_INPUT(rReg, rBit); // receivePin to input (pullups are off)
DIRECT_WRITE_HIGH(sReg, sBit); // sendPin High
interrupts();
while ( !DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is LOW AND total is positive value
total++;
}
//Serial.print("SenseOneCycle(1): ");
//Serial.println(total);
if (total > CS_Timeout_Millis) {
return -2; // total variable over timeout
}
// set receive pin HIGH briefly to charge up fully - because the while loop above will exit when pin is ~ 2.5V
noInterrupts();
DIRECT_WRITE_HIGH(rReg, rBit);
DIRECT_MODE_OUTPUT(rReg, rBit); // receivePin to OUTPUT - pin is now HIGH AND OUTPUT
DIRECT_WRITE_HIGH(rReg, rBit);
DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
DIRECT_WRITE_LOW(sReg, sBit); // sendPin LOW
interrupts();
#ifdef FIVE_VOLT_TOLERANCE_WORKAROUND
DIRECT_MODE_OUTPUT(rReg, rBit);
DIRECT_WRITE_LOW(rReg, rBit);
delayMicroseconds(10);
DIRECT_MODE_INPUT(rReg, rBit); // receivePin to INPUT (pullup is off)
#else
while ( DIRECT_READ(rReg, rBit) && (total < CS_Timeout_Millis) ) { // while receive pin is HIGH AND total is less than timeout
total++;
}
#endif
(...)
Does anyone know how to do this / know a better alternative?