capacitive sensor with no ground

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;
}