adding ldr to my project

I created a smoke alarm and a motion activated led for my hallway. I noticed that the led also jumps on on motion during daytime wich is a waste of battery power. I therefore connected a ldr to my project but i'm struggling with the code. I connected on end of the ldr with a resistor to the - and the other end goes to arduino's pin 10. I tried this code to test it and i got some readings back so i'm figuring the wiring is ok.

//Program by Jeremy Blum
//www.jeremyblum.com

//Reads and analog sensor and displays the value
int sensePin = 10;

void setup()
{
  //Note: We don't need to specifiy sensePin as an
  //input, since it defaults to that when we read it

  //This is the default value, but we can set it anyways
  analogReference(DEFAULT); //5V Reference on UNO
  
  //Allows us to listen to serial communications from the arduino
  Serial.begin(9600); 
}

void loop()
{
  // print the button state to a serial terminal
  Serial.println(analogRead(sensePin)); 
  delay(500);
  //wait half a second, then print again.
}

This is my original project without ldr:

//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 sensorPin= A0;
int buzzerPin= 12;
int smoke_level;
int pirPin = 7;    //the digital pin connected to the PIR sensor's output
int ledPin = 3;

void setup() {
Serial.begin(115200); //sets the baud rate for data transfer in bits/second
pinMode(sensorPin, INPUT);//the smoke sensor will be an input to the arduino
pinMode(buzzerPin, OUTPUT);//the buzzer serves an output in the circuit
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
}

  void loop() {
 
 activePir();
 smoke();
 digitalWrite(ledPin, LOW);

}

void activePir() {
  if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         delay(60000);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       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;                        
           delay(50);
           }
       }
}

void smoke() {
smoke_level= analogRead(sensorPin); //arduino reads the value from the smoke sensor
if(smoke_level > 200){ //if smoke level is greater than 200, the buzzer will go off
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
}
else{
digitalWrite(buzzerPin, LOW);
}
}

And this is with ldr integrated (well, sort of)

//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 sensorPin= A0;
int buzzerPin= 12;
int smoke_level;
int pirPin = 7;    //the digital pin connected to the PIR sensor's output
int ledPin = 3;
int ldr = 10;
int ldrValue;

void setup() {
Serial.begin(115200); //sets the baud rate for data transfer in bits/second
pinMode(sensorPin, INPUT);//the smoke sensor will be an input to the arduino
pinMode(buzzerPin, OUTPUT);//the buzzer serves an output in the circuit
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
digitalWrite(ledPin, LOW);
}

  void loop() {
 
  ldrValue = analogRead(ldr);
 if (ldrValue <= 800) { 
    activePir();
  } 
  else {
    smoke();
  }
  
  }

void activePir() {
  if(digitalRead(pirPin) == HIGH){
       digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         delay(60000);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

       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;                        
           delay(50);
           }
       }
}

void smoke() {
smoke_level= analogRead(sensorPin); //arduino reads the value from the smoke sensor
if(smoke_level > 200){ //if smoke level is greater than 200, the buzzer will go off
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
}
else{
digitalWrite(buzzerPin, LOW);
}
}

The only thing that needs adjusting is that the pir only can be activated when the ldr's value is > 800. According to the searches i did online that should be enough te only activate the pir/led when it's dark.

Hoping somebody can get me on the right track.

Many thanks !
Any feedback about the rest of the code is also welcomed, it's pretty straight forward; pir senses motion an triggers led that stays on during certain amount of time and if the smoke alarm is triggered an buzzer sound will go off trough connected piezo buzzer...

And this is with ldr integrated (well, sort of)

You need to send me your Arduino, LDR, LED, and PIR, so that I can see for myself what the code does, since you did not describe what the code actually does.

You need to wire the LDR as part of a voltage divider and then use an analog input to measure the voltage

Arduino 5v ----RRRRR-------x---------LDR---------GND

I would try a resistor (RRRR) of about the same resistance as the LDR (you may need to experiment). Then use the ADC to measure the voltage at point x.

...R

If you try Robin2's suggestion while under normal lighting conditions, you should get somewhere around 500 for your ldrValue. As it gets darker, this number will increase.

If the intention is to ONLY disable the LED in the daylight or with lights on, then the condition to do this should not affect the operation of anything else.

If this is the intention, then you could try this for the first "if" statement in your activePir() function:

if (digitalRead(pirPin) == HIGH) {
  ldrValue = analogRead(ldr);
  if (ldrValue > 800) {
    digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state when hallway is dark
  }
  if (lockLow) {
    //makes sure we wait for a transition to LOW before any further output is made:
    lockLow = false;
    delay(60000);
  }
  takeLowTime = true;
}

reworked to code but still the led jumps on either in light or dark.

//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 sensorPin= A0; //smoke sensor
int buzzerPin= 12; //piezo buzzer
int smoke_level;
int pirPin = 7;    //the digital pin connected to the PIR sensor's output
int ledPin = 3; //led
int ldr = A1; //light sensor
int ldrValue;

void setup() {
  Serial.begin(115200); //sets the baud rate for data transfer in bits/second
  pinMode(sensorPin, INPUT);//the smoke sensor will be an input to the arduino
  pinMode(buzzerPin, OUTPUT);//the buzzer serves an output in the circuit
  pinMode(pirPin, INPUT); //sets pir as input
  pinMode(ledPin, OUTPUT); //sets led as output
  pinMode(ldr, INPUT); //sets lidr as input
  digitalWrite(pirPin, LOW); //turns pir off on boot
  digitalWrite(ledPin, LOW); //turns led off on boot
  digitalWrite(buzzerPin, LOW); //turn piezo off on boot-will beep on boot if i don't put this here
}

