繁体   English   中英

开启直流电机的 PIR 传感器

[英]PIR Sensor that turns on DC Motors

我正在做一个辅助项目,一旦 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 能够正常运行,但是当需要对其进行测试时,它会读取检测到/未检测到运动,尽管没有真正的运动。 我的预期结果是运动传感器正常运行,因此直流电机可以相应地打开/关闭。

我不确定我是否理解,但如果我是对的,这不正是您要寻找的:

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
}

你的逻辑有问题。

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 秒打印“检测到运动”有什么意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM