所以我在arduino的3.3v引脚,arduino的gnd引脚上运行PIR,然后进入输入9.我将代码调整到网站,但它仍然无法正常工作。我希望继电器在PIR为低电平时接通,然后在PIR为高电平时关闭。我还需要PIR在注册另一个输入之前等待至少5秒钟。
#define RELAY_ON 1
#define RELAY_OFF 0
#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
#define Relay_5 6
#define Relay_6 7
/*-----( Declare Variables )-----*/
int waittime; // Delay between changes
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int pirPin = 9; //the digital pin connected to the PIR sensor's output
void setup() /****** SETUP: RUNS ONCE ******/
{
waittime = 1000;
pinMode(pirPin, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
//-------( Initialize Pins so relays are active at reset)----
digitalWrite(Relay_1, RELAY_ON);
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_3, RELAY_ON);
digitalWrite(Relay_4, RELAY_ON);
digitalWrite(Relay_5, RELAY_ON);
digitalWrite(Relay_6, RELAY_ON);
//---( THEN set pins as outputs )----
digitalWrite(pirPin, LOW);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_6, OUTPUT);
delay(4000); //Check that all relays are active at Reset
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
val = digitalRead(pirPin); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
//---( Turn all 8 relays ON in pairs)---
digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(waittime); // wait for a second
digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF
delay(waittime);
digitalWrite(Relay_5, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_6, RELAY_OFF);// set the Relay OFF
delay(5000); // wait see all relays ON
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
//---( Turn all 8 relays OFF in pairs)---
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(waittime);
digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
delay(waittime);
digitalWrite(Relay_5, RELAY_ON);// set the Relay ON
digitalWrite(Relay_6, RELAY_ON);// set the Relay ON
delay(10000); // wait see all relays OFF
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}

答案 0 :(得分:0)
当事情没有按照我期望的方式发挥作用时,我总是先看看接线。写一些简单的测试代码,除了打印PIR信号引脚的状态外什么都不做。你期望它会变高吗?你期望它会降低吗?如果没有,您有接线问题。在你考虑其余的编程之前,先把它弄清楚。
至于接线......你没有在你的描述中提到一个上拉电阻 - 大多数PIR模块(至少是我曾经使用过的)都需要一个上拉电阻信号引脚在没有信号时将输出拉高。也许你有一个而且没有提到它,但如果没有那么那可能是你的问题 - 没有上拉电阻,你的输出是浮动的,你的设备将无法按照你期望的方式工作。也就是说,大多数Arduino电路板(你没有说明你使用的是哪个)都有数字引脚上的内部上拉电阻,但你必须通过以下方式启用它们:
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
您正在设置()例程中将信号引脚设置为低电平,从而关闭内部上拉电阻。将其更改为HIGH以将其打开,然后您可以避免添加外部电阻。
另外,你确定你的PIR模块运行在3.3V?有些需要5V。检查其数据表以确定。