2 个 PIR 运动传感器 +Arduino [英] 2 PIR motion sensors +Arduino

查看:27
本文介绍了2 个 PIR 运动传感器 +Arduino的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目是通过使用被动红外传感器检测运动来实现自动照明.

My project is to allow automatic lightening by detecting motion using PIR Sensors.

这就是我想做的:

当第一个运动传感器inputpin"为高电平时,这意味着检测到运动 1,ledPin1 设置为 HIGH...., 如果是LOW ledpin2 应该是HIGH.

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.

我写了这段代码,但它实际做的是在从第一个传感器inputPin"检测到运动后,ledPin3 被设置为高,就好像第二个传感器总是高!

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 !

谁能帮我解决这个问题.谢谢

Can any one help me with this problem. Thanks

` ``

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

推荐答案

例如将 inputPin2 从 1 更改为 3.只是不要使用引脚 0 或 1(分配给 Tx 和 Rx 的引脚),希望能奏效.

Change inputPin2 from 1 to 3 for example. Just don't use the pins 0 or 1 (those assigned for Tx and Rx), hope this works.

这篇关于2 个 PIR 运动传感器 +Arduino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