功能
Arduino使用以下内容: 1.红外传感器 2. LED灯 3. MotorFan 4.接力
因此,当用户接近IR传感器时,它将给出' 1'的串行打印。因为它已经检测到接近,所以这将把LED和电机风扇的状态从低切换到高。但是,5s后,motorfan的状态将从HIGH切换到LOW,而LED的状态仍将保持HIGH状态,只要serialPrint为1'就会保持高电平状态。
但是,当用户离开红外传感器时,LED状态将在10秒后从HIGH切换到LOW。
我做了什么:
我所做的代码:
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的状态将从LOW切换到HIGH。来自红外传感器。然而,我面临的主要问题是MOTORFAN的状态在5秒后不会从HIGH切换到LOW,但是当serialPrint为&#39; 0时,两种状态都只会切换到LOW。
因此,我做错了什么?请帮助。谢谢。
答案 0 :(得分:0)
小心! Arduino上的前两个引脚用于串行通信,如果您将其用于其他内容则会中断。切换到更高的引脚号,问题应该消失。