user34274: 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 Link of virtual circuit 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);
    }
}


user4143: Your problem is with the entire structure of your program. Pretty much everything is dependent on something else not having happened. Instead you need to make things more independent.
You should separate out your PIR sensor code into discrete chunks. One sensor isn't dependent on the other one having triggered or not, so there is no call to "link" them with an "else if". Also naming variables better will help understand the flow of code easier.
uint8_t savedStatePIR1 = LOW;
uint8_t savedStatePIR2 = LOW;

bool motionDetectedPIR1 = false;
bool motionDetectedPIR2 = false;

void loop() {
    uint8_t currentStatePIR1 = digitalRead(inputPinPIR1);
    uint8_t currentStatePIR2 = digitalRead(inputPinPIR2);
    bool haveChanged = false;

    if (currentStatePIR1 != savedStatePIR1) {
        savedStatePIR1 = currentStatePIR1);
        haveChaged = true;
        if (currentStatePIR1 == HIGH) { // Activated
            motionDetectedPIR1 = true;
        } else { // Deactivated
            motionDetectedPIR1 = false;
        }
    }

    // Exact duplicate with PIR1 changed to PIR2
    if (currentStatePIR2 != savedStatePIR2) {
        savedStatePIR2 = currentStatePIR2);
        haveChanged = true;
        if (currentStatePIR2 == HIGH) { // Activated
            motionDetectedPIR2 = true;
        } else { // Deactivated
            motionDetectedPIR2 = false;
        }
    }

    // Now, if either state changed this iteration through loop()
    // update the LEDs and the display, and play a tone if you
    // need to. Note that there is already a tone() function so
    // you really don't need to write your own.
    if (haveChanged) { // Something changed, so update things.
            digitalWrite(ledPin, motionDetectedPIR1);
            digitalWrite(ledPinB, motionDetectedPIR2);
            lcd.clear();

            if (motionDetectedPIR1 || motionDetectedPIR2) {
                tone(pinSpeaker, 300, 160); 
                if (motionDetectedPIR1) {
                    lcd.setCursor(0, 0);
                    lcd.print("PIR 1 Active!!");
                }
                if (motionDetectedPIR2) {
                    lcd.setCursor(0, 1);
                    lcd.print("PIR 2 Active!!");
                }
            } else {
                lcd.setCursor(3, 0); 
                lcd.print("Waiting For"); 
                lcd.setCursor(3, 1);
                lcd.print("Motion."); 
            }
        }
    }
}

Note: this code isn't tested at all (it'll be full of errors). The basic principle, though, is:

First look for motion in PIR 1 and record if there is or not.
Repeat for the second PIR.
If there has been any change in state then (and only then) update the outputs.


34274: <p>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!!</p>

<p>Problem-2. second issue is the LCD is not displaying some text correctly.</p>

<p>If possible, it will be great if the system can display PIR1 status on line 1 and PIR2 status on line 2.</p>

<p>Can anyone help please to  fix the two problems and add the extra feature ?</p>

<p>Here is a <a href="https://circuits.io/users/1283839" rel="nofollow noreferrer">Link</a> of virtual circuit to see the sketch and connections.</p>

<p>Here is the code</p>

<pre><code>#include &lt;LiquidCrystal.h&gt; 

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 &lt; duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}
</code></pre>
 4143: <p>Your problem is with the entire structure of your program. Pretty much everything is dependent on something else not having happened. Instead you need to make things more independent.</p>

<p>You should separate out your PIR sensor code into discrete chunks. One sensor isn't dependent on the other one having triggered or not, so there is no call to "link" them with an "else if". Also naming variables better will help understand the flow of code easier.</p>

<pre><code>uint8_t savedStatePIR1 = LOW;
uint8_t savedStatePIR2 = LOW;

bool motionDetectedPIR1 = false;
bool motionDetectedPIR2 = false;

void loop() {
    uint8_t currentStatePIR1 = digitalRead(inputPinPIR1);
    uint8_t currentStatePIR2 = digitalRead(inputPinPIR2);
    bool haveChanged = false;

    if (currentStatePIR1 != savedStatePIR1) {
        savedStatePIR1 = currentStatePIR1);
        haveChaged = true;
        if (currentStatePIR1 == HIGH) { // Activated
            motionDetectedPIR1 = true;
        } else { // Deactivated
            motionDetectedPIR1 = false;
        }
    }

    // Exact duplicate with PIR1 changed to PIR2
    if (currentStatePIR2 != savedStatePIR2) {
        savedStatePIR2 = currentStatePIR2);
        haveChanged = true;
        if (currentStatePIR2 == HIGH) { // Activated
            motionDetectedPIR2 = true;
        } else { // Deactivated
            motionDetectedPIR2 = false;
        }
    }

    // Now, if either state changed this iteration through loop()
    // update the LEDs and the display, and play a tone if you
    // need to. Note that there is already a tone() function so
    // you really don't need to write your own.
    if (haveChanged) { // Something changed, so update things.
            digitalWrite(ledPin, motionDetectedPIR1);
            digitalWrite(ledPinB, motionDetectedPIR2);
            lcd.clear();

            if (motionDetectedPIR1 || motionDetectedPIR2) {
                tone(pinSpeaker, 300, 160); 
                if (motionDetectedPIR1) {
                    lcd.setCursor(0, 0);
                    lcd.print("PIR 1 Active!!");
                }
                if (motionDetectedPIR2) {
                    lcd.setCursor(0, 1);
                    lcd.print("PIR 2 Active!!");
                }
            } else {
                lcd.setCursor(3, 0); 
                lcd.print("Waiting For"); 
                lcd.setCursor(3, 1);
                lcd.print("Motion."); 
            }
        }
    }
}
</code></pre>

<p>Note: this code isn't tested at all (it'll be full of errors). The basic principle, though, is:</p>

<ol>
<li>First look for motion in PIR 1 and record if there is or not.</li>
<li>Repeat for the second PIR.</li>
<li>If there has been any change in state then (and only then) update the outputs.</li>
</ol>
