Downloaded the CapacitiveSensor library and got a working touch sensor made using Arduino Leonardo. Attached is a picture of the set-up.
Basically, everything works, but I have a few questions I want to clarify since this is part of a report I have to write.
1) How do I increase the sensitvity of the pin? It says that the pin can actually sense a human touch a quarter inch away, but the serial monitor doesn't show any change unless I actually come into contact with the metal pin. I tried using a metal plate as a touch sensor but I still have to place my finger on the sensor.
The next few questions are regarding the code and what it means:
#include <CapacitiveSensor.h>
CapacitiveSensor capSensor_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor capSensor_2_6 = CapacitiveSensor(2,6);
const int pinR = 13;
const int pinG = 12;
const int pinB = 11;
void setup() {
Serial.begin(9600);
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
digitalWrite(pinG, LOW);
}
void loop() {
long value1=capSensor_2_4.capacitiveSensor(30);
long value2=capSensor_2_6.capacitiveSensor(30);
Serial.println(value1);
Serial.print("\t");
Serial.println(value2);
delay(10);
// First Pin | Red Light
if (value1>100)
digitalWrite(pinR, HIGH);
else
digitalWrite(pinR, LOW);
// Second Pin | Blue Light
if (value2>100)
digitalWrite(pinB, HIGH);
else
digitalWrite(pinB, LOW);
}
2.1) In the above code, what do value1 and value2 refer to? Do they refer to the RC time constant needed for the receive pin to change state (charge/discharge), or something else?
2.2) Also, why do we need to take 30 samples for value1 and value2? When I change it to one sample (long value1=capSensor_2_4.capacitiveSensor(1)) both give a value of zero on the serial monitor for 90% of the time. What does taking samples mean and how does it affect value1&2 or the accuracy thereof?
This last question(s) is kinda related to the above few, but I'm putting them in because they've been puzzling me for quite a while now:
#include <CapacitiveSensor.h>
CapacitiveSensor capSensor_2_4 = CapacitiveSensor(2,4);
CapacitiveSensor capSensor_2_6 = CapacitiveSensor(2,6);
const int pinR = 13;
const int pinG = 12;
const int pinB = 11;
void setup() {
Serial.begin(9600);
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
digitalWrite(pinG, LOW);
}
void loop() {
// First Pin | Red Light
long oldValueR=capSensor_2_4.capacitiveSensor(30);
delay(10);
long newValueR=capSensor_2_4.capacitiveSensor(30);
long differenceR = newValueR - oldValueR;
Serial.println(differenceR);
delay(10);
if (differenceR>15 || differenceR<-15)
digitalWrite(pinR, HIGH);
else
digitalWrite(pinR, LOW);
oldValueR = newValueR;
// Second Pin | Blue Light
long oldValueB=capSensor_2_6.capacitiveSensor(30);
delay(10);
long newValueB=capSensor_2_6.capacitiveSensor(30);
long differenceB = newValueB - oldValueB;
Serial.print("\t");
Serial.println(differenceB);
delay(10);
if (differenceB>15 || differenceB<-15)
digitalWrite(pinB, HIGH);
else
digitalWrite(pinB, LOW);
oldValueB = newValueB;
}
I wrote a variant of the previous code that uses the difference between the current and the previous value of...whatever value1 & value2 is (refer Q2.1) as the stimulus to light the LED up.
3.1) When observing the values recorded in the serial monitor, I observed that touching the sensor sometimes leads to negative numbers being displayed, hence the code if (differenceB>15 || differenceB<-15.
Why is it that the difference can become negative? In the previous code, the serial monitor numbers (ie value1 and value2) will always increase from a small number to a very huge number (eg 10 -> 2000). Hence logically when I compare newValue with oldValue (which are basically value1/2 compared before and after), the difference should also be positive. That is not the case as observed in the screenshot attached. The numbers go
10, -13, 12, -30
which is a net negative change. Why does this happen and what does it signify?
Any help would be greatly appreciated. Thank you!