title: 2 PIR motion sensors +Arduino
body: <p>My project is to allow automatic lightening by detecting motion using PIR Sensors.</p>
  
  <p>THis is what I want to do :</p>
  
  <p>when the first motion sensor "inputpin" is HIGH which means a motion1 is detected , ledPin1 is set to HIGH.... then I check the signal from the other PIR sensor "inputpin2"  if it is HIGH ledPin3 should be HIGH , If it is LOW ledpin2 should be HIGH.</p>
  
  <p>I wrote this code , but what it actually do is after a motion is detected from the first sensor"inputPin" , ledPin3 is set to high as if the second sensor is always HIGH !</p>
  
  <p>Can any one help me with this problem.
  Thanks</p>
  
  <p>` ``</p>
  
  <pre><code>int ledPin1 = 13;                // choose the pin for the LED
  int ledPin2 = 12;
  int ledPin3 = 11;
  int inputPin = 2;               // choose the input pin (for PIR sensor)
  int inputPin2 = 1;
  int pirState1 = LOW;             // we start, assuming no motion detected
  int pirState2 = LOW;  
  int val = 0;                    // variable for reading the pin status
  int val2 = 0;
  int pinSpeaker = 10;           //Set up a speaker on a PWM pin (digital 9, 10, or 11)
  
  void setup() {
    pinMode(ledPin1, OUTPUT);      // declare LED as output
    pinMode(ledPin2, OUTPUT);      // declare LED as output
    pinMode(ledPin3, OUTPUT); 
    pinMode(inputPin, INPUT);     // declare sensor 1 as input
     pinMode(inputPin2, INPUT);    // declare sensor 2 as input
   // pinMode(pinSpeaker, OUTPUT);
    Serial.begin(9600);
  }
  
  void loop(){
    val = digitalRead(inputPin);  // read input value
    if (val == HIGH) {            // check if the input is HIGH
      digitalWrite(ledPin1, HIGH);  // turn LED ON
      delay (1500);
  
       if (pirState1 == LOW) {
        // we have just turned on
        Serial.println("Motion1 detected!");
        // We only want to print on the output change, not state
        pirState1 = HIGH;
      }
  
       delay (1500);
  
      // check sensor 2 after delay
      val2 = digitalRead(inputPin2);
       if  (val2 == HIGH){
      digitalWrite(ledPin2, LOW);
      delay(1500);
      digitalWrite(ledPin3,HIGH);
      //playTone(300, 160);
      delay(1500);
       if (pirState2 == LOW) {
        // we have just turned on
        Serial.println("Motion1 from sensor 2 detected!");
        // We only want to print on the output change, not state
        pirState2 = HIGH;
      }
      }
      if(val2 == LOW){
      digitalWrite(ledPin2, HIGH);
      //playTone(300, 160);
      delay(1500);
       digitalWrite(ledPin3,LOW);
       delay(1500);
      }
  
  
  
  
  } else {
        digitalWrite(ledPin1, LOW); // turn LED OFF
        delay (1500);
        digitalWrite(ledPin2, LOW); // may be already
        //playTone(0, 0);
        delay(1500); 
        digitalWrite(ledPin3, LOW); // turn LED OFF
        delay (1500);      
        if (pirState1 == HIGH){
        // we have just turned of
        Serial.println("Motion ended!");
        // We only want to print on the output change, not state
        pirState1 = LOW;
      }
  if (pirState2 == HIGH){
        // we have just turned of
        Serial.println("Motion ended!");
        // We only want to print on the output change, not state
        pirState2 = 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>