基本上,我正在制作一个Arduino闹钟,只有在激活了Pir传感器后才会关闭。我只是想让压电蜂鸣器在时间在12:01到12:05之间时进行快速演示,并且如果在这段时间内激活了Pir传感器,则压电蜂鸣器将关闭并且该功能无效。问题在于,一旦时间是12:01和12:05并且激活了运动传感器,压电蜂鸣器就不会关闭,并且串行监视器也不会一直打印“ No Motion!”。应在何时打印“检测到运动!”
这是我的代码
//Digital Arduino Clock Code
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 4, 8, 9, 10, 11);
int h=12;
int m;
int s;
int flag;
int TIME;
int ledPin = 13;
int inputPin = 2;
int pirState = LOW;
int val = 0;
int buzzer = 7;
const int hs=8;
const int ms=9;
int state1;
int state2;
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare Sensor as input
lcd.begin(16,2);
Serial.begin(9600);
}
void loop()
{
lcd.setCursor(0,0);
s=s+1;
lcd.print("TIME:");
lcd.print(h);
lcd.print(":");
lcd.print(m);
lcd.print(":");
lcd.print(s);
if(flag<12)lcd.print("AM");
if(flag==12)lcd.print("PM");
if(flag>12)lcd.print("PM");
if(flag==24)flag=0;
delay(1000);
lcd.clear();
if(s==60){
s=0;
m=m+1;
}
if(m==60)
{
m=0;
h=h+1;
flag=flag+1;
}
if(h==13)
{
h=1;
}
lcd.setCursor(0,1);
lcd.print("HAVE A NICE DAY");
//alarm clock function w/ PIR sensor and Buzzer
if(h==12 && (m>=1 && m<=5) && (pirState == LOW))
{
Serial.println("No Motion!!");
lcd.clear();
lcd.setCursor(0,1);
lcd.print("WAKEUP");
tone(buzzer, 50, 100);
pirState = HIGH;
/
}
else if(h==12 && (m>=1 && m<=5) && (pirState == HIGH))
{
Serial.println("Motion DETECTED!");
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Alarm Deactivated");
noTone(buzzer);
pirState == LOW;
}
else
{
noTone(buzzer);
}
}