1

So I have been messing around with the Arduino Capsense library with the immensely helpful guide; http://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);
}
Edgar Bonet
45.1k4 gold badges42 silver badges81 bronze badges
asked Apr 15, 2016 at 10:34
1
  • 1
    Maybe you wired sensor cs_5_7 to pins 5 and 7, as it's name misleadingly suggests... Commented Apr 15, 2016 at 12:52

1 Answer 1

1

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.

answered Apr 16, 2016 at 13:53
1
  • 1
    The library documentation says it's OK to share the send pin among several sensors. Commented Apr 18, 2016 at 7:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.