/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 60;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
long unsigned int lowIn2;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
/////////////////////////////
//Sensor A
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
/////////////////////////////
//Sensor B
boolean lockLow2 = true;
boolean takeLowTime2;
/////////////////////////////
//Sensor A
int pirPin = 3; //the digital pin connected to the PIR sensor's output
int ledPin = 12; //Extra LED
/////////////////////////////
//Sensor B
int pirPinB = 2; //the digital pin connected to the PIR sensor's output
int ledPinB = 11; //On Board LED
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
/////////////////////////////
//Sensor A
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);
/////////////////////////////
//Sensor B
pinMode(pirPinB, INPUT);
pinMode(ledPinB, OUTPUT);
digitalWrite(pirPinB, LOW);
//give the sensor some time to calibrate
Serial.print("Calibrating PIR Sensors A & B | 60 Seconds ");
for(int i = 0; i < calibrationTime; i++){
Serial.print("*");
delay(1000);
}
Serial.println(" Finished");
Serial.println("SENSOR ACTIVE");
delay(50);
}
////////////////////////////
//LOOP
void loop(){
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;
Serial.println("---");
Serial.print("motion detected on sensor A at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
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;
Serial.print("motion ended on sensor A at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
/////////////////////////////////
//Sensor B
if(digitalRead(pirPinB) == HIGH){
digitalWrite(ledPinB, 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 on sensor B at ");
Serial.print(millis()/1000);
Serial.println(" sec");
delay(50);
}
takeLowTime2 = true;
}
if(digitalRead(pirPinB) == LOW){
digitalWrite(ledPinB, 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 on sensor B at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
delay(50);
}
}
}
here is some sample output
Calibrating PIR Sensors A & B | 60 Seconds ************************************************************ Finished
SENSOR ACTIVE
motion detected on sensor A at 60 sec
motion detected on sensor B at 60 sec
motion ended on sensor B at 67 sec
motion detected on sensor B at 74 sec
motion ended on sensor B at 76 sec
motion ended on sensor A at 77 sec
motion detected on sensor A at 89 sec
motion detected on sensor B at 89 sec
motion ended on sensor B at 92 sec
motion ended on sensor A at 95 sec
what we would like to achieve is instead of having it say
motion detected on sensor A at 60 sec
motion ended on sensor A at 77 sec
have the output say
motion detected on sensor A at 1:22:30 PM [DATE if Possible]
motion ended on sensor A at 1:22:44 PM [DATE if Possible]
thanks!