I am working on a project based on the code from this site: How to use Pyroelectric ("Passive") Infrared Sensors (PIR)
I'm working with 2 PIR sensors and I can't figure out how to get a message to appear when one sensor is triggered shortly after the other. Here are the three conditions I need fulfilled:
Condition 1. Sensor 1 is triggered, message1 is displayed (DONE)
Condition 2. Sensor 2 is triggered, message2 is displayed (DONE)
Condition 3. Sensor 1 is triggered then Sensor 2 is triggered within a couple seconds, message3 is displayed
/*
* PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPinSensor1 = 2; // choose the input pin (for PIR sensor)
int inputPinSensor2 = 9; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int valSensor1 = 0; // variable for reading the pin status (sensor 1)
int valSensor2 = 0; // variable for reading the pin status (sensor 2)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPinSensor1, INPUT); // declare sensor as input
pinMode(inputPinSensor2, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
valSensor1 = digitalRead(inputPinSensor1); // read input value
valSensor2 = digitalRead(inputPinSensor2); // read input value
// SENSOR 1
if (valSensor1 == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected! (message 1)");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended! (message 1)");
// We only want to print on the output change, not state
pirState = LOW;
}
}
// SENSOR 2
if (valSensor2 == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected! (message 2)");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended! (message 2)");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
I appreciate any help you can offer. Thanks for your time.