I am writing code for Arduino with the use of two passive infrared motion sensors to detect if someone has entered a room or if someone has left the room. One sensor will be on the inner portion of the door and another sensor will be outside of the door.
I want to create an interrupt mechanism such that if the sensor on the outside is triggered first and the sensor on the inside is triggered next, a message is printed on the serial monitor that someone has entered the room and vice versa.
I am trying to implement the first case (someone entering the room) by having an interrupt inside of another interrupt. But it doesn't work. Because if the sensor inside of the room is triggered first and the sensor outside of the room is triggered next, the program is still printed on the serial monitor that someone has entered the room. But I don't want this to be the case. I only want it to print out that some has entered the room if the first sensor triggers first and then the second sensor triggers.
The code that I write is provided below. Does anyone know how I can resolve this issue?
const byte INSIDE = 2;
const byte OUTSIDE =3;
volatile byte state = LOW;
byte x;
byte y;
void InsideTrig() {
attachInterrupt(digitalPinToInterrupt(OUTSIDE), OutsideTrig, HIGH);
}
void OutsideTrig() {
state= !state;
}
void setup() {
digitalWrite(INSIDE, HIGH);
digitalWrite(OUTSIDE, HIGH);
Serial.begin(9600);
}
void loop() {
attachInterrupt(digitalPinToInterrupt(INSIDE), InsideTrig, HIGH);
if (state == 1) {
Serial.println("Someone has entered the room.");
}
delay(1000);
}