Problems related to Infra red sensor

I have two infra red sensors and a buzzer. Sensor PIR located at the top of the door for detecting if someone wants to go out or enter the house. Sensor CS will be on when placing the key next to it.

When a person tries to go out without key, the buzzer will alarm because sensor PIR detects motion while sensor CS keeps in "HIGH" status. But if he tries to go out with key, the buzzer will not alarm because sensor CS is in "LOW" status.

When he comes back (Sensor PIR = "HIGH"), the buzzer will alarm if he forgets to place the key back to the sensor CS (LOW). But the alarm will stop when he places the key next to sensor CS.

The code I wrote can fulfill the "Go out" scenario only. I don't know how to write the "Come back" scenario because the logic just reverse.

Sensor PIR: SR501
Sensor CS: Flying Fish (https://www.amazon.de/-/en/Infrared-obstacle-distance-sensor-Flying/dp/B07WNPXWGZ)

Here is my code.

//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 lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5;

unsigned long count = 0;
unsigned long delaytime = 1000;

boolean lockLow = true;
boolean takeLowTime;

int CS = 9;    // Flying fish
int PIR = 3;    // PIR sensor
int Bell = 8;   // buzzer


void setup(){
  Serial.begin(9600);
  pinMode(CS, INPUT);
  pinMode(PIR, INPUT);
  pinMode(Bell, OUTPUT);
  digitalWrite(PIR, LOW);
  digitalWrite(Bell, LOW);

  Serial.print("calibrating sensor");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

void loop() {
    if (digitalRead(CS) == LOW && digitalRead(PIR) == HIGH){
        digitalWrite(Bell, HIGH);
        tone(Bell, 330, 10);
        delay(200);
        digitalWrite(Bell, LOW);  
        delay(200);             
      } else {
        digitalWrite(Bell, LOW);  
        }
      
      if(lockLow){
        lockLow = false;            
        Serial.println("---");
        Serial.print("motion detected at ");
        Serial.print(millis()/1000);
        Serial.println("sec"); 
        delay(100);
        }         
        takeLowTime = true;
}

It works for the "Go out" scenario. But when I wrote for the "Come back" one, it doesn't work.

  if (digitalRead(CS) == LOW && digitalRead(PIR) == HIGH) {
    digitalWrite(Bell, HIGH);
    tone(Bell, 330, 10);
  } else {
    noTone(Bell);
  }
}

Hi, @vanillavanilla
Welcome to the forum.

Is this your code?
What does the code posted do?

Can you post a link to data/specs of PIR detector?
Have you written your code in stages to prove each of your individual inputs before adding any logic?

Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hello Tom, thank you for your reminder. I updated my post. Hope it's clear.

Please refrain from updating old posts, update by using a new post so that the flow of the thread is not confusing?

The PIR are not PIRs by the way.
They are an infrared reflective module.

They will work better and faster than PIRs as they detect solid obstacles, not warm/hot objects.

Tom.. :smiley: :+1: :coffee: :australia: