首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >开启直流电机的PIR传感器

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

Stack Overflow用户
提问于 2019-04-08 14:54:18
回答 2查看 1K关注 0票数 0

我正在做一个辅助项目,一旦PIR传感器检测到运动,直流电机(2x)将打开5秒,然后关闭。如果运动停止了,直流马达就会关闭。

所以我的问题是,我没有达到我刚才提到的预期结果。从我的角度来看,我的运动传感器似乎就是自己断断续续的,直流电机在持续5秒后表现正常,但运动传感器显示尽管有运动,但仍有运动,这导致直流电机运行。直流电机应在有运动的情况下运行。

我已经在另一个arduino UNO和breadboard上尝试过这个硬件和组件,这个问题似乎与代码有关。

我还试着后退一步,看看是否可以让运动检测功能打开LED灯。在串行监视器中,似乎发现有运动被检测到,但实际上没有。

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

我试着换用9V电池,看看这是否影响了直流电机的性能。

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

现在,下面是串行监视器..。对于我提供的给定代码。这就是它提供给我的。**请记住,我没有将任何运动放入arduino单元,并且出于某种奇怪的原因,它检测到有运动。

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

代码语言:javascript
复制
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!
代码语言:javascript
复制
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能正常工作,但当它测试它的时候,它读到有运动检测到/没有检测到运动,尽管没有真正的运动。我期望的结果是运动传感器能够正常工作,这样直流电机就可以相应地打开/关闭。

EN

回答 2

Stack Overflow用户

发布于 2020-01-02 08:04:09

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2019-04-08 17:23:32

你的逻辑是混乱的。

代码语言:javascript
复制
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秒打印“运动检测到”有什么意义?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55567742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档