user16157: #include <LiquidCrystal.h>
#include <Wire.h>

LiquidCrystal lcd(2, 3, 4, 5, 11, 12);// initialize the library with the numbers of the interface pins

int pirPin = 7; //Variable that will receive the digital signal from the sensor 
int LEDgreen = 6; //LED addressed to the digital pin 6
int speaker = 8; //speaker addressed to the digital pin 7
int buttonPin = 9; // the pin that the pushbutton is attached to

void setup(){
  pinMode(buttonPin, INPUT);// Initialize the button pin as an input:
  pinMode(pirPin, INPUT);  // declares variable pirPin as an input
  pinMode(LEDgreen, OUTPUT); // declares variable LEDgreen as an output
  pinMode(speaker, OUTPUT); // declares variable speaker as an output
  lcd.begin(16, 2);
  // turn on the cursor:
  lcd.cursor();
  Serial.begin(9600);
}

void loop(){ 
  int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
  int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
  if(buttonState == LOW){
    if(digitalRead(pirPin) == LOW){
      /*The LED Blinks once with the command digitalWrite
      the LED keeps turned on after blink during the motion cycle*/   
      digitalWrite(LEDgreen, HIGH); 
      delay(1000);
      digitalWrite(LEDgreen, LOW);
      delay(500);
      digitalWrite(LEDgreen, HIGH); 
      /*speaker plays frequency, the number 7 is the digital pin, 
      the second number is the frequency played and the third number is the sound duration
      we repeated this command with different frequency and varying the time in order to create a 
      pleasant sound */
      tone(4,261,300);
      delay(200);
      tone(4,329,300);
      delay(200);
      tone(4,392,600);
      delay(200);
      tone(4,329,300);
      delay(200);
      tone(4,392,300);
      delay(200);
      tone(4,493,600);
      delay(200);
      tone(4,392,300);
      delay(200);
      tone(4,493,300);
      delay(200);
      tone(4,294,600);
      delay(200);
      lcd.setCursor(0,0);
      lcd.print("  Motion  ");  // Print a message to the LCD.
      lcd.setCursor(0,1);
      lcd.print(" Detected ");
    } else if(digitalRead(pirPin) ==  HIGH) {
       digitalWrite(LEDgreen, LOW); // Turn off the LED in the case where no motion is detected
       lcd.setCursor(0,0);
       lcd.print("No Motion  ");
       lcd.setCursor(0,1);
       lcd.print(" Detected ");
     }
   } else if(buttonState == HIGH) {
     if(digitalRead(pirPin) == LOW) {
       digitalWrite(LEDgreen, HIGH); 
       delay(1000);
       digitalWrite(LEDgreen, LOW);
       delay(500);
       digitalWrite(LEDgreen, HIGH);
       tone(4,261,300);
       delay(200);
       tone(4,329,300);
       delay(200);
       tone(4,392,600);
       delay(200);
       tone(4,329,300);
       delay(200);
       tone(4,392,300);
       delay(200);
       tone(4,493,600);
       delay(200);
       tone(4,392,300);
       lcd.setCursor(0,0);
       lcd.print("  Motion  ");
       lcd.setCursor(0,1);
       lcd.print(" Detected & door ");
     } else if(digitalRead(pirPin) ==  HIGH) {
       digitalWrite(LEDgreen, LOW);
       // Turn off the LED in the case where no motion is detected
       tone(4,329,300);
       delay(200);
       tone(4,392,300);
       delay(200);
       tone(4,493,600);
       delay(200);
       tone(4,392,300);
       lcd.setCursor(0,0);
       lcd.print("No Motion  ");
       lcd.setCursor(0,1);
       lcd.print(" Detected but door ");
     }
   }
   delay(2000); // wait for 1 seconds before compare again
 }


user7174: This is a summary of your basic loop logic:
void loop() {
  int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
  int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
  if (buttonState == LOW) {
    if (digitalRead(pirPin) == LOW) {
        // buttonState is LOW
        // pirPin is recently read as LOW
        // ... Motion Detected ...
    }
    else if (digitalRead(pirPin) ==  HIGH) {
        // buttonState is LOW
        // pirPin is recently read as HIGH
        // ... No Motion Detected ...
    }
  }
  else if (buttonState == HIGH) {
    if (digitalRead(pirPin) == LOW) {
        // buttonState is HIGH
        // pirPin is recently read as LOW
        // ... Motion Detected & Door ...
    }
    else if (digitalRead(pirPin) ==  HIGH) {
        // buttonState is HIGH
        // pirPin is recently read as HIGH
        // ... No Motion Detected but Door ...
    }
  }
  delay(2000); // wait for 1 seconds before compare again
}

