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...
