Hi there, I am simply wanting to have four PIR sensors that each individually control one switch on the relay module. The code seems to be working fine with one PIR sensor, however I have tried altering the code to accommodate for two PIR sensors (See code at bottom of post) but it would appear that when the arduino receives a signal from both sensors the relay switch flicks on and off for a period of about 5seconds before settling back down again.
I am using the following code for the PIR sensors: Arduino Playground - PIRsense
The only problem I have with this code is that I would prefer it if the relay turns on when there is movement detected (HIGH rather than LOW) as this would conserve more energy if this is going to be running all day.
My other concern is: will the arduino will be able to operate all 4 relay switches at once? - baring in mind that I am actually just using the relay as an electromagnetic switch and therefore am not driving any loads.
Here is an image of the relay module I am using (wiring in the code below differs from the image shown):
Could someone give me some advice as to how it might be possible to have each PIR sensor trigger a switch on the relay module (effectively repeat the code above but make sure that each sensor is processing the code seperately.) Here is my attempt at trying to incorporate a second sensor:
/////////////////////////////
//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; //PIR 2 added here
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow1 = true;
boolean takeLowTime1;
boolean lockLow2 = true; //PIR 2 added here
boolean takeLowTime2;
int pirPin1 = 11; //the digital pin connected to the PIR sensor's output
int pirPin2 = 12; //PIR 2 added here
int ledPin = 13;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT); //PIR 2 added here
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin1, LOW);
digitalWrite(pirPin2, LOW); //PIR 2 added here
//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(){
//If input 1 is HIGH
if(digitalRead(pirPin1) == HIGH){
digitalWrite(ledPin, 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);
}
takeLowTime1 = true;
}
//If input 2 is HIGH
if(digitalRead(pirPin2) == HIGH){
digitalWrite(ledPin, 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:
lockLow1 = false;
Serial.println("---");
Serial.print("motion detected at2 ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime2 = true;
}
//If input 1 is LOW
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 input 2 is LOW
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);
}
}
//#####
}
}
