Hi everyone, im quite new to arduino and was interested in doing something like an alarm but the function is that when the alarm is == to the RTC timing, the buzzer will start to ring for a minute. But the thing is once i try with my codes, it will work when i seperate the door sensor but once i try and put it together again, it will ring again.. Please help me (This would greatly help me as i can use the similar style to input my Sigfox Unabiz device in too)
Please look at the 'Void alarm1()'
#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("Alarm1");
// 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();
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
{
if( Hor == 19 && (Min == 6 || Min == 7) && state == LOW) //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{ //Once user open the door sensor, the alarm will stop buzzing
noTone(buz);
}
delay(200);
}
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
How many of these need to be global?
else{ //Once user open the door sensor, the alarm will stop buzzing
noTone(buz);
}
That comment is wrong. You do NOT want to use an else here. You want to turn the buzzer off if it is before 19:06 (it shouldn't be on), if it is after 19:08, OR if the door is open.
I assume that the comment associated with the uselessly-named state variable means that it is supposed to hold the state of a switch that is pressed when the door is closed.
But, nowhere do you actually read the state of the pin that the switch is connected to, if there is such a switch, so it is pointless to talk about shutting the noise off when the door is closed.
Let's say if i were to remove the else since it will make it complicated.. i changed it to else if and the condition is almost the same except for detecting the state of the switch which is HIGH, but after changing, i tried to run and it is almost the same with my previous code..
i tried to off the switch, it works by stopping the buzzer but once i on the switch, it continued to sound the buzzer (It started and ended the alarm perfectly, did not exceed nor ring before the alarm)
But i just want the alarm to stop once i OFF the switch (Which i dont want it to sound anymore)
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);
}