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);
}
cs_5_7
to pins 5 and 7, as it's name misleadingly suggests...