【发布时间】:2016-10-21 14:57:32
【问题描述】:
功能:
Arduino 使用以下功能: 1.红外传感器 2. LED灯 3. 马达风扇 4. 接力
因此,当用户接近 IR 传感器时,它会给出一个 serialPrint 为“1”,因为它检测到接近,这会将 LED 和电机风扇的状态从 LOW 切换到 HIGH。但是,5s 后,motorfan 的状态会从 HIGH 切换到 LOW,而 LED 的状态仍然会保持 HIGH,只要 serialPrint 为 '1' 就会保持 HIGH。
但是,当用户离开红外传感器附近时,LED 状态会在 10 秒后从高电平切换到低电平。
我做了什么:
我做过的代码:
const int signalPin = 1; //wire pin to analog for IR Sensor
//Motor-Fan Relay
byte FanRelay = 4;
byte LightRelay = 6;
int IRSignal; //variable signal, will hold the analog value read by Arduino
long duration;
int distance;
unsigned long Timer;
unsigned long Interval = 10000; //teh repeat Interval
unsigned long MotorFanOff = 5000;
void setup()
{
//Execute only once at startup
//pinMode (FanPin , OUTPUT) ; // Set pinMode for FanPin as OUTPUT, display
pinMode (signalPin, INPUT); //infared sensor line will be an input to the Arduino
pinMode(FanRelay, OUTPUT);
pinMode(LightRelay, OUTPUT);
Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}
void loop()
{
//execute multiple times in a loop
IRSignal = analogRead(signalPin); //arduino reads the value from the infared sensor
distance = 9462 / (IRSignal -16.92);
if(distance < 30 && distance > 0)
{
Timer = millis();
// Write a pin of HIGH
Serial.println("1");
//Set motor-fan to operate
digitalWrite (FanRelay, HIGH);
digitalWrite (LightRelay, HIGH);
//After a delay of 5s, MotorFan will toggle to LOW
//Toggle MotorFan to LOW after 5s
if ((millis()-Timer)>MotorFanOff){
digitalWrite (FanRelay, LOW);
}
}
else
{
Serial.println("0");
//Check if Timer is longer than 10s
if ((millis()-Timer)>Interval){
digitalWrite (LightRelay, LOW);
digitalWrite (FanRelay, LOW);
}
}
delay(1000);
}
问题:
此时,在测试时,当红外传感器的 serialPrint 为“1”时,LED 和电机风扇的状态都会从低切换到高。但是,我面临的主要问题是 MOTORFAN 的状态在 5 秒后不会从 HIGH 切换到 LOW,但是当 serialPrint 为“0”时,这两种状态都只会切换到 LOW。
那么,我做错了什么?请帮助。谢谢。
【问题讨论】:
标签: arduino arduino-uno