开启直流电动机的PIR传感器

时间:2019-04-08 06:54:18

标签: arduino arduino-uno

我正在一个侧面项目中,一旦PIR传感器检测到运动,直流电动机(2x)将打开5秒钟,然后关闭。如果运动停止,则直流电动机将关闭。

所以我的问题是我没有达到上面刚刚提到的预期结果。从我的角度来看,我的运动传感器似乎只是在自行打开和关闭,并且直流电动机在运转5秒钟后仍表现出应有的状态,但运动传感器却说尽管有运动,导致直流电动机运行。有运动时,直流电动机应运转。

我在另一个arduino UNO和面包板上尝试了这种确切的硬件和组件,问题似乎与代码有关。

我也尝试过后退一步,看看是否可以通过运动检测来打开LED灯。在串行监视器中,似乎正在检测到运动,但实际上没有。

我还尝试调整PIR传感器上的电位计以及(调整灵敏度和时间)。我还尝试过切换“可重复”触发器和“不可重复”触发器,以查看是否存在问题。

我尝试过更换9V电池,看是否影响了直流电动机的性能。

我还仔细检查了一下,确保每根电线都放在正确的位置。

到目前为止,以下是我提供的给定代码的串行监视器...。这就是它为我提供的。 **请记住,我没有对arduino单元进行任何运动,并且出于某种奇怪的原因,它会检测到运动。

这是串行监视器显示的内容...

2:31:43.219 -> Motors are ON
02:31:43.219 -> Motion detected!
02:31:48.215 -> Motors are ON
02:31:48.249 -> Motors are OFF
02:31:48.249 -> Motion stopped!
02:31:53.232 -> Motors are ON
02:31:53.232 -> Motion detected!
02:31:58.220 -> Motors are ON
02:31:58.253 -> Motors are OFF
02:31:58.253 -> Motion stopped!
02:32:03.238 -> Motors are ON
02:32:03.238 -> Motion detected!
02:32:08.230 -> Motors are ON
02:32:08.265 -> Motors are OFF
02:32:08.265 -> Motion stopped!
const int switchMotion=2;
const int motorPin=9;
const int motorPinB=8;
int motionState=0;
int motionDetected = LOW;

void setup() {
  //Selecting as an input and output the switch and the motor
  pinMode(switchMotion,INPUT);
  pinMode(motorPin,OUTPUT);
  pinMode(motorPinB, OUTPUT);
  Serial.begin(9600); //Set serial out if we want debugging
  delay(5000); //Allow time for the PIR Sensor to calibrate
}

void loop() {

 motionState = digitalRead(switchMotion);  // Reads the motion sensor

 if(motionState == HIGH) // checks if Sensor is HIGH 
  {


    digitalWrite(motorPin,HIGH); //turn on Motor A 
    digitalWrite(motorPinB,HIGH); //turn on Motor B
    delay(5000);  //runs for 5 seconds and stops 
    Serial.println("Motors are ON");


 if (motionDetected == LOW) {        
     Serial.println("Motion detected!"); // print Motion Detected
     motionDetected = HIGH;       // update variable state to HIGH
   }



 else  {
    digitalWrite(motorPin,LOW);    //turn off Motor A
    digitalWrite(motorPinB,LOW);   //turn off Motor B
    Serial.println("Motors are OFF");
   if (motionDetected == HIGH){
       Serial.println("Motion stopped!");
        motionDetected = LOW;       // update variable state to LOW


       }
     }    
  }
}

目标是在个人靠近PIR运动传感器的情况下,使直流电动机在设定的时间段内打开,并在该时间段结束后,电动机关闭,并且有一个设置的延迟时间运动传感器再次检测运动,以使直流电动机再次打开。它应该是一个恒定的周期,在此状态下不运动-直流电动机应关闭。并且当运动时-直流电动机应该打开。例外是有冷却时间。

实际结果是

我希望motionDetected可以正常运行,但是当需要对其进行测试时,它会读取到尽管没有实际运动,但仍检测到/未检测到运动。我的预期结果是运动传感器可以正常工作,因此直流电动机可以相应地打开/关闭。

2 个答案:

答案 0 :(得分:1)

我不确定我是否理解,但是如果我正确的话,这不就是您要寻找的东西吗

const int switchMotion = 2;
const int motorPin = 9;
const int motorPinB = 8;

void setup() {
  //Selecting as an input and output the switch and the motor
  pinMode(switchMotion,INPUT);
  pinMode(motorPin,OUTPUT);
  pinMode(motorPinB, OUTPUT);
  Serial.begin(9600); //Set serial out if we want debugging
  delay(5000); //Allow time for the PIR Sensor to calibrate
}

void loop() {
 if(digitalRead(switchMotion) == HIGH) // checks if Sensor is HIGH 
  {
    Serial.println("Motion detected!"); 
    digitalWrite(motorPin, HIGH); //turn on Motor A 
    digitalWrite(motorPinB, HIGH); //turn on Motor B
    Serial.println("Motors are ON");
    delay(5000);  //runs for 5 seconds and stops
    digitalWrite(motorPin, LOW);    //turn off Motor A
    digitalWrite(motorPinB, LOW);   //turn off Motor B
    Serial.println("Motors are OFF");
  }
// You can add a delay here if you want
}

答案 1 :(得分:0)

您的逻辑混乱了。

If the sensor is low, nothing happens.

first run of loop after sensor turned high:
  turn on the motors,
  wait 5 seconds,
  print "Motors are ON",
  print "Motion detected",
  set motionDetected HIGH.

second run of loop (if sensor is still high):
  turn on the motors,
  wait 5 seconds,
  print "Motors are ON",
  now motionDetected is HIGH so:
    turn off motors
    print "Motors are OFF"
    print "Motion stopped"
    set motionDetected LOW

second run of loop if (sensor is low again):
  nothing happens -> motors stay on

要解决此问题,请确保else属于正确的if! 您想在传感器为低时关闭电动机,而不是在传感器为高且以前为高时关闭电动机。

也应将打印件放置在延迟时间内。实际检测到5秒钟后打印“检测到运动”的意义是什么。