PDA

View Full Version : Capacitive touch - trapping a long press?



HankMcSpank
- 9th June 2011, 09:14
So I've been dabbling with capacitive touch - trapping a short 'press' (touch) is pretty simply, but I'm just wondering how to go about approaching ID'ing a long 'press' - anyone ventured into this area before?

rsocor01
- 9th June 2011, 11:39
The reading will stay low as long as the metal sensor is being touched. Add some logic to that and you should be able to do it.

Robert

HankMcSpank
- 9th June 2011, 12:49
I agree...but that's what I'm asking!

So to trap a quick touch, you just keep checking the frequency from the output of the CPS module ...via a fixed timebase (timer interrupt) - if the frequency has deviated down by say 20% or more than a sensor has been touched (to trap the deviation I just compare the last sampled count with the present)

But the problem with holding a finger on a sensor, is that between successive interupts the frequency count will be more or less the same between successive interrupt ...so I guess I'm trying to wrap my head around the logic!

I guess if you have an average 'normal count' (where nothing is touched) stored away somewhere, then you can trap when a finger is being held on the sensor ...but the problem with 'normal count' & capacitive touch is that the frequency can drift (temperature humidity etc)...so the 'normal count' itself moves around! This isn't really a problem with a quick touch, becuase all I'm doing is comparing the present count with the last count ...these are only one short interrupt away from one another (the 'normal count isn't likely to drift that much under such circumstances)

Need to mull it over a bit longer I guess...but thought it worth asking here as chances are someone has addressed this before!

rsocor01
- 10th June 2011, 03:23
You might be able to use some code for a long press in a regular pushbutton and convert it to what you need. You just need to have a variable Last_High_Reading in order to compare the current readings to that last high reading. If the variable Current_Reading is not below 80% of Last_High_Reading then the sensor was released. I have written some code for a long press in a regular pushbutton but not for a Capacitive Touch Sensor.

http://www.picbasic.co.uk/forum/showthread.php?t=6941&highlight=long+press
http://www.picbasic.co.uk/forum/showthread.php?t=14420&highlight=long+press

Robert

HankMcSpank
- 10th June 2011, 09:22
You might be able to use some code for a long press in a regular pushbutton and convert it to what you need. Robert

Hi Robert, unfortunately, I don't think that would be possible.

With a regular button/switch press using interrupts, firstly an interrupt happens (ie when the switch is pressed), then in the interrupt routine the rogram just rechecks after a short while to see if the same condition is held.

But with capacitive touch, you have to exit the interrupt routine in order to get the next 'timebase' (the timebase here being 'time' between a timer based based interrupt - & is used to count the output from the CPS module), in other words you can't hang around in the interrupt routine to see if the condition has held.

cncmachineguy
- 10th June 2011, 10:14
HI Hank. Here I go with my counters again. Once you detect a "press", the pressed condition becomes the "normal" Then you are really checking for "unpress". Check the time between the 2, and determine if it was a long press short press or whatever. So the counter is used to count the time while waiting for the release.

rsocor01
- 10th June 2011, 10:20
I was thinking of some logic outside the interrrupt routine, in the main program. In the interrupt routine you find the variables Last_High_Reading and Current_Reading. Even though the main program is stuck in a loop, it will still be going to the interrupt routine. I was thinking of something like this,



Cnt = 0
REPEAT 'Main program will be in this loop until sensor is released.
Cnt = Cnt + 1
Pause 2
If Cnt > 500 then RETURN 'A long press was detected. Exit sub-routine.
UNTIL Current_Reading > 0.8*Last_High_Reading

HankMcSpank
- 10th June 2011, 23:51
Thanks guys....I'll give both of those ideas some serious consideration as I hadn't considered either.