Hi, I have question to ask about my code and want some help with it.
My code is supposed to:
- Read how many capacitive sensors are touched
- Determine whether PIR detects hand
- Output high signal for certain delay for different number of capacitive sensors read
(ex) one capacitive sensors read->output high for one second
two capacitive sensors read->output high for two seconds and so forth
For reference, instead connecting the output directly to motor, I connected to a LED to check whether the code functions correctly.
HOWEVER,,,
My code works fine up to three capacitive sensors touched. However, when I touch four capacitive sensors, it does not work!!
In addition, even though I've only changed the length of duration of "HIGH" signal, the brightness of LED changes!! (ex) LED is brighter when two capacitive sensors are touched compared to when one capacitive sensor is touched
Please help me!
My code is as follow:
//sensePin is output from PIR sensor (input to ARduino)
int sensePin = 0;
//pin assigned for capacitive sensors
int ts1 = 15;
int ts2 = 14;
int ts3 = 13;
int ts4 = 12;
//pin output to motor
int motor = 2;
void setup(){
pinMode(motor, OUTPUT);
}
//loop
void loop(){
int val = analogRead(sensePin);
//threshold for PIR sensor
if(val > 300)
{
int f = 0;
int sum = 0;
while (f == 0){
int a = analogRead(ts1);
int b = analogRead(ts2);
int c = analogRead(ts3);
int d = analogRead(ts4);
//check capone is touched
if(a < 200)
{
sum = sum + 1;
}
//check captwo is touched
if(b < 200)
{
sum = sum + 1;
}
//check capthree is touched
if(c < 200)
{
sum = sum + 1;
}
//check capfour is touched
if(d < 200)
{
sum = sum + 1;
}
//one capacitor touched
if (sum == 1)
{
digitalWrite(motor, 1);
delay(1000);
f= f+1;
}
//two capacitors touched
if (sum == 2)
{
digitalWrite(motor, 1);
delay(2000);
f= f+1;
}
//three capacitors touched
if (sum == 3)
{
digitalWrite(motor, 1);
delay(3000);
f= f+1;
}
//four capacitors touched
if (sum == 4)
{
digitalWrite(motor, 1);
delay(40000);
f= f+1;
}
//set motor LOW when nothing pressed
else
{
digitalWrite(motor, LOW);
}
}
}
}