I'm trying to make a project where two PIR sensors will be placed 15 cm apart (bounded by a wall). When people enter the room, their movements will be detected by the first PIR sensor and then the second PIR sensor. When I try it, if the reading of the first sensor is high then the second sensor is low then the detection will return to first sensor. The program I made is like this:
#include <Adafruit_MLX90614.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// Temperature Sensor MLX90614
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
const byte degree_sym = B11011111;
// Ultrasonic Sensor HC-SR04
int trigPin = 8;
int echoPin = 9;
float duration;
float distance;
// PIR Sensor
int pir1 = 7;
int pir2 = 6;
int N_PIR1 = 0;
int N_PIR2 = 0;
int K_PIR1 = LOW;
int K_PIR2 = LOW;
int visitors = 0;
int max_visitors = 10;
long x = 0;
// IR Sensor
int varIR = 0;
const int pinIR = 4;
// LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
mlx.begin();
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
if (visitors < 1) {
visitors = 0;
}
condition1();
ultrasonic();
if(distance <= 10) {
if(visitors < max_visitors) {
temp_check();
}
else {
lcd.clear();
condition2();
}
}
IR_check();
}
void condition1() {
lcd.setCursor(0,0);
lcd.print("Please Check");
lcd.setCursor(0,1);
lcd.print("Your Body Temp");
}
void condition2() {
lcd.setCursor(0,0);
lcd.print("The Room");
lcd.setCursor(0,1);
lcd.print("is Full");
delay(2000);
lcd.clear();
}
void ultrasonic() {
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;
Serial.print(distance);
Serial.print(" cm ");
Serial.print(duration);
Serial.print(" ms");
Serial.println();
}
void temp_check() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp Checking");
delay(2000);
lcd.clear();
float body_temp = mlx.readObjectTempC();
Serial.print(body_temp);
Serial.print(degree_sym);
Serial.print("C");
Serial.println();
lcd.setCursor(0,0);
lcd.print("Temp : ");
lcd.print(body_temp);
lcd.write(degree_sym);
lcd.print("C");
delay(1000);
lcd.clear();
if(body_temp<25.50){
lcd.setCursor(0,0);
lcd.print("Check Temperature");
lcd.setCursor(0,1);
lcd.print("Again");
delay(2000);
}
if(body_temp>25.49&&body_temp<37.51){
lcd.setCursor(0,0);
lcd.print("Normal Temp");
lcd.setCursor(0,1);
lcd.print("WELCOME");
for(x = 0; x < 500;x ++) {
visitors_in();
}
delay(3000);
}
if(body_temp>37.50) {
lcd.setCursor(0,0);
lcd.print("High Temp");
lcd.setCursor(0,1);
lcd.print("No Entry");
delay(3000);
}
lcd.clear();
}
void visitors_in() {
N_PIR1 = digitalRead(pir1);
Serial.println("PIR1");
Serial.println(N_PIR1);
if(N_PIR1 == HIGH) {
if(K_PIR1 == LOW) {
Serial.println("Motion Detected");
N_PIR2 = digitalRead(pir2);
Serial.println("PIR2");
Serial.println(N_PIR2);
if(N_PIR2 == HIGH) {
if(K_PIR2 == LOW) {
Serial.println("Visitors In");
visitors++;
Serial.println("Visitors : ");
Serial.println(visitors);
delay(3000);
x = 600;
N_PIR1 = 0;
}
}
}
}
else{
if(K_PIR1 == HIGH) {
Serial.println("Motion Stopped");
K_PIR1 = LOW;
if(K_PIR2 == HIGH) {
Serial.println("Motion Stopped");
K_PIR2 = LOW;
}
}
}
}
void visitors_out() {
delay(2000);
N_PIR2 = digitalRead(pir2);
Serial.println("PIR2");
Serial.println(N_PIR2);
if(N_PIR2 == HIGH) {
delay(100);
if(K_PIR2 == LOW) {
Serial.println("Motion detected");
N_PIR1 = digitalRead(pir1);
delay(3000);
Serial.println("PIR1");
Serial.println(N_PIR1);
if(N_PIR1 == HIGH) {
if(K_PIR1 == LOW) {
Serial.println("Visitors Out");
visitors--;
Serial.println("Visitors : ");
Serial.println(visitors);
delay(3000);
x = 600;
N_PIR2 = 0;
}
}
}
}
else{
if(K_PIR2 == HIGH) {
Serial.println("Motion Stopped");
K_PIR2 = LOW;
if(K_PIR1 == HIGH) {
Serial.println("Motion Stopped");
K_PIR1 = LOW;
}
}
}
}
void IR_check() {
varIR = digitalRead(pinIR);
delay(200);
if(varIR == LOW) {
Serial.println("Door Open");
for(x = 0; x < 400;x ++) {
visitors_out();
}
}
}
How to program the second sensor to continue detecting (without returning to the first sensor) for a certain time?