2017-11-23 17 views
0

저는 Arduino 코딩을 처음 접했습니다. 현재 알람을 울리려고합니다. 알람이 울리면 사용자는 알람을 중지하고 다음 알람을 듣기 위해 도어 센서를 한 번만 제거해야합니다. 내가 가지고있는 것은 시간을 추적하는 실시간 시계 모듈입니다.도어 센서를 제거하면 알람을 끄는 방법?

그래서 저는 독자적으로 코드를 작성하려고했지만 실패했습니다. 결과는 여기에 있습니다. 알람이 울린 후, 나는 서로 도어 센서를 제거한다. 그것은 윙윙 거리는 소리를 멈추는 데 (부분적으로는 올바르지 만) 한 번 돌려 주면 알람 지속 시간 (1 분으로 설정) 동안 울립니다. 여기

코드의 내가 가진 :

#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); 
} 

그냥 내가

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

에서했다 변수 상태가 그래서 내 문제를 요약하는 것을 명확히하기 위해, 나는로 시스템을 얻기 위해 노력하고 있어요 문 센서 번을 번 읽음으로써 문 센서를 다시 넣어도 알람이 멈추고 다음 알람을 듣습니다.

누군가가 나를 도울 수 있기를 바랍니다. 당신에게 친절한 관심을 가져 주셔서 감사합니다.

피씨 나는 내가 풀 수없는 논리라고 생각한다.

답변

0

당신의 상태 변수가 높거나 낮은 입력 핀의 상태를 읽고 있기 때문에 부울이어야한다고 생각합니다. 또한, 나는 당신이 당신의 논리에서 무엇을하려고하는지 이해하지 못합니다. 20:11 또는 20:12에만 자기 센서를 확인하려고합니까? 시간이 여전히 20:11 또는 20:12 인 동안 센서를 다시 넣으면 다시 소리가납니다.