void loop() {

  activePir(); //activepir loop
  smoke(); //smoke alert loop
}

void activePir() {
  if (digitalRead(pirPin) == HIGH) {
    ldrValue = analogRead(ldr); //get value from ldr
    if (ldrValue > 500) {
      digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state when hallway is dark
    }
    if (lockLow) {
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;
      //delay(60000);
      delay(60);
    }
    takeLowTime = true;
  }

  if(digitalRead(pirPin) == LOW){       
    digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

    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;                        
      delay(50);
    }
  }
}

void smoke() {
  smoke_level= analogRead(sensorPin); //arduino reads the value from the smoke sensor
  if(smoke_level > 200){ //if smoke level is greater than 200, the buzzer will go off
    digitalWrite(buzzerPin, HIGH);
    delay(500);
    digitalWrite(buzzerPin, LOW);
    delay(500);
  }
  else{
    digitalWrite(buzzerPin, LOW);
  }
}

Also made a schematic of the current setup:

Your code would be a lot easier to read and understand if you used the auto-format tool in the IDE.

Your code would be a lot easier to debug if you added some debug prints to it.

That's a bit latin to me i'm afraid.
Copied the code from the gui using 'copy for forum' option in the menu and pasted it. Hope this is better now? Regarding the second point, do you mean //tags after code lines for moe info? If so, i added them now as far as i know what those line do. Hope this is better now.

The IDE has an auto-format tool accessed via the "tools" tab, or simply by typing ctrl-T.

Your code would be a lot easier to debug if you added some debug prints to it.

Diagnostics are much easier if you can get the patient to talk to you.

Serial.print (F("Smoke level is "));
Serial.println (smoke_level);

is one good way.

The 220Ω resistor seems very low ... I suspect the ldrValue will always be above 500.

It would be great if in the light the ldrValue pulls down below 250 and in the dark the ldrValue goes above 750. This will give a good margin (±250) from the comparison "if (ldrValue > 500)"

Light: ldrValue = 0-250
Dark: ldrValue = 750-1023

My best guess is that the resistor should be around 4.7K. I've written a small sketch you could use to adjust the LDR resistor so that there's a good voltage swing from light to dark. The LED will go ON/OFF based on your condition (> 500) and the actual readings can be viewed using the serial monitor.

After getting appropriate results, you could test your code to see what happens:

int ledPin = 3; //led
int ldr = A1; //light sensor
unsigned short ldrValue = 0;

void setup() {
  Serial.begin(115200); //sets the baud rate for data transfer in bits/second
  pinMode(ledPin, OUTPUT); //sets led as output
  pinMode(ldr, INPUT); //sets lidr as input
  digitalWrite(ledPin, LOW); //turns led off on boot
}

void loop() {
  ldrValue = analogRead(ldr); //get value from ldr
  if (ldrValue > 500) {
    digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state when hallway is dark
  }
  else
  {
    digitalWrite(ledPin, LOW);
  }
  Serial.print("ldrValue = ");
  Serial.println(ldrValue);
  delay(500);
}

Added a 4.7 resistor and it worked !! Thanks for your help !! I'll paste the entire code incase anyone needs it in the future.

//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 sensorPin= A0; //smoke sensor
int buzzerPin= 12; //piezo buzzer
int smoke_level;
int pirPin = 7;    //the digital pin connected to the PIR sensor's output
int ledPin = 3; //led
int ldr = A1; //light sensor
unsigned short ldrValue = 0;

void setup() {
  Serial.begin(115200); //sets the baud rate for data transfer in bits/second
  pinMode(sensorPin, INPUT);//the smoke sensor will be an input to the arduino
  pinMode(buzzerPin, OUTPUT);//the buzzer serves an output in the circuit
  pinMode(pirPin, INPUT); //sets pir as input
  pinMode(ledPin, OUTPUT); //sets led as output
  pinMode(ldr, INPUT); //sets lidr as input
  digitalWrite(pirPin, LOW); //turns pir off on boot
  digitalWrite(ledPin, LOW); //turns led off on boot
  digitalWrite(buzzerPin, LOW); //turn piezo off on boot-will beep on boot if i don't put this here
}

void loop() {

  activePir(); //activepir loop
  smoke(); //smoke alert loop
}

void activePir() {
  if (digitalRead(pirPin) == HIGH) {
    ldrValue = analogRead(ldr); //get value from ldr
    if (ldrValue > 800) {
      digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state when hallway is dark
    }
    if (lockLow) {
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;
      delay(60000);
    }
    takeLowTime = true;
  }

  if(digitalRead(pirPin) == LOW){       
    digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state

    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;                        
      delay(50);
    }
  }
}

void smoke() {
  smoke_level= analogRead(sensorPin); //arduino reads the value from the smoke sensor
  if(smoke_level > 200){ //if smoke level is greater than 200, the buzzer will go off
    digitalWrite(buzzerPin, HIGH);
    delay(500);
    digitalWrite(buzzerPin, LOW);
    delay(500);
  }
  else{
    digitalWrite(buzzerPin, LOW);
  }
}