I am new to Arduino so please give me some advice on this really simple problem. I am currently using an Arduino Mega in a school project to make 4 capacitive sensors which are supposed to receive an input when you touch the sensors, and I have programmed the arduino such that you will light up an LED upon touching the sensors.
Now the problem is after I light up the LEDs the serial data monitor gives me many different numbers when I am touching the sensors. Is it actually possible for me to tweak my program such that outputs of my sensor will show up as a single line of '0' when the sensor is not being touched and '1' when the sensor is being touched? This would really help me in the quantifying of some of the data I would wish to collect from my experiments with the sensors.
My code is
#include <CapacitiveSensor.h>
CapacitiveSensor cs_2_4 = CapacitiveSensor(2,4); // 1M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor cs_5_7 = CapacitiveSensor(5,7);
CapacitiveSensor cs_8_10 = CapacitiveSensor(8,10);
CapacitiveSensor cs_11_13 = CapacitiveSensor(11,13);
void setup()
{
cs_2_4.set_CS_AutocaL_Millis(0xFFFFFFFF);// turn off autocalibrate on channel 1
cs_5_7.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_8_10.set_CS_AutocaL_Millis(0xFFFFFFFF);
cs_11_13.set_CS_AutocaL_Millis(0xFFFFFFFF);
Serial.begin(9600);
pinMode(3,OUTPUT);
pinMode(6,OUTPUT);
pinMode(9,OUTPUT);
pinMode(12,OUTPUT);
}
void loop()
{
long sensor1 = cs_2_4.capacitiveSensor(50);
long sensor2 = cs_5_7.capacitiveSensor(50);
long sensor3 = cs_8_10.capacitiveSensor(50);
long sensor4 = cs_11_13.capacitiveSensor(50);
Serial.print("\t");
Serial.println(sensor1, BIN); // print sensor output
Serial.print("\t");
Serial.print(sensor2, BIN);
Serial.print("\t");
Serial.print(sensor3, BIN);
Serial.print("\t");
Serial.print(sensor4, BIN);
if(sensor1 >= 1000)
{
digitalWrite(3,HIGH);
}
else{
digitalWrite(3,LOW);
}
if(sensor2 >= 1000)
{
digitalWrite(6,HIGH);
}
else{
digitalWrite(6,LOW);
}
if(sensor3 >= 1000)
{
digitalWrite(9,HIGH);
}
else{
digitalWrite(9,LOW);
}
if(sensor4 >= 1000)
{
digitalWrite(12,HIGH);
}
else{
digitalWrite(12,LOW);
}
}
and the output that I end up receiving is
Hence it would be really great if I could tweak my code such that the output on the serial monitor would be '0' when there is no output(Not touching sensor) and '1' when there is an output (touching sensor)
Thanks a lot for the help once again!