Two PIR sensors not working properly, help needed.

Hi, I a just beginning with Arduino, I need some hellp here!!
I have added two PIR sensors to this circuit with an LCD.
Both PIR sensors are working fine and LCD is showing the correct PIR - only if a single PIR is active.
Problem-1. If the PIR1 and PIR2 gets active at the same time, and PIR1 goes off - it will still work as both PIRs are on, both LED will be on and buzzer will sound!!
In short when both sensors are active, Arduino cant sense which one went off later!!

Problem-2. second issue is the LCD is not displaying some text correctly.

If possible. It will be great if the system can display PIR1 status on line 1 and PIR2 status on line 2.

Can anyone help please to fix the two problems and add the extra feature ?

Here is a virtual circuit link to see the sketch and connections.

Here is the code

#include <LiquidCrystal.h> 

int ledPin = 13;                // choose the pin for the LED
int ledPinB = 12;               // choose the pin for the LED 2
int inputPin = 6;               // choose the input pin (for PIR sensor)
int inputPinB = 7;              // choose the input pin (for PIR sensor 2)
int pirState = LOW;             // we start, assuming no motion detected
int pirStateB = LOW;            // we start, assuming no motion detected PIR2
int val1 = 0;                  // variable for reading the pin status
int val2 = 0;                  // variable for reading the pin status
int pinSpeaker = 10;           // Set up a speaker on a PWM pin (digital 9, 10, or 11)

LiquidCrystal lcd(9, 8, 5, 4, 3, 2); 

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(ledPinB, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(inputPinB, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  Serial.begin(9600);
  
  lcd.setCursor(0, 0);           // Set LCD cursor position (column, row)
  lcd.print("Security");         // Print text to LCD
  lcd.setCursor(0, 1);           // Set LCD cursor position (column,row) 
  lcd.print("System");           // Print text to LCD
  delay(1000);                   // wait 1s  Delay to read text
  lcd.clear();                   // clear LCD display    
  lcd.setCursor(0, 0);           // Set LCD cursor position (column, row)
  lcd.print("System Booting"); 
  delay(1000);                   // Delay to read text
  lcd.clear();                   // Clear LCD    
  lcd.setCursor(0, 0); 
  lcd.print("Processing...");
  delay(1000);
  lcd.clear(); 
  lcd.setCursor(0, 0);
  lcd.print("Waiting For");
  lcd.setCursor(0, 1);
  lcd.print("Motion"); 
}

void loop(){
  val1 = digitalRead(inputPin);  // read input value
  val2 = digitalRead(inputPinB);  // read input value
  if (val1 == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(150);

    
    if (pirState == LOW) {
      // just turned on
      Serial.println("Motion detected!");
      lcd.clear() ;
      lcd.setCursor(0, 0);   // Set LCD cursor position (column 0, row 0)
      lcd.print("PIR 1 Active!!");
      // only want to print on the output change, not state
      pirState = HIGH;
    }
  } 
  else if (val2 == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPinB, HIGH);  // turn LED ON
    playTone(300, 160);
    delay(150);

    
    if (pirStateB == LOW) {
      // just turned on
      Serial.println("Motion detected!");
      lcd.clear() ;
      lcd.setCursor(0, 1);      // Set LCD cursor position (column 0, row 0)
      lcd.print("PIR 2 Active!!");
      // only want to print on the output change, not state
      pirStateB = HIGH;
    }
  } 

  
  else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      digitalWrite(ledPinB, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      // just turned off
      Serial.println("Motion ended!");
      lcd.clear() ;
      lcd.setCursor(3, 0); 
      lcd.print("Waiting For"); 
      lcd.setCursor(3, 1);
      lcd.print("Motion.");      // only want to print on the output change, not state
      // only want to print on the output change, not state
      pirState = LOW;
    }
  }
}
// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) {
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}
  lcd.print("Security");         // Print text to LCD
  lcd.setCursor(0, 1);           // Set LCD cursor position (column,row)
  lcd.print("System");           // Print text to LCD
  delay(1000);                   // wait 1s  Delay to read text
  lcd.clear();                   // clear LCD display   
  lcd.setCursor(0, 0);           // Set LCD cursor position (column, row)
  lcd.print("System Booting");
  delay(1000);                   // Delay to read text
  lcd.clear();                   // Clear LCD   
  lcd.setCursor(0, 0);
  lcd.print("Processing...");
  delay(1000);
  lcd.clear();

