I am trying to set up a capacitive sensor on an attiny. I have it working fine when the power is supplied from an arduino but when I cut it off and only do battery it goes completely haywire. I am assuming this is because I am cutting off a large ground. I just ordered a 7 segment display to see what the readings are like when its cut from ground since I can't do serial monitor with it unplugged
any suggestions on how to handle this would be great. I rather not have to connect it to a large ground (like Arduino Playground - HomePage suggests).
pwillard:
That's not making sense. battery power should not matter.
why would it not make sense? You are completely removing the ground from the system. attiny grounded through arduino through usb through computer through house...right? when you go battery the ground from house is removed, hence changing the values.
I too saw this same effect of the sensor not working when on battery verses on the "mains". I made some slight modifications to the CapacitiveSensor.cpp library file that seems to have fixed this issue. Essentially, I wanted to drive the circuit to an output set low as to connect it to ground. This helps to drain the capacitance on each iterative cycle so that each measurement cycle starts from zero. In addition, I also added a timer to the loop to help allow the discharge be complete before the next measurement.
int CapacitiveSensor::SenseOneCycle(void)
{
  noInterrupts();
*sOut &= ~sBit; Â // set send pin to INPUTÂ Â (<<<<<<<<<<<< Change here <<<<<<<<<<<<)
*sOut &= ~sBit;Â Â Â Â // set Send Pin Register low
*rReg &= ~rBit;Â Â Â Â // set receivePin to input
*rOut &= ~rBit;Â Â Â Â // set receivePin Register low to make sure pullups are off
*rReg |= rBit;Â Â Â Â // set pin to OUTPUT - pin is now LOW AND OUTPUT
*rReg &= ~rBit;Â Â Â Â // set pin to INPUT
*sOut |= sBit;Â Â Â Â // set send Pin High
  interrupts();
while ( !(*rIn & rBit)Â && (total < CS_Timeout_Millis) ) {Â // while receive pin is LOW AND total is positive value
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();
*rOut |= rBit;    // set receive pin HIGH - turns on pullup
*rReg |= rBit;Â Â Â Â // set pin to OUTPUT - pin is now HIGH AND OUTPUT
*rReg &= ~rBit;Â Â Â Â // set pin to INPUT
*rOut &= ~rBit;   // turn off pullup
    *sOut |= sBit; // set send pin to OUTPUT  (<<<<<<<<<< change here <<<<<<<<<<<<)
*sOut &= ~sBit;Â Â Â Â // set send Pin LOW
  interrupts();
long CapacitiveSensor::capacitiveSensorRaw(uint8_t samples)
{
total = 0;
if (samples == 0) return 0;
if (error < 0) return -1;Â Â Â Â Â Â Â Â Â // bad pin - this appears not to work
for (uint8_t i = 0; i < samples; i++) {Â Â // loop for samples parameter - simple lowpass filter
if (SenseOneCycle() < 0)Â return -2;Â // variable over timeout
delay(5);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // (<<<<<<<<<<< Change Here <<<<<<<<<<<<<<<<<<<)
}
return total;
}