Hi,
I've been working with arduino and 2 PIR sensors to control the lights in my room. So basically when i enter the room , the lights should go on automatically and after the exit , the lights should go off.. Im using 2 sensors so i can handle multiple entries and exits incase more than 1 person is in the room. So basically im using a variable ctr to keep track of the number of people in the room at one point of time.
Ive placed 2 PIR sensors side by side with a small gap say PIR1 and PIR2. While entering PIR1 triggers first and then PIR2 this increments ctr by1 . While exiting PIR2 triggers first then PIR1 this decrements ctr by 1. Therefore if ctr is 0 then i can switch off the light(using relay).else the light will remain on.
The best part is that the code works fine.. the whole setup works awesome but only for a while. After about 2 hours or so ( and sometimes in random in between ) for some reason when i exit instead of PIR2 triggering before PIR1 , the opposite occurs which then increments ctr by 1 instead of decrementing. THis keeps happening for exits therefore ctr never goes to 0. But if i then restart the arduino everything works fine again for a couple of hours and then the problem starts. The code is fine , the sensors are placed right , its just that it acts weird after a while , although restarting fixes it but thats not reliable.
Here is the code
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
*
* The Parallax PIR Sensor is an easy to use digital infrared motion sensor module.
* (http://www.parallax.com/detail.asp?product_id=555-28027)
*
* The sensor's output pin goes to HIGH if motion is present.
* However, even if motion is present it goes to LOW from time to time,
* which might give the impression no motion is present.
* This program deals with this issue by ignoring LOW-phases shorter than a given time,
* assuming continuous motion is present during these phases.
*
*/
/////////////////////////////
//VARS
//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 lowIn1;
long unsigned int lowIn2;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 100;
boolean lockLow1 = true;
boolean takeLowTime1;
boolean lockLow2 = true;
boolean takeLowTime2;
int pirPin2 = 10;
int pirPin1 = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 7;
int flag1=0;
int flag2=0;
int ctr=0;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin1, LOW);
digitalWrite(pirPin2, 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);
}
////////////////////////////
//LOOP
void loop(){
static int relayVal = 0;
if(digitalRead(pirPin1) == HIGH){
//the led visualizes the sensors output pin state
if(lockLow1){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow1 = false;
Serial.println("---");
Serial.print("motion detected at1 ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
flag1=1;
if(flag2==1)
{
ctr=ctr+1;
Serial.println("ctr at 1= ");
Serial.println(ctr);
flag1=0;
flag2=0;
if(ctr<=0)
digitalWrite(ledPin, LOW);
if(ctr>0)
digitalWrite(ledPin, HIGH);
}
}
takeLowTime1 = true;
}
//*************8
if(digitalRead(pirPin2) == HIGH){
//the led visualizes the sensors output pin state
if(lockLow2){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow2 = false;
Serial.println("---");
Serial.print("motion detected at2 ");
Serial.print(millis()/1000);
Serial.println(" sec");
//Serial.println(flag1);
//Serial.println(flag2);
delay(50);
flag2=1;
if(flag1==1)
{
ctr=ctr-1;
Serial.println("ctr at 2 = ");
Serial.println(ctr);
flag1=0;
flag2=0;
if(ctr<=0)
digitalWrite(ledPin, LOW);
if(ctr>0)
digitalWrite(ledPin, HIGH);
}
}
takeLowTime2 = true;
}
//************
if(digitalRead(pirPin1) == LOW){
//digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime1){
lowIn1 = millis(); //save the time of the transition from high to LOW
takeLowTime1 = 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(!lockLow1 && millis() - lowIn1 > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow1 = true;
Serial.print("motion ended at1 "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
//#####
if(digitalRead(pirPin2) == LOW){
//digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
if(takeLowTime2){
lowIn2 = millis(); //save the time of the transition from high to LOW
takeLowTime2 = 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(!lockLow2 && millis() - lowIn2 > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow2 = true;
Serial.print("motion ended at2 "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
//#####
}
would really appreciate some help..Thanks a lot ..