Go Down

Topic: Cap Sensing single event while button pressed.  (Read 51 times) previous topic - next topic

I want to create a piano with cap sensing, But I only want the touch sensor to cause only one event while its pressed with out re-triggering the key while the key is still pressed. Here is some code I gathered from the Internets. 

Code: [Select]
#include <CapacitiveSensor.h>

int led = 13;                                         

long time = 0;
int state = HIGH;

boolean yes;
boolean previous = false;

int debounce = 200;

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

void setup()                   
   cs_2_4.set_CS_AutocaL_Millis(0xFFFFFFFF);  //Calibrate the sensor...
   pinMode(led, OUTPUT);

void loop()                   
    long total1 =  cs_2_4.capacitiveSensor(30);
    if (total1 > 60){yes = true;}
    else {yes = false;}   
    // to toggle the state of state
    if(yes == true && previous  == false && millis() - time>debounce){
       if(state == LOW){
         state = HIGH;
         state = LOW;
         time = millis();     
      digitalWrite(led, state);
      previous = yes;



Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131