Cannot Maintain LED's constant HIGH

I am using a new arduino uno (i believe rev.2).
Arduino powered externally with 5.5v supply.
I breaboarded a PIR, LDR, and 3 LED’s. (Code attached)…First timer, code is hacked up pretty bad I think.
The PIR is powered externally with 5.5v supply.

When I digitalWrite ledPin2 & ledPin3, I don’t want them to flicker…currently they blink at the rate of ledPin1…Everything else is what I expect. Not sure how to prevent the blink or flicker.

#CODE

/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/

int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = 0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the 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 we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

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

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

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;


void setup() {
 Serial.begin(9600);
  pinMode(photocellPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  digitalWrite(pirPin, LOW);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
}
void loop() {
  photocellVal = analogRead(photocellPin);
  pirVal = digitalRead(pirPin);

  if (photocellVal < minLight and ledState == 0){
    // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
    
    digitalRead(pirPin);
  if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }
    //Serial.println("fade up");
  }                      
    
    if(photocellVal > minLight and ledState == 1 and pirVal == LOW);
    {       
       digitalWrite(ledPin1, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(ledPin2, LOW);
       digitalWrite(ledPin3, LOW);
           
             
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
       }
    }
  }
}

Please use the code tags!

// Per.

What's wrong with this line? ;) if(photocellVal > minLight and ledState == 1 and pirVal == LOW);

yes, take the ; off that one one so the stuff in the following set of { } can be executed, or not, as intended.

First of all I want to apologize to Zapro, I didn't understand how to insert the code. I think I do now. Thank you for the kind post.

LarryD / CrossRoads: When I remove the ; ledPin2 & ledPin3 do not go off anymore when motion stops...

I am brand new at this, have been at it for 16 hours this weekend, and in fact i love it. Please bare with me, I will learn quickly.

Perhaps if I learn C++ it will help?

Learn it by doing it. For example, in setup:

