loading

Arduino capacitive touch sensor help

so I have been messing around with the Arduino Capsense library with the immensely helpful guide; https://www.instructables.com/id/How-To-Use-Touch-Sensors-With-Arduino/  (many thanks to DangerousTim!)   using the code provided, i managed to get a relay to operate with a sensor.  It worked great and I was able to adjust the distance and sensitivity.  But when i tried adding another sensor and relay, it stopped working.  I've been trying to isolate the problem for a while now and have gotten nowhere.  I'm hoping someone can help me, here's the original code for the single sensor and relay;

#include <CapacitiveSensor.h>

#include <CapacitiveSensor.h>                                          //change '42' to any desired pin...

long time = 0;
int state = HIGH;


boolean yes;

boolean previous = false;


int debounce = 200;

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2); 

// To add more sensors...
//CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
//CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil

void setup()                   
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);

   pinMode(13, OUTPUT);
 
}

void loop()                   
{
    long total1 =  cs_4_2.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; }
    else state = LOW;
    time = millis();
    }
      
      
      digitalWrite(13, state);
    
      previous = yes;
    

    
      delay(10);



}

and here's the code for the two sensors and relays

#include <CapacitiveSensor.h>


int led = 13;    
int led2 = 12;   

long time = 0;
int state = HIGH;
long time2 = 0;
int state2 = HIGH;

boolean yes;
boolean previous = false;
boolean yes2;
boolean previous2 = false;


int debounce = 200;

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


CapacitiveSensor   cs_5_7 = CapacitiveSensor(4,6);       

void setup()                   
{
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
   cs_5_7.set_CS_AutocaL_Millis(0xFFFFFFFF);  //Calibrate the sensor...
   pinMode(led, OUTPUT);
   pinMode(led2, OUTPUT);
}

void loop()                   
{
    int total1 =  cs_4_2.capacitiveSensor(30);
    Serial.println(total1);
    int total2 =  cs_5_7.capacitiveSensor(30);
     
     
   
    if (total1 > 60){yes = true;}
    else {yes = false;} 
  if (total2 > 60){yes2 = true;}
    else {yes2 = false;}  
   
    // to toggle the state of state
    if(yes == true && previous  == false && millis() - time>debounce){
     
     
       if(state == LOW){
         state = HIGH;
       }
       else
         state = LOW;
         time = millis(); 
    }
        
       
        if(yes2 == true && previous2  == false && millis() - time2>debounce){
         
        if(state2 == LOW){
         state2 = HIGH;
       }
       else
         state2 = LOW;
           time2 = millis();
      
    }
      
  
      digitalWrite(led, state);
      previous = yes;
      digitalWrite(led2, state2);
      previous2 = yes2;

    
     
      delay(10);


}

Any suggestions are always helpful.

sort by: active | newest | oldest
1-10 of 11Next »

I think you are making a mistake initializing **cs_5_7**.

CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);

CapacitiveSensor cs_5_7 = CapacitiveSensor(4,6);

You are using same pin 4 for both Capacitive sensors. Try using different pins. I am quite sure that, this would help.

AtranE1 year ago

1:int total1 = cs_4_2.capacitiveSensor(30);

2:Serial.println(total1);
3:int total2 = cs_5_7.capacitiveSensor(30);

when these three lines together the capacitance of 4.2 i have to touch it several time to turn the LED on and sevral times to turn it off. by commenting line 3 with pins 5.7, 4.2 works fine. any idea why

CraigC261 year ago

@dudes, is there a way to make this function like a press button instead of a toggle ON/OFF? As in the LED will only be HIGH when total1 > 60 while still debouncing as 1 button press?

dudes (author)  CraigC261 year ago

Absolutely! Take a look at the regular debounce sketch, the one in the examples. Simply substitute the button for the capacitive touch sensor. (ie. instead of requiing the button ipn be pulled HIGH, require the total be > 60.) From there it should be pretty easy!

caitlinsdad2 years ago

I haven't worked with capsense but it may be that each sensor needs to be on its own send/receive pin. The additional sensors hooked up to pin 4 is confusing the arduino and may change the electronic properties of the circuit ganging them up. Try to upload a picture of your wiring setup. It would help see what you are doing.

dudes (author)  caitlinsdad2 years ago

oh, forgot that part. I have one sensor on pins 4+6 with 6 as the receive pin, the other sensor is on 5+7 with 7 as the receive pin. If you need any other info I'll be glad to (attempt to) provide it.

dudes (author)  dudes2 years ago

oops pin 6 is supposed to be pin 2, my mistake

CraigC26 dudes1 year ago

is there a way to make this function like a press button instead of a toggle ON/OFF? As in the LED will only be HIGH when total1 > 60 while still debouncing as 1 button press?

so CapacitiveSensor cs_5_7 = CapacitiveSensor(4,6);

should be CapacitiveSensor cs_5_7 = CapacitiveSensor(5,7); when declaring your second sensor?

dudes (author)  caitlinsdad2 years ago

Thanks! I kinda wondered if it would be something simple like that. I don't have access to my Arduino right now, but when I do, I'll try that out and get back to you.

1-10 of 11Next »