LOL

Problem-1. If the PIR1 and PIR2 gets active at the same time, and PIR1 goes off - it will still work as both PIRs are on, both LED will be on and buzzer will sound!!
In short when both sensors are active, Arduino cant sense which one went off later!!

At the moment you are doing a if (val1 == HIGH) {, else if (val2 == HIGH) { and else {. That makes the PIR states dependent on each other.
I would change this to simple if for the first two cases. Keep the LED lighting and setting of pirState within those two if () /LED on../ else /LED off../ statements.
In a separate block below then deal with playing the tone if either PIR is active.

If possible. It will be great if the system can display PIR1 status on line 1 and PIR2 status on line 2.

In a separate block below update the LCD and write both status depending on the values of pirState and pirStateB.

Problem-2. second issue is the LCD is not displaying some text correctly.

What text? Under what circumstances?

playTone(0, 0);

Is that necessary?

void playTone(long duration, int freq) {

Is the built-in function not suitable?

arduarn:

  lcd.print("Security");         // Print text to LCD

lcd.setCursor(0, 1);          // Set LCD cursor position (column,row)
  lcd.print("System");          // Print text to LCD
  delay(1000);                  // wait 1s  Delay to read text
  lcd.clear();                  // clear LCD display 
  lcd.setCursor(0, 0);          // Set LCD cursor position (column, row)
  lcd.print("System Booting");
  delay(1000);                  // Delay to read text
  lcd.clear();                  // Clear LCD 
  lcd.setCursor(0, 0);
  lcd.print("Processing...");
  delay(1000);
  lcd.clear();



LOL

At the moment you are doing a *if (val1 == HIGH) {*, *else if (val2 == HIGH) {* and *else {*. That makes the PIR states dependent on each other.
I would change this to simple *if* for the first two cases. Keep the LED lighting and setting of *pirState* within those two _if () /*LED on..*/ else /*LED off..*/_ statements.
In a separate block below then deal with playing the tone if either PIR is active.In a separate block below update the LCD and write both status depending on the values of *pirState* and *pirStateB*.What text? Under what circumstances?Is that necessary?


void playTone(long duration, int freq) {



Is the [built-in function](https://www.arduino.cc/en/Reference/Tone) not suitable?

Thanks for the reply buddy, this is my first day with Arduino codes... So I am just playing with them I don't know anything.

Can you give me a full sketch with two functional PIRs, Two LEDs, 1 buzzer with tone, and an LCD ?
It will be e great help so that I can understand more about it from your code.

playTone(0, 0); - Yes, that was not necessary, I got the base sketcht from PIR Sensor Arduino Alarm | Make:, and it was there.

You can check this link to see what is happening with the LCD text

I really dont know what is suitable or necessary for Arduino, as I said above this is my day 1!!

shijil:
Can you give me a full sketch with two functional PIRs, Two LEDs, 1 buzzer with tone, and an LCD ?
It will be e great help so that I can understand more about it from your code.

So do you want to learn it, or do you want someone else to do it for you?

this is the learning place

this is the get someone else to do it for you place

if you really want to learn, a good start are the tutorials in the IDE you downloaded.

BulldogLowell:
So do you want to learn it, or do you want someone else to do it for you?

this is the learning place

this is the get someone else to do it for you place

if you really want to learn, a good start are the tutorials in the IDE you downloaded.

Thanks for the reply.
I have already spend hours on this same script and I couldn't figure it out!
I don't have much time and most probably, I will not be using Arduino in future.
So I think if anyone else can fix this code for me/ create a new one - it will be great.
I have already posted ads to hire people.