Hello Folks,
I'm having problems making the alarm go off. I'll appreciate any help.
the clock works and update time and everything..
thanks in advance
////////////////////////////////////////////Libraries//////////////////////////////////////
#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
#include <dht.h>
#define DS3231_I2C_ADDRESS 0x68
#define DHT11_PIN 9
////////////////////////////////////////////Objects////////////////////////////////////////
LiquidCrystal_I2C lcd(0x3F,16,2); // Display I2C 16 x 2
RTC_DS1307 RTC; // library works with DS3231 as stated in the reading material
DateTime now;
dht DHT;
///////////////////////////////////////////////Button///////////////////////////////////////
int setTIME=2; // Button SET MENU'
int incr=4; // Button +
int decr=3; // Button -
int onDemand=5; // Date Button
int piezoPin=13; // Piezo buzzer
/////////////////////////////////////////////Day of the week//////////////////////////////
char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
/////////////////////////////////////////////Variables///////////////////////////////////////
int hhUP;
int mmUP;
int yyUP;
int bbUP;
int ddUP;
int menu = 0;
///////////////////////////////////// Alarm Varaiable ////////////////////////////////
int alarmHrs =12;
int alarmMins=0;
///////////////////////////////////////Setup//////////////////////////////////////////
void setup()
{
lcd.init();
lcd.backlight();
lcd.clear();
pinMode(setTIME,INPUT);
pinMode(incr,INPUT);
pinMode(decr,INPUT);
pinMode(onDemand,INPUT);
pinMode(piezoPin, OUTPUT);
Serial.begin(9600);
Wire.begin();
RTC.begin();
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// Set the date and time at compile time
RTC.adjust(DateTime(__DATE__, __TIME__));
}
// RTC.adjust(DateTime(__DATE__, __TIME__)); //removing "//" to adjust the time
// The default display shows the date and time
int menu=0;
}
//////////////////////////////////////Main Loop////////////////////////////////////////
void loop(){
{
DateTime now = RTC.now();
if (digitalRead(setTIME)){ menu = menu + 1; }
if (menu == 0){ updateDisplay();}
if (menu == 1){ DisplaySetHour();}
if (menu == 2){ DisplaySetMinute();}
if (menu == 3){ DisplaySetYear();}
if (menu == 4){ DisplaySetMonth();}
if (menu == 5){ DisplaySetDay();}
if (menu == 6){ setAlarmHrs();}
if (menu == 7){ setAlarmMins();}
if (menu == 8){ alarmReady();}
if (menu == 9){ StoreAgg(); delay(500); menu = 0;}
delay(100);
}
if (digitalRead(onDemand) == HIGH){
DATE();
}
if (alarmHrs == now.hour() && alarmMins == now.minute() && now.second() == 0){
soundAlarm();
}
}
/////////////////////////////////Date ON DEMAND///////////////////////////////////////////
void DATE(){
DateTime now = RTC.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Date: ");
if (now.day()<=9)
{
lcd.print("0");
}
lcd.print(now.day(), DEC);
ddUP=now.day();
lcd.print("/");
if (now.month()<=9)
{
lcd.print("0");
}
lcd.print(now.month(), DEC);
bbUP=now.month();
lcd.print("/");
lcd.print(now.year(), DEC);
yyUP=now.year();
lcd.setCursor(0,1);
lcd.print("Alarm: ");
lcd.print(alarmHrs, DEC);
lcd.print(":");
lcd.print(alarmMins, DEC);
delay(1000);
}
/////////////////////////////////Time and Temperature Display////////////////////////
void updateDisplay ()
{
DateTime now = RTC.now();
int chk = DHT.read11(DHT11_PIN);
int dd = now.dayOfWeek();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(weekDay[dd]);
lcd.setCursor(6,0);
if (now.hour()<=9)
{
lcd.print("0");
}
lcd.print(now.hour(), DEC);
hhUP=now.hour();
lcd.print(":");
if (now.minute()<=9)
{
lcd.print("0");
}
lcd.print(now.minute(), DEC);
mmUP=now.minute();
lcd.print(":");
if (now.second()<=9)
{
lcd.print("0");
}
lcd.print(now.second(), DEC);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(DHT.temperature);
lcd.print(" C");
lcd.setCursor(9,1);
lcd.print(DHT.humidity);
lcd.setCursor(15, 1);
lcd.print("%");
delay(1000);
}
///////////////////////////////// Hour Setting///////////////////////////////
void DisplaySetHour(){
lcd.clear();
DateTime now = RTC.now();
if(digitalRead (incr) == HIGH){
if( hhUP == 23){ hhUP = 0;}
else{hhUP = hhUP +1;}}
if( digitalRead (decr) == HIGH){
if( hhUP == 0 ){hhUP = 23;}
else{ hhUP = hhUP - 1;}}
lcd.setCursor(0,0);
lcd.print("Set hour:");
lcd.setCursor(0,1);
lcd.print(hhUP, DEC);
delay(200);
}
//////////////////////////////// Minutes Setting///////////////////////////////
void DisplaySetMinute(){
lcd.clear();
if(digitalRead(incr) == HIGH){
if ( mmUP == 59){ mmUP = 0;}
else{mmUP = mmUP + 1;}}
if(digitalRead(decr)==HIGH){
if ( mmUP == 0){ mmUP = 59;}
else{ mmUP = mmUP - 1;}}
lcd.setCursor(0,0);
lcd.print("Set Minutes:");
lcd.setCursor(0,1);
lcd.print(mmUP ,DEC);
delay(200);
}
/////////////////////////////// Year Setting //////////////////////////////////
void DisplaySetYear() {
lcd.clear();
if(digitalRead(incr) == HIGH){ yyUP = yyUP + 1;}
if(digitalRead(decr) == HIGH){ yyUP = yyUP - 1;}
lcd.setCursor(0,0);
lcd.print("Set Year:");
lcd.setCursor(0,1);
lcd.print(yyUP,DEC);
delay(200);
}
////////////////////////////////// Month Setting ///////////////////////////////
void DisplaySetMonth() {
lcd.clear();
if(digitalRead(incr) == HIGH){
if (bbUP == 12){ bbUP = 1;}
else { bbUP = bbUP + 1;}}
if(digitalRead(decr)==HIGH){
if ( bbUP == 1){ bbUP = 12;}
else{ bbUP = bbUP - 1;}}
lcd.setCursor(0,0);
lcd.print("Set Month:");
lcd.setCursor(0,1);
lcd.print(bbUP,DEC);
delay(200);
}
///////////////////////////////////// Date Setting //////////////////////////////
void DisplaySetDay() {
lcd.clear();
if(digitalRead(incr) == HIGH){
if ( ddUP == 31){ ddUP = 1;}
else{ ddUP = ddUP + 1;}}
if(digitalRead(decr) == HIGH){
if ( ddUP == 1){ ddUP = 31;}
else{ ddUP = ddUP - 1;}}
lcd.setCursor(0,0);
lcd.print("Set Day:");
lcd.setCursor(0,1);
lcd.print(ddUP ,DEC);
delay(200);
}
//////////////////////////////////// Saving Changes///////////////////////////////
void StoreAgg()
{
// Variable saving
lcd.clear();
lcd.setCursor(0,0);
lcd.print("SAVING IN");
lcd.setCursor(0,1);
lcd.print("PROGRESS");
RTC.adjust(DateTime(yyUP,bbUP,ddUP,hhUP,mmUP,30));
delay(200);
}
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Alarm Setting/////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Alarm Hours /////////////////////////////////
void setAlarmHrs(){
lcd.clear();
if(digitalRead(incr) == HIGH){
if(alarmHrs == 23){alarmHrs = 0;}
else{alarmHrs = alarmHrs + 1;}}
if(digitalRead(decr) == HIGH){
if (alarmHrs == 0){ alarmHrs = 23;}
else{alarmHrs = alarmHrs - 1;}}
lcd.setCursor(0,0);
lcd.print("Set Alarm Hour");
lcd.setCursor(0,1);
lcd.print(alarmHrs, DEC);
delay(200);
}
///////////////////////////////////////// Alarm Minutes ///////////////////////////////
void setAlarmMins(){
lcd.clear();
if(digitalRead(incr) == HIGH){
if(alarmMins == 59){ alarmMins = 0;}
else{alarmMins = alarmMins +1;}}
if(digitalRead(decr) == HIGH){
if(alarmMins<9){lcd.print(0);}
if(alarmMins == 0){ alarmMins = 59;}
else{ alarmMins = alarmMins - 1;}}
lcd.setCursor(0,0);
lcd.print("Set Alarm Min");
lcd.setCursor(0,1);
lcd.print(alarmMins, DEC);
delay(200);
}
//////////////////////////////////////Alarm Ready Message/////////////////////////
void alarmReady(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Alarm Ready! ");
lcd.setCursor(5,1);
lcd.print(alarmHrs, DEC);
lcd.print(":");
lcd.print(alarmMins, DEC);
delay(200);
}
///////////////////////////////// Alarm Sound ////////////////////////////////////
void soundAlarm(){
digitalWrite(piezoPin, HIGH);
delay(250);
digitalWrite(piezoPin, LOW);
delay(250);
}