2个PIR运动传感器+ Arduino

时间:2014-04-02 23:11:54

标签: sensor arduino-uno

我的项目是通过使用PIR传感器检测运动来实现自动闪电。

这就是我想要做的事情:

当第一个运动传感器“inputpin”为HIGH表示检测到motion1时,ledPin1设置为HIGH ....然后我检查来自另一个PIR传感器“inputpin2”的信号,如果它是HIGH,则ledPin3应为HIGH ,如果是LOW,则ledpin2应为HIGH。

我编写了这段代码,但它实际上做的是从第一个传感器“inputPin”检测到一个动作后,ledPin3被设置为高,好像第二个传感器始终为高!

任何人都可以帮我解决这个问题。 感谢

```

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 < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}
`

1 个答案:

答案 0 :(得分:1)

例如,将 inputPin2 从1更改为3。不要使用引脚0或1(分配给Tx和Rx的引脚),希望这样做。