However, this is bad code:
if (digitalRead(pirPin) == LOW) {
    // ... Motion Detected ...
}
else if (digitalRead(pirPin) ==  HIGH) {
    // ... No Motion Detected ...
}

because each time the digitalRead(..) function is called, it can give a different value. So even though your intention is clear, this code won't work the way you expect. Consider what happens if the first digitalRead(pirPin) is high, so digitalRead(pirPin)==LOW is false, but then the second digitalRead(pirPin) is low, so digitalRead(pirPin)==HIGH is also false. Then neither block of code runs. Instead, use:
if (digitalRead(pirPin) == LOW) {
    // ... Motion Detected ...
}
else {
    // ... No Motion Detected ...
}

Or since you already sampled it at the start of the loop with int pirVal = digitalRead(pirPin);, simplify the logic as:
void loop() {
  int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
  int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal

  if (buttonState == LOW) {
    if (pirVal == LOW) {
        // buttonState is LOW
        // pirPin is LOW
        // ... Motion Detected ...
    }
    else {
        // buttonState is LOW
        // pirPin is HIGH
        // ... No Motion Detected ...
    }
  }
  else {
    // buttonState is HIGH
    if (pirVal == LOW) {
        // buttonState is HIGH
        // pirPin is LOW
        // ... Motion Detected & Door ...
    }
    else {
        // buttonState is HIGH
        // pirPin is HIGH
        // ... No Motion Detected but Door ...
    }
  }
  delay(2000); // wait for 1 seconds before compare again
}

By the way, if you're using the Arduino IDE, you can easily clean up the indentation to make these control structures easier to see, by using Tools | Auto Format.

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

LiquidCrystal lcd(2, 3, 4, 5, 11, 12);// initialize the library with the numbers of the interface pins

int pirPin = 7; //Variable that will receive the digital signal from the sensor 
int LEDgreen = 6; //LED addressed to the digital pin 6
int speaker = 8; //speaker addressed to the digital pin 7
int buttonPin = 9; // the pin that the pushbutton is attached to

void setup(){
  pinMode(buttonPin, INPUT);// Initialize the button pin as an input:
  pinMode(pirPin, INPUT);  // declares variable pirPin as an input
  pinMode(LEDgreen, OUTPUT); // declares variable LEDgreen as an output
  pinMode(speaker, OUTPUT); // declares variable speaker as an output
  lcd.begin(16, 2);
  // turn on the cursor:
  lcd.cursor();
  Serial.begin(9600);
}

void loop(){ 
  int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
  int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
  if(buttonState == LOW){
    if(digitalRead(pirPin) == LOW){
      /*The LED Blinks once with the command digitalWrite
      the LED keeps turned on after blink during the motion cycle*/   
      digitalWrite(LEDgreen, HIGH); 
      delay(1000);
      digitalWrite(LEDgreen, LOW);
      delay(500);
      digitalWrite(LEDgreen, HIGH); 
      /*speaker plays frequency, the number 7 is the digital pin, 
      the second number is the frequency played and the third number is the sound duration
      we repeated this command with different frequency and varying the time in order to create a 
      pleasant sound */
      tone(4,261,300);
      delay(200);
      tone(4,329,300);
      delay(200);
      tone(4,392,600);
      delay(200);
      tone(4,329,300);
      delay(200);
      tone(4,392,300);
      delay(200);
      tone(4,493,600);
      delay(200);
      tone(4,392,300);
      delay(200);
      tone(4,493,300);
      delay(200);
      tone(4,294,600);
      delay(200);
      lcd.setCursor(0,0);
      lcd.print("  Motion  ");  // Print a message to the LCD.
      lcd.setCursor(0,1);
      lcd.print(" Detected ");
    } else if(digitalRead(pirPin) ==  HIGH) {
       digitalWrite(LEDgreen, LOW); // Turn off the LED in the case where no motion is detected
       lcd.setCursor(0,0);
       lcd.print("No Motion  ");
       lcd.setCursor(0,1);
       lcd.print(" Detected ");
     }
   } else if(buttonState == HIGH) {
     if(digitalRead(pirPin) == LOW) {
       digitalWrite(LEDgreen, HIGH); 
       delay(1000);
       digitalWrite(LEDgreen, LOW);
       delay(500);
       digitalWrite(LEDgreen, HIGH);
       tone(4,261,300);
       delay(200);
       tone(4,329,300);
       delay(200);
       tone(4,392,600);
       delay(200);
       tone(4,329,300);
       delay(200);
       tone(4,392,300);
       delay(200);
       tone(4,493,600);
       delay(200);
       tone(4,392,300);
       lcd.setCursor(0,0);
       lcd.print("  Motion  ");
       lcd.setCursor(0,1);
       lcd.print(" Detected &amp; door ");
     } else if(digitalRead(pirPin) ==  HIGH) {
       digitalWrite(LEDgreen, LOW);
       // Turn off the LED in the case where no motion is detected
       tone(4,329,300);
       delay(200);
       tone(4,392,300);
       delay(200);
       tone(4,493,600);
       delay(200);
       tone(4,392,300);
       lcd.setCursor(0,0);
       lcd.print("No Motion  ");
       lcd.setCursor(0,1);
       lcd.print(" Detected but door ");
     }
   }
   delay(2000); // wait for 1 seconds before compare again
 }
