Two PIR sensors not working properly - Sketch not working properly.

This is a copy of another topic
https://forum.arduino.cc/index.php?topic=481404.0

I know it is better to learn Arduino, but I will not be using Arduino for anything else in the future, and I dont have much time now.
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);
    }
}

No need to use the same sketch, anyone may add a completely new one!
All I need is two PIRs, two LEDs, one LCD and one buzzer - without bugs like my code.
believe me , I have already spend hours on this and couldn't fix it.

shijil:
I dont have much time now.

Is this for a class project?

Power_Broker:
Is this for a class project?

NO, just for a personal project.

Well, in that case, here is a new sketch:

#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 val1 = 0;                   // variable for reading the pin status
int prevVal1 = 0;               // variable for storing previous pin status
int risingEdgeTrigger1 = 0;     // rising edge trigger for PIR1
int fallingEdgeTrigger1 = 0;    // falling edge trigger for PIR1
int val2 = 0;                   // variable for reading the pin status
int risingEdgeTrigger2 = 0;     // rising edge trigger for PIR2
int fallingEdgeTrigger2 = 0;    // falling edge trigger for PIR2
int prevVal2 = 0;               // variable for storing previous 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.clear() ;
}



void loop()
{
  prevVal1 = val1;
  prevVal2 = val2;
  val1 = digitalRead(inputPin);  // read input value
  val2 = digitalRead(inputPinB);  // read input value

  risingEdgeTrigger1 = checkRisingEdge(val1, prevVal1);
  risingEdgeTrigger2 = checkRisingEdge(val2, prevVal2);
  fallingEdgeTrigger1 = checkFallingEdge(val1, prevVal1);
  fallingEdgeTrigger2 = checkFallingEdge(val2, prevVal2);

  if (risingEdgeTrigger1 && risingEdgeTrigger2) //Both initiate
  {
    playTone(300, 160);
    Serial.println("Motion detected!");
    lcd.setCursor(0, 0);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 1 Active!!");
    digitalWrite(ledPin, HIGH);  // turn LED ON
    lcd.setCursor(0, 1);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 2 Active!!");
    digitalWrite(ledPinB, HIGH);  // turn LED ON
  }
  else if (risingEdgeTrigger1 && !risingEdgeTrigger2 && !fallingEdgeTrigger2) //PIR1 initiates, PIR2 already initiated
  {
    playTone(300, 160);
    Serial.println("Motion detected!");
    lcd.setCursor(0, 0);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 1 Active!!");
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
  else if (risingEdgeTrigger2 && !risingEdgeTrigger1 && !fallingEdgeTrigger1) //PIR2 initiates, PIR1 already initiated
  {
    playTone(300, 160);
    Serial.println("Motion detected!");
    lcd.setCursor(0, 1);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 2 Active!!");
    digitalWrite(ledPinB, HIGH);  // turn LED ON
  }
  else if (risingEdgeTrigger1 && fallingEdgeTrigger2) //PIR1 initiates, PIR2 stops
  {
    playTone(300, 160);
    Serial.println("Motion detected!");
    lcd.setCursor(0, 0);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 1 Active!!");
    digitalWrite(ledPin, HIGH);  // turn LED ON
    lcd.setCursor(0, 1);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 2 Inactive");
    digitalWrite(ledPinB, LOW);  // turn LED OFF
  }
  else if (risingEdgeTrigger2 && fallingEdgeTrigger1) //PIR2 initiates, PIR1 stops
  {
    playTone(300, 160);
    Serial.println("Motion detected!");
    lcd.setCursor(0, 0);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 1 Inactive");
    digitalWrite(ledPin, LOW);  // turn LED OFF
    lcd.setCursor(0, 1);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 2 Active!!");
    digitalWrite(ledPinB, HIGH);  // turn LED ON
  }
  else if (fallingEdgeTrigger1 && fallingEdgeTrigger2) //PIR1 and PIR2 turn off
  {
    lcd.setCursor(0, 0);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 1 Inactive");
    digitalWrite(ledPin, LOW);  // turn LED OFF
    lcd.setCursor(0, 1);      // Set LCD cursor position (column 0, row 0)
    lcd.print("PIR 2 Inactive");
    digitalWrite(ledPinB, LOW);  // turn LED OFF
  }
  else
  {
    //do nothing
  }
}



// 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);
  }
}



int checkRisingEdge(int val, int prevVal)
{
  int answer = 0;

  if(val && !prevVal)
  {
    answer = 1;
  }
  
  return answer;
}



int checkFallingEdge(int val, int prevVal)
{
  int answer = 0;

  if(!val && prevVal)
  {
    answer = 1;
  }
  
  return answer;
}

Test it and let me know if it works.