LED's remain in HIGH state


i using new arduino uno (i believe rev.2).
arduino powered externally 5.5v supply.
i breaboarded pir, ldr, , 3 led's. (code attached)...first timer, code hacked pretty bad think.
the pir powered externally 5.5v supply.
simply, when dark , motion detected, 1 led fades on , off, other 2 turn on , continues until motion stops. @ point works fine. (its still dark)
problem is: if start in dark condition, create motion activating pir, led's come on...but if turn lights on while led's on, stay on, don't go off when motion stops.

code: [select]
/*
this example shows output of analogread() of photocell.
by m.gonzalez
www.codingcolor.com
the example code in public domain
*/

int photocellpin = 0;// photocell connected analog pin 0
int photocellval = 0; // define photocell variable
int ledpin = 9;// led connected digital pin 9
int ledstate = 0;//state of led
int fadedown = 30;//delay per fade
int fadeup = 20;//delay per fade
int minlight = 20;//min light threshold
int maxlight = 20;//max light threshold
//the time give sensor calibrate (10-60 secs according datasheet)
int calibrationtime = 30;        

//the time when sensor outputs low impulse
long unsigned int lowin;        

//the amount of milliseconds sensor has low
//before assume motion has stopped
long unsigned int pause = 5000;  

boolean locklow = true;
boolean takelowtime;  

//int ldrpin = 11;
int pirpin = 2;    //the digital pin connected pir sensor's output
int ledpin1 = 3;
int ledpin2 = 4;
int ledpin3 = 5;


void setup() {
serial.begin(9600);
 pinmode(photocellpin, input);
 pinmode(ledpin, output);
 pinmode(pirpin, input);
 //pinmode (ldrpin, input);
 pinmode(ledpin1, output);
 pinmode (ledpin2, output);
 pinmode (ledpin3, output);
 digitalwrite(pirpin, low);

 //give sensor time calibrate
 serial.print("calibrating sensor ");
   for(int = 0; < calibrationtime; i++){
     serial.print(".");
     delay(1000);
     }
   serial.println(" done");
   serial.println("sensor active");
   delay(50);
}
void loop() {
 photocellval = analogread(photocellpin);

 if (photocellval < minlight , ledstate == 0){
   digitalread(pirpin);
   
 if(digitalread(pirpin) == high){
      // fade in min max in increments of 5 points:
 for(int fadevalue = 0 ; fadevalue <= 255; fadevalue +=5) {
   // sets value (range 0 255):
   analogwrite(ledpin1, fadevalue);        
   // wait 30 milliseconds see dimming effect    
   delay(30);
   digitalwrite(ledpin2, high);
   digitalwrite(ledpin3, high);
      if(locklow){  
        //makes sure wait transition low before further output made:
        locklow = false;            
        serial.println("---");
        serial.print("motion detected @ ");
        serial.print(millis()/1000);
        serial.println(" sec");
        delay(50);
        }        
        takelowtime = true;
      }
   //serial.println("fade up");
 }                      
   
   if(photocellval > minlight , ledstate == 1);
   digitalread(pirpin);
   if(digitalread(pirpin) == low){      
      digitalwrite(ledpin1, low);  //the led visualizes sensors output pin state
      digitalwrite(ledpin2, low);
      digitalwrite(ledpin3, low);
         
           
      if(takelowtime){
       lowin = millis();          //save time of transition high low
       takelowtime = false;       //make sure done @ start of low phase
       }
      //if sensor low more given pause,
      //we assume no more motion going happen
      if(!locklow && millis() - lowin > pause){  
          //makes sure block of code executed again after
          //a new motion sequence has been detected
          locklow = true;                        
          serial.print("motion ended @ ");      //output
          serial.print((millis() - pause)/1000);
          serial.println(" sec");
          delay(50);
      }
   }
 }
}

 
moderator edit: [code] [/code] tags added.

    if(photocellval > minlight and ledstate == 1);
and   &&


Arduino Forum > Using Arduino > LEDs and Multiplexing > LED's remain in HIGH state


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support