Cap Sense Question - Switch Action

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


My Question is can anyone share a program to change the switch conditon so that it is an alternate action as opposed to momentary, I am still learning C and arduino but need to get this sorted ASAP and would like help to achieve this ???

The "Digital:BlinkWithoutDelay" example shows how to take a periodic event (in that case it's the expiration of a time interval) and use that to toggle a state (the state of the LED). Look at what is done to "ledState" when the timer interval ends. You need to do something similar to toggle between your ON and OFF states.

Also look at the "Digital:Debounce" example for how to detect when an input (in that case it's a button) has transitions to a new state and has been stable for a while.

I ran the debounce program and it seems to have a mind of it's own, Is it supposed to change states by itself ? Mine does

The Debounce example has problems. It requires an external pull-down resistor on pin 2 and a button connecting pin 2 to +5. It also doesn't act the way the comments say it does:

"Each time the input pin goes from LOW to HIGH (e.g. because of a push-button press), the output pin is toggled from LOW to HIGH or HIGH to LOW."

What it ACTUALLY does is turn the LED on when the button reads HIGH and turn it off when the button reads LOW.

I've re-written the example to A) use the internal pull-up so you only need a button between pin 2 and Ground, and B) it actually toggles the LED on each button press. Perhaps this version would make a better example for you:

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // turn on the internal pull-up
  pinMode(ledPin, OUTPUT);

void loop()
  int currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != lastButtonState) {
    lastDebounceTime = millis();

  if ((millis() - lastDebounceTime) > debounceDelay && 
    currentButtonState != buttonState) {
    // We have a new and different button state   
    buttonState = currentButtonState;
    if (buttonState == LOW) // Pressed
      // The new state is "Pressed"
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
  lastButtonState = currentButtonState;