pinMode(pirPin, INPUT); << pin declared as an input with no internal pullup resistor
pinMode(ledPin1, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
digitalWrite(pirPin, LOW); << this line turns off the internal pullup resistor, which wasn’t turned on to start.

I don’t see anything else grossly wrong. Do you have a schematic of what you have wired up?

Hi Larry, i removed both references to the pirPin you mentioned, same result. ledPin1 does what its supposed to (fades on and off as soon as its dark and continues until light is sensed. But ledPins 2 & 3 still blink at the same rate as the fade on ledPin1.

I don't have a schematic because I am so new at this, but basically it's as I just desricbed:

ledPin1 should come on when it gets dark, and it does. Should stay on until it gets light, regardless of the motion sensor. ledPIns 2 & 3 only come on when motion is sensed, and they do, and should go off when either motion stops or it gets light. Now this does happen, but I want them constant on until either on of those scenarios occur. With the current code, they flicker at the same rate as the ledPin1 fade rate.

I'm really trying.... Best regards, Jack

Hi Larry, i removed both references to the pirPin you mentioned,

I didn't say to do this. I was hinting that the ; was not supposed to be there.

My mistake, sorry. Okay, i put the two lines back and removed the ; from voidsetup() { pinMode(pirPin, INPUT);

before removing the next ; i "verified and came up with an error:

sketch_mar31a.ino: In function 'void setup()': sketch_mar31a:39: error: expected `;' before 'pinMode'

I did remove the setup line digitalWrite(pirPin, LOW);
and nothing changed.

Again, ledPin1 does what its supposed to (fades on and off as soon as its dark and continues until light is sensed.
And ledPins 2 & 3 only come on and stay on when motion is sensed, but still blink at the same rate as the fade on ledPin1.
I’m trying to keep them solid on until no more motion is detected.

here is the code:

/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/

int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = 0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the 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 we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

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

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

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;


void setup() {
 Serial.begin(9600);
  pinMode(photocellPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
}
void loop() {
  photocellVal = analogRead(photocellPin);
  pirVal = digitalRead(pirPin);

  if (photocellVal < minLight and ledState == 0){
    // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
    
    digitalRead(pirPin);
  if(digitalRead(pirPin) == HIGH){
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }
    //Serial.println("fade up");
  }                      
    
    if(photocellVal > minLight and ledState == 1 and pirVal == LOW);
    {       
       digitalWrite(ledPin1, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(ledPin2, LOW);
       digitalWrite(ledPin3, LOW);
           
             
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
       }
    }
  }
}

One thing I did notice, is ledPins 2 & 3 flicker at the same rate as the delay(30); in line 64

Not much time here, but I noticed these:
long unsigned int pause = 5000;
long unsigned int lowIn;
Try and change thes lines to:
unsigned long

You have to remove the ; on this line:
if(photocellVal > minLight and ledState == 1 and pirVal == LOW);

I think you need to change and to &&
if (photocellVal < minLight and ledState == 0){

That got me close...ledPins2&3 do not blink now!!!...that made me happy to see.

The only problem left now is, all the LEDs stay on.

ledPin1 was turning on at dark and off at light, regardless of motion and ledPins2&3 were turning on when motion was detected and only went off when motion stopped

meant to re-post current code:

/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/

int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = 0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the 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 we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

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

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

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;


void setup() {
 Serial.begin(9600);
  pinMode(photocellPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
}
void loop() {
  photocellVal = analogRead(photocellPin);
  pirVal = digitalRead(pirPin);

  if (photocellVal < minLight && ledState == 0){
    // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
    
  if (pirVal == HIGH) { 
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);}
    
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }
    //Serial.println("fade up");
  }                      
    
    if(photocellVal > minLight && ledState == 1 and pirVal == LOW)
    {       
       digitalWrite(ledPin1, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(ledPin2, LOW);
       digitalWrite(ledPin3, LOW);
           
             
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
       }
    }
  }

Humour me and make: int photocellPin = 0;// Photocell connected to analog pin 0 int photocellPin = A0; // Photocell connected to analog pin

unsigned long int lowIn; // remove int

unsigned long int pause = 5000; // remove int

if(takeLowTime){ // change to if(takeLowTime == true){

when you say :

unsigned long int lowIn; // remove int

unsigned long int pause = 5000; // remove int

you want me to comment them out?

I tried the A0 and takeLowTime change and everything works except ledPins2&3 dont turn off when motion stops

As below.
unsigned long lowIn;

unsigned long pause = 5000UL;

Place some Serial.println(someVariable); statements at strategic locations in your code to print the values of different variables to see if things are happening as you think they are.

okay, I’ll do that now.

Here is an update on Code.

ledPin1 works fine, goes HIGH (fades on and off) when dark and goes LOW when light deteced.

ledPIns2&3 light HIGH when motion is detected (no flicker!!), but now do not go off when no motion detected.
This happened when i removed the ; from
if(photocellVal > minLight && ledState < 2 and pirVal == LOW)

/*
This example shows the output of an analogRead() of a Photocell.
By M.Gonzalez
www.codingcolor.com
The example code is in the public domain
*/

int photocellPin = 0;// Photocell connected to analog pin 0
int photocellVal = A0; // define photocell variable
int pirVal = LOW;// motion sensor variable
int ledState = 0;//state of the 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 we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

//the time when the sensor outputs a low impulse
unsigned long lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
unsigned long pause = 5000UL;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 2;    //the digital pin connected to the PIR sensor's output
int ledPin1 = 3;
int ledPin2 = 4;
int ledPin3 = 5;


void setup() {
 Serial.begin(9600);
  pinMode(photocellPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  

  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
}
void loop() {
  photocellVal = analogRead(photocellPin);
  pirVal = digitalRead(pirPin);

  if (photocellVal < minLight && ledState == 0){
    // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin1, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);
    
  if (pirVal == HIGH) { 
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);}
    
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }
    //Serial.println("fade up");
  }                      
    
    if(photocellVal > minLight && ledState < 2 and pirVal == LOW)
    {       
       digitalWrite(ledPin1, LOW);  //the led visualizes the sensors output pin state
       digitalWrite(ledPin2, LOW);
       digitalWrite(ledPin3, LOW);
           
             
       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
       }
    }
  }

I said change:
if(photocellVal > minLight && ledState < 2 and pirVal == LOW)
TO
if(photocellVal > minLight && ledState < 2 && pirVal == LOW)