【发布时间】:2016-11-22 04:22:55
【问题描述】:
功能:
利用红外传感器切换 LED 灯和电机风扇的状态。因此,当满足触发距离条件时,LED 灯和 motorFAN 状态都会从 LOW 切换到 HIGH。延迟 5 秒后,只要满足触发距离条件,motorFAN 状态将从 HIGH 切换到 LOW,而 LED 灯的状态将保持 HIGH。当触发距离不再满足时,LED 灯将切换到 LOW。
问题:
当满足触发距离条件时,LED 灯和 motorFAN 状态都会从 LOW 切换到 HIGH。
但是,motorFan 状态在 5 秒延迟后不会从 HIGH 切换到 LOW,它仍将保持 HIGH。
第二个问题,当触发距离条件不再满足时,LED 灯和电机风扇状态都从 HIGH 切换到 LOW 之后。当再次满足条件时,只有 LED 灯从 LOW 切换到 HIGH,但电机风扇状态不切换,保持在 LOW 状态。
因此,需要寻求帮助以纠正问题以及做错了什么。 谢谢。
代码:
const int signalPin = 1; //wire pin to analog for IR Sensor
//Motor-Fan connected to arduino pin number
//const int FanPin = 5;
//Motor-Fan Relay
byte FanRelay = 4;
//const int FanRelay = 4;
//Light Relay
byte LightRelay = 6;
//const int LightRelay = 5;
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 SmellInterval = 10000;
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);
Serial.println(distance);
if(distance < 30 && distance > 0)
{
Timer = millis();
// Write a pin of HIGH
Serial.println("1");
digitalWrite (FanRelay, HIGH);
digitalWrite (LightRelay, HIGH);
if (Timer > SmellInterval){
//digitalWrite (FanPin, LOW);
digitalWrite (FanRelay, LOW);
}
}
else
{
Serial.println("0");
//Check if Timer is longer than 10s
if ((millis()-Timer)>Interval){
//digitalWrite (FanPin, LOW);
//digitalWrite (FanRelay, LOW);
digitalWrite (LightRelay, LOW);
}
}
delay(1000);
}
【问题讨论】:
标签: arduino delay arduino-uno