|
#define MQ2PIN 36 //烟雾报警器AO
#define FMQ 15 //蜂鸣器引脚
unsigned long detect_os_time = 0;
const uint8_t JDQ = 16; //控制风扇继电器
void setup()
{ 蜂鸣器是低电平触发
pinMode(FMQ,OUTPUT);
pinMode(MQ2PIN, INPUT);
Serial.begin(9600);
digitalWrite(FMQ,HIGH);
}
void MQ2_FanCtrol()
{
int MQ2Val = analogRead(MQ2PIN);
if(MQ2Val > 2000)
{
//digitalWrite(FMQ, LOW);//打开蜂鸣器
digitalWrite(JDQ, HIGH);//打开风扇
if(millis()- detect_os_time > 5000)
{
detect_os_time = millis();
digitalWrite(FMQ, HIGH);
Serial.println("GOGOGOG ");
digitalWrite(JDQ, LOW);
}
}
}
void loop()
{
Serial.println("Sensor AD Value = ");
Serial.println(analogRead(MQ2PIN));
MQ2_FanCtrol();
delay(1000);
}
我想烟雾传感器监测到大于2000值,然后就让蜂鸣器和电机转动,但是每次一监测到后,蜂鸣器就一直响,进入不了停止里边,我对定时方面不太懂,麻烦大佬为我解答一下。
|
|