Go Down

Topic: My LED will get brighter? (Read 417 times) previous topic - next topic

Hi, I have question to ask about my code and want some help with it.
My code is supposed to:
1. Read how many capacitive sensors are touched
2. Determine whether PIR detects hand
3. 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);
       }
   
     }
    }
}

PaulS

Quote
My code works fine up to three capacitive sensors touched. However, when I touch four capacitive sensors, it does not work!!

Code: [Select]
        delay(1000);
        delay(2000);
         delay(3000);
         delay(40000);

One thousand, two thousand, three thousand, forty thousand...
Not quite a linear progression.

Literals are treated as ints, unless indicated otherwise. You have not indicated otherwise, so 40000 is treated as an int. Look at the reference pages to determine the size of, and maximum value that can be stored in, an int. Hint: It's less than 40,000.

I'm sorry I didn't change back 40000->4000
I was just trying to test whether it will work if it has longer delays and forgot to change it back to 4000.

//four capacitors touched
       if (sum == 4)
       {
             digitalWrite(motor, 1);
        delay(40000);
        f= f+1;
       }

should be
//four capacitors touched
       if (sum == 4)
       {
             digitalWrite(motor, 1);
        delay(4000);
        f= f+1;
       }

jraskell


I'm sorry I didn't change back 40000->4000
I was just trying to test whether it will work if it has longer delays and forgot to change it back to 4000.



Except, when treated as an int, 40000 is not greater than 4000.

PaulS

Add a call to Serial.begin() to setup().

Add calls to Serial.print() and/or Serial.println() to loop(), to confirm that the "capacitors" are being correctly read, and that sum and f are being correctly calculated.

Also, look at the documentation for the if statement. Using else if to nest the if statements makes the code more efficient, since the evaluation of the if statement stops when a true condition is found. You are counting the number of sensors returning "touched". That number can not satisfy more than one of the second set of if statements.

Go Up