I'm not sure if I'm misusing this method (though I don't think I am), because it doesn't work as expected.
I tend to get mostly garbled output, comprised of some combination of slices of the real data.
ex:
expected -
30066
28261
30575
I have 4 sensors, all set up the same way, so I'll just show the code for one of them (this is within the loop function):
long total1 = cs_4_2.capacitiveSensor(30);
if (total1 != 0){
Serial.print(total1);
Serial.print("\n");
}
...
delay(100)
My ultimate goal is very simple, I just want to be able to distinguish between which sensor was touched. If there is a different way to do this, I'm open to using it.
Ok, here is the entire sketch. Taken (obviously) from a demo on using capacitive sensors. I'm using electric paint with sensors attached to simply send a message to my app saying which sensor was touched. The comments are just variations I've tried to get proper output.
#include <CapacitiveSensor.h>
/*
* CapitiveSense Library Demo Sketch
* Paul Badger 2008
* Uses a high value resistor e.g. 10 megohm between send pin and receive pin
* Resistor effects sensitivity, experiment with values, 50 kilohm - 50 megohm. Larger resistor values yield larger sensor values.
* Receive pin is the sensor pin - try different amounts of foil/metal on this pin
* Best results are obtained if sensor foil and wire is covered with an insulator such as paper or plastic sheet
*/
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10 megohm resistor between pins 4 & 2, pin 2 is sensor pin, add wire, foil
CapacitiveSensor cs_4_5 = CapacitiveSensor(4,6); // 10 megohm resistor between pins 4 & 6, pin 6 is sensor pin, add wire, foil
CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10 megohm resistor between pins 4 & 8, pin 8 is sensor pin, add wire, foil
CapacitiveSensor cs_4_10 = CapacitiveSensor(4,10); // 10 megohm resistor between pins 4 & 10, pin 10 is sensor pin, add wire, foil
void setup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600);
}
void loop()
{
long start = millis();
long total1 = cs_4_2.capacitiveSensor(30);
long total2 = cs_4_5.capacitiveSensor(30);
long total3 = cs_4_8.capacitiveSensor(30);
long total4 = cs_4_10.capacitiveSensor(30);
if (total1 != 0){
// Serial.print("one");
Serial.println(total1);
//Serial.print("\n");
}
if (total2 != 0){
// Serial.println("two");
Serial.println(total2);
// Serial.print("\n");
}
if (total3 != 0){
// Serial.println("three");
Serial.println(total3);
// Serial.print("\n");
}
if (total4 != 0){
// Serial.println("four");
Serial.println(total4);
// Serial.print("\n");
}
delay(100);
}
Thank you for the replies. I tried adding the 'L' to my conditionals, which seemed to work for about 50 prints but then went back to splitting and chopping things.
While I haven't found the cause of my malformed string/multi-digit outputs, I changed it so I'm just printing integers instead of trying to print words (a single digit integer can't be split onto multiple lines..), which is sufficient for my case.
I am interested to know why I had an issue with this, but I'd guess it's something very specific to my setup and so won't be of much value to anyone else.