#Include CapacitiveSensor library

@Tangibit great work :slight_smile: I was wondering what the typical delta t was between not being touched and being touched in your example?

Do you find that microseconds granularity is sometimes not precise enough? If so there is another free-running counter in the Spark core you could use that runs at a 72th of a microsecond (13.89ps aka 1/72MHz aka the system clock)

You can see it used to generate the micros() output:

unsigned long micros(void)
{
	return (DWT->CYCCNT / SYSTEM_US_TICKS);
}

basically calling DWT->CYCCNT gives you the current timer count. SYSTEM_US_TICKS = 72.

Also when you subtract the previous UNSIGNED LONG count of DWT->CYCCNT from the current UNSIGNED LONG count of DWT->CYCCNT you naturally defeat (with coding wizardry) the case when the DWT->CYCCNT count wraps.

uint32 LAST_DWT = DWT->CYCCNT;
while( (DWT->CYCCNT - LAST_DWT) > 72 ) {
  // One microsecond has elapsed since LAST_DWT
  // DWT->CYCCNT can also wrap and it won't affect us 
  // if done as above using the power of subtraction :) 
  break;
}