Go Down

Topic: CapSense (Read 1 time) previous topic - next topic


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
* Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin

CapSense   cs_4_2 = CapSense(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired

int led13 = 13;
void setup()                   

   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example


void loop()                   
    long start = millis();
    long total1 =  cs_4_2.capSense(30);

if (total1 >= 30) // when pressed serial reads approx 60, 50% of this is 30
  digitalWrite (led13, HIGH);
  digitalWrite (led13, LOW);



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:
Code: [Select]

boolean buttonOn = false;
int counter = 0;
int threshold = 30;
int debounceThreshold = 10;

void loop() {
  // get input from capsense
  if( total > threshold)
    counter = 0;

  if(counter > debounceThreshold)
    buttonOn = !buttonOn;
Creative technologist + OSHW engineer + lover of learning interested in bio+art+tech, form and digital fabrication


Thank You Very Much that is exactly what i need.

Go Up