hc-501 PIR 红外运动传感器编程问题 [已关闭]

问题描述 投票:0回答:1

我一直在尝试在arduino上对这个运动传感器进行编程,以便在检测到时移动伺服系统,但它似乎根本不起作用我什至买了另一个,但仍然不起作用我尝试根据中间线的intrent指令链接它适用于 rthe boutput 和右侧 GND 左侧 V5,甚至反之亦然,所有方法似乎都不起作用,正如我所说,代码的想法非常简单,我什至向 chatgpt 询问了代码,在这里,它不起作用,我不知道出了什么问题:

    type #include <Servo.h>



    // Pin connections

    const int motionSensorPin = 2;  // Connect the motion sensor output pin to Arduino digital pin 2

    const int servoPin = 9;         // Connect the servo signal pin to Arduino digital pin 9



    // Servo configuration

    Servo myServo;                  // Create a servo object



    // Variables

    int servoPosition = 0;          // Initial position of the servo (adjust as needed)

    int motionState = LOW;          // Current motion state (LOW: no motion, HIGH: motion detected)

    int motionSensorState = LOW;    // Previous motion sensor state



    void setup() {

      myServo.attach(servoPin);     // Attach the servo to the specified pin

      pinMode(motionSensorPin, INPUT); // Set the motion sensor pin as input

      myServo.write(servoPosition); // Set the initial position of the servo

    }



    void loop() {

      motionSensorState = digitalRead(motionSensorPin); // Read the motion sensor state



      if (motionSensorState != motionState) {

        if (motionSensorState == HIGH) {

          servoPosition = 30;       // Set the servo position to 90 degrees when motion is detected
          servoPosition = -30;  

       } else {

                // Set the servo position to 0 degrees when motion stops

        }

        myServo.write(servoPosition); // Move the servo to the desired position

        motionState = motionSensorState; // Update the motion state variable

      }

    }here

我的代码#1 `

        #include <Servo.h>
    
    /**
    important note: stop the loop if close loop if away
    */
    Servo servo1; 
    const int PIN_TO_SENSOR = 2;   // the pin that OUTPUT pin of sensor is connected to
    int pinStateCurrent   = LOW; // current state of pin
    int pinStatePrevious  = LOW; // previous state of pin
    void setup() {
      // put your setup code here, to run once:
      servo1.attach(10);
    
       Serial.begin(9600);            // initialize serial
      pinMode(PIN_TO_SENSOR, INPUT); // set arduino pin to input mode to read value from OUTPUT pin of sensor
    
    }
    
    void loop() {
    
        pinStatePrevious = pinStateCurrent; // store old state
      pinStateCurrent = digitalRead(PIN_TO_SENSOR);   // read new state
    
      if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {   // pin state change: LOW -> HIGH
        Serial.println("Motion detected!");
         
         servo1.write(-30); 
      servo1.write(+30); 
      servo1.write(-30); 
        // TODO: turn on alarm, light or activate a device ... here
      }
      else
      if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {   // pin state change: HIGH -> LOW
        Serial.println("Motion stopped!");
         
         servo1.write(-30); 
      servo1.write(+30); 
      servo1.write(-30); 
        // TODO: turn off alarm, light or deactivate a device ... here
      }
    }

#2

    #include <Servo.h>
    
    /**
    important note: stop the loop if close loop if away
    */
    Servo servo1; 
    Servo servo2;
    Servo servo3; 
    
    const int PIN_TO_SENSOR = 2;   // the pin that OUTPUT pin of sensor is connected to
    int pinStateCurrent   = LOW; // current state of pin
    int pinStatePrevious  = LOW; // previous state of pin
    
    void setup() {
      Serial.begin(9600);            // initialize serial
      pinMode(PIN_TO_SENSOR, INPUT); 
      servo1.attach(10);// set arduino pin to input mode to read value from OUTPUT pin of sensor
    }
    
    void loop() {
      pinStatePrevious = pinStateCurrent; // store old state
      pinStateCurrent = digitalRead(PIN_TO_SENSOR);   // read new state
    
      if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {   // pin state change: LOW -> HIGH
        Serial.println("Motion detected!");
    
      servo2.write(0); 
     delay(90);
        // TODO: turn on alarm, light or activate a device ... here
      }
      else
      if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {   // pin state change: HIGH -> LOW
        Serial.println("Motion stopped!");
    
      servo2.write(0); 
     delay(90);
        // TODO: turn off alarm, light or deactivate a device ... here
      }
    }

#3

    #include <Servo.h>
    
    
    Servo servo1; 
    Servo servo2;
    Servo servo3; 
    
    const int PIN_TO_SENSOR = 2;   // the pin that OUTPUT pin of sensor is connected to
    int pinStateCurrent   = LOW; // current state of pin
    int pinStatePrevious  = LOW; // previous state of pin
    
    void setup() {
      Serial.begin(9600);            // initialize serial
      pinMode(PIN_TO_SENSOR, INPUT); 
      servo1.attach(10);// set arduino pin to input mode to read value from OUTPUT pin of sensor
    }
    
    void loop() {
      pinStatePrevious = pinStateCurrent; // store old state
      pinStateCurrent = digitalRead(PIN_TO_SENSOR);   // read new state
    
      if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {   // pin state change: LOW -> HIGH
        Serial.println("Motion detected!");
    
      servo2.write(0); 
     delay(90);
        // TODO: turn on alarm, light or activate a device ... here
      }
      else
      if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {   // pin state change: HIGH -> LOW
        Serial.println("Motion stopped!");
    
      servo2.write(0); 
     delay(90);
        // TODO: turn off alarm, light or deactivate a device ... here
      }
    }

我不确定这是否是代码逻辑错误或硬件或连接错误,您可以建议这是我第一次遇到这种传感器,我仍然怀疑它是否可以工作

arduino android-sensors arduino-c++ motion servo
1个回答
0
投票

这段代码对我有用。我从您的代码中更改了它,以便控制所设置的相同伺服系统。它根据 PIR 向舵机写入不同的值,以便舵机移动。

#include <Servo.h>

Servo servo1; 
const int PIN_TO_SENSOR = 2;   // the pin that OUTPUT pin of sensor is connected to
int pinStateCurrent   = LOW; // current state of pin
int pinStatePrevious  = LOW; // previous state of pin

void setup() {
  Serial.begin(9600);            // initialize serial
  pinMode(PIN_TO_SENSOR, INPUT); 
  servo1.attach(10);// set arduino pin to input mode to read value from OUTPUT pin of sensor
}

void loop() {
  pinStatePrevious = pinStateCurrent; // store old state
  pinStateCurrent = digitalRead(PIN_TO_SENSOR);   // read new state

  if (pinStatePrevious == LOW && pinStateCurrent == HIGH) {   // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");

    servo1.write(0); 
    delay(90);
  }
  else if (pinStatePrevious == HIGH && pinStateCurrent == LOW) {   // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");

    servo1.write(90); 
    delay(90);
  }
}

#0

servoPosition = -30;
代码丢失。
#1 伺服写入之间需要延迟。
#2&3设置舵机1但写入舵机2,无论PIR如何,始终写入0到舵机

© www.soinside.com 2019 - 2024. All rights reserved.