2017-11-23 4 views
0

私はArduinoのコーディングにはかなり新しいです。現在、私はアラームを作成しようとしています。アラームが鳴ったら、ユーザーはドアセンサーを一度取り外すだけでアラームを停止し、次のアラームを聞く必要があります。私が持っているのは、時間を追跡するためのリアルタイムクロックモジュールです。ドアセンサーが取り外されたときにアラームをオフにするには?

私は自分でコードしようとしましたが、失敗しました。結果はここにあります。警報が鳴った後、私はお互いから扉のセンサーを取り外します。 (部分的に正しい)鳴動を止めますが、一度戻ってくると、アラームの持続時間(1分に設定されています)の間、鳴り続けます。

ここで私が持っているコードです:ちょうど私は私の問題を要約する

http://www.instructables.com/id/How-to-Use-a-Magnetic-Door-Switch-Sensor-With-Ardu/

から取った変数の状態は、私は、システムを取得しようとしていますことを明確にする

#include <DS3231.h> 
#include <Wire.h> 
#include <LiquidCrystal.h> 

LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 
DS3231 rtc(SDA, SCL); 
Time t; 
#define buz 11 
//This is Buzzer that is plug into pin 11 (Change it accordingly to what you will be putting it in the Arduino) 
int Hor;   // This is declaring the alarm in Hours 
int Min;   // This is declaring the alarm in Minutes 
int Sec;   // This is declaring the alarm in Seconds 
const int sensor = 10; // Door sensor connected to Pin 10 
int state; // 0 close - 1 open switch 

void setup() { 
    Wire.begin(); 
    rtc.begin(); 
    Serial.begin(9600); 
    pinMode(buz, OUTPUT); 
    lcd.begin(16,2);  
    lcd.setCursor(0,0); 
    lcd.print("Alarm"); 
    lcd.setCursor(0,1); 
    lcd.print("Test"); 
    // The following lines can be uncommented to set the date and time 
    //rtc.setDOW(WEDNESDAY);  // Set Day-of-Week to SUNDAY 
    //rtc.setTime(12, 0, 0);  // Set the time to 12:00:00 (24hr format) 
    //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 
    delay(2000); 
    pinMode(sensor, INPUT_PULLUP); 
} 

void loop() { 
    t = rtc.getTime(); 
    Hor = t.hour; 
    Min = t.min; 
    Sec = t.sec; 
    lcd.setCursor(0,0); 
    lcd.print("Time: "); 
    lcd.print(rtc.getTimeStr()); 
    lcd.setCursor(0,1); 
    lcd.print("Date: "); 
    lcd.print(rtc.getDateStr()); 
    alarm1(); 
    alarm2(); 
    delay(1000); 
} 

void Buzzer() { 
    digitalWrite(buz,HIGH); 
    delay(500); 
    digitalWrite(buz, LOW); 
    delay(500); 
} 

void alarm1() { 
    //E.g. This is the first alarm for the medicine 
    state = digitalRead(sensor); 
    if(Hor == 20 && (Min == 11 || Min == 12) && state == 0) { 
    //Comparing the current time with the Alarm time 
    Buzzer(); 
    lcd.clear(); 
    lcd.print("1st Alarm ON"); 
    lcd.setCursor(0,1); 
    lcd.print("Morning Medicine"); 
    } else if (Hor == 20 && (Min == 11 || Min == 12) && state == 1) { 
    noTone(buz); 
    } 
    delay(200); 
} 

void alarm2() { 
    if(Hor == 19 && (Min == 48 || Min == 49) && state == 0) { 
    //Comparing the current time with the Alarm time 
    Buzzer(); 
    lcd.clear(); 
    lcd.print("2nd Alarm ON"); 
    lcd.setCursor(0,1); 
    lcd.print("Afternoon Medicine"); 
    } else { 
    //Once user opens the door sensor, the alarm will stop buzzing 
    noTone(buz); 
    } 
    delay(200); 
} 

は、ドアセンサーの開封をに一度読んで、ドアセンサーを戻してもアラームが止まり、次のアラームを聞きます。

私は本当に誰かが私を助けることを願っています。あなたに親切に感謝します。

P.S私は解決できないような論理だと思います。

答えて

0

あなたの状態変数が高いか低い入力ピンの状態を読み込んでいるのでブール値でなければならないと思います。また、私は自分のロジックで何をしようとしているのか分からない。あなたは20:11または20:12に磁気センサーをチェックしようとしていますか?時間がまだ20時11分または20時12分になっている間にセンサーを戻すと、それが再びバズするのは理にかなっています。

関連する問題