</code></pre>
 7174: <p>This is a summary of your basic loop logic:</p>

<pre><code>void loop() {
  int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
  int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal
  if (buttonState == LOW) {
    if (digitalRead(pirPin) == LOW) {
        // buttonState is LOW
        // pirPin is recently read as LOW
        // ... Motion Detected ...
    }
    else if (digitalRead(pirPin) ==  HIGH) {
        // buttonState is LOW
        // pirPin is recently read as HIGH
        // ... No Motion Detected ...
    }
  }
  else if (buttonState == HIGH) {
    if (digitalRead(pirPin) == LOW) {
        // buttonState is HIGH
        // pirPin is recently read as LOW
        // ... Motion Detected &amp; Door ...
    }
    else if (digitalRead(pirPin) ==  HIGH) {
        // buttonState is HIGH
        // pirPin is recently read as HIGH
        // ... No Motion Detected but Door ...
    }
  }
  delay(2000); // wait for 1 seconds before compare again
}
</code></pre>

<p>However, this is bad code:</p>

<pre><code>if (digitalRead(pirPin) == LOW) {
    // ... Motion Detected ...
}
else if (digitalRead(pirPin) ==  HIGH) {
    // ... No Motion Detected ...
}
</code></pre>

<p>because each time the digitalRead(..) function is called, it can give a different value. So even though your intention is clear, this code won't work the way you expect. Consider what happens if the first digitalRead(pirPin) is high, so digitalRead(pirPin)==LOW is false, but then the second digitalRead(pirPin) is low, so digitalRead(pirPin)==HIGH is also false. Then neither block of code runs. Instead, use:</p>

<pre><code>if (digitalRead(pirPin) == LOW) {
    // ... Motion Detected ...
}
else {
    // ... No Motion Detected ...
}
</code></pre>

<p>Or since you already sampled it at the start of the loop with <code>int pirVal = digitalRead(pirPin);</code>, simplify the logic as:</p>

<pre><code>void loop() {
  int buttonState = digitalRead(buttonPin);// Read the pushbutton input pin:
  int pirVal = digitalRead(pirPin); //read the digital value from the pirPin and store the value in the local variable pirVal

  if (buttonState == LOW) {
    if (pirVal == LOW) {
        // buttonState is LOW
        // pirPin is LOW
        // ... Motion Detected ...
    }
    else {
        // buttonState is LOW
        // pirPin is HIGH
        // ... No Motion Detected ...
    }
  }
  else {
    // buttonState is HIGH
    if (pirVal == LOW) {
        // buttonState is HIGH
        // pirPin is LOW
        // ... Motion Detected &amp; Door ...
    }
    else {
        // buttonState is HIGH
        // pirPin is HIGH
        // ... No Motion Detected but Door ...
    }
  }
  delay(2000); // wait for 1 seconds before compare again
}
</code></pre>

<p>By the way, if you're using the Arduino IDE, you can easily clean up the indentation to make these control structures easier to see, by using Tools | Auto Format.</p>
