Hi, how should I permanently synchronize my alarm program (as per below code) from RTC time?
My RTC is working fine. It's my program that doesn't synchronize its time from the RTC. When power is off in my arduino project, the RTC retains its correct time and the LCD display, however the alarm schedules resets its time to 0:0:0 year 2000 every time the power is turned-off. I verified it via serial monitor.
The TimeAlarms program seems dependent on the Time library's default time.
When I upload the code with correct time settings (set time command), the alarm time period seems to start only when I clicked the serial monitor (the serial time output is delayed compared to the actual time of the RTC). This means that when I set time to 10,0,0 - and just upload the code 5 minutes later, then opening the serial monitor will still display 10:0:0 even if the real time is already 10:05:00
How to fix this problem? I'm just a newbie in arduino.
#include <DS3231.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#define buz 9
#define buzz 12
#define boz 8
const int buzzer = 12; //buzzer to arduino pin 9
int Hor;
int Min;
int Sec;
// Init the DS3231 using the hardware interface
DS3231 rtc(SDA, SCL);
// Init a Time-data structure
Time t;
void setup()
{
// Setup Serial connection
Serial.begin(19200);
rtc.begin();
pinMode(buz, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(boz, OUTPUT);
lcd.begin(16,2);
setTime(23,55,0,14,5,18); // set time to Saturday 8:29:00am 14 May 1 2018
// create the alarms
Alarm.alarmRepeat(7,30,0,MorningAlarm);
Alarm.alarmRepeat(8,30,0,MorningAlarm);
Alarm.alarmRepeat(12,0,0,EveningAlarm);
Alarm.alarmRepeat(15,0,0,EveningAlarm);
Alarm.alarmRepeat(17,30,0,EveningAlarm);
Alarm.alarmRepeat(20,0,0,EveningAlarm);
//Alarm.alarmRepeat(dowMonday,23,10,30,WeeklyAlarm); // 8:30:30 every Saturday
//Alarm.timerRepeat(10, Repeats); // timer for every 15 seconds
//Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
}
void loop()
{
t = rtc.getTime(); // Get data from the DS3231
Hor = t.hour;
Min = t.min;
Min = t.min;
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(rtc.getTimeStr());
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.print(rtc.getTimeStr());
delay(1000);
lcd.setCursor(0,1);
lcd.print("Date: ");
lcd.print(rtc.getDateStr());
delay(1000);
lcd.setCursor(0,1);
lcd.print("Day: ");
lcd.print(rtc.getDOWStr());
lcd.print(" ");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(rtc.getTemp());
lcd.print(" C");
lcd.print(" ");
delay(1000); //Delay is for displaying the time in 1 second interval.
digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println("Alarm: - turn lights off");
}
void EveningAlarm(){
Serial.println("Alarm: - turn lights on");
void Buzzer();
digitalWrite(buz,HIGH);
delay(1000);
digitalWrite(buz, LOW);
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
}
void WeeklyAlarm(){
Serial.println("Alarm: - its Monday Morning");
void Buzzer();
digitalWrite(buz,HIGH);
delay(1000);
digitalWrite(buz, LOW);
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
}
void ExplicitAlarm(){
Serial.println("Alarm: - this triggers only at the given date and time");
}
void Repeats(){
Serial.println("15 second timer");
void Buzzer();
digitalWrite(buz,HIGH);
delay(1000);
digitalWrite(buz, LOW);
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
tone(buzzer, 3000); // Send 1KHz sound signal...
delay(50); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(50); // ...for 1sec
}
void OnceOnly(){
Serial.println("This timer only triggers once");
}
void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}