Hi,
I would like to turn Capsense into an alternate action switch rather then a momentry action switch.
I would like to add the above with appropriate debounce.
Here is what i have so far.
#include <CapSense.h>
/*
CapitiveSense Library Demo Sketch
Paul Badger 2008
Uses a high value resistor e.g. 10M between send pin and receive pin
Pay attention, he says he wants to 1) use a capacitive button as a toggle switch and 2) debounce the input from CapSense.
@BlackSnake: Firstly, how have you implemented the hardware? Do you have a soft touch area or a rigid one? With capacitive sensing, you may not really need to do debouncing, but I'll share one way you can if you think you need to.
There are MANY ways to debounce an input, but in this case you should probably using a summing threshold. Create a variable that will be used as a counter. Now, increment this counter every time total1 goes over your threshold (just like you are lighting the LED). If total1 is less than the threshold, reset the counter.
Right after you've incremented the counter, check to see that the counter itself has reached a certain threshold. In other words, you can use a counter to make sure that the switch has been pressed for X amount of clock cycles (which in reality could be a very short period of time). As soon as the counter has reached a certain threshold, you know that you can then toggle a boolean variable that indicates the state of the switch.
Here's some pseudo-code:
boolean buttonOn = false;
int counter = 0;
int threshold = 30;
int debounceThreshold = 10;
void loop() {
// get input from capsense
if( total > threshold)
counter++;
else
counter = 0;
if(counter > debounceThreshold)
buttonOn = !buttonOn;
}