I have an alarm for "tea time", "lunch time" and "tea time". It works well at times but occasionally it gets stuck on the alarm not resetting, other times it does not trigger the alarm. Please help I am new to Arduino and all of this.
Here is the code:
#include <LiquidCrystal_I2C.h>
#include <ThreeWire.h>
#include <RtcDS1302.h>
ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
LiquidCrystal_I2C lcd(0x3f,16,2);
int Relay = 7;
void setup ()
{
//RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
//Rtc.SetDateTime(compiled);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" ClearChoice");
lcd.setCursor(0,1);
lcd.print(" Installations");
delay(5000);
lcd.clear();
pinMode(7, OUTPUT);
pinMode(8, INPUT);
digitalWrite(Relay, LOW);
}
void Alarm()
{
digitalWrite(Relay, HIGH);
delay(3000);
digitalWrite(Relay, LOW);
}
void AlarmActive()
{
RtcDateTime now = Rtc.GetDateTime();
int MySec = now.Second();
int MyMin=now.Minute();
int MyHour=now.Hour();
int MyDay=now.DayOfWeek();
if(MyHour==10 && MyMin==00 && MySec==00 && MyDay<=5)
{
Alarm();
}
if(MyHour==10 && MyMin==15 && MySec==00 && MyDay<=5)
{
Alarm();
}
if(MyHour==12 && MyMin==00 && MySec==00 && MyDay<=5)
{
Alarm();
}
if(MyHour==12 && MyMin==30 && MySec==00 && MyDay<=5)
{
Alarm();
}
if(MyHour==15 && MyMin==00 && MySec==00 && MyDay<=5)
{
Alarm();
}
if(MyHour==15 && MyMin==15 && MySec==00 && MyDay<=5)
{
Alarm();
}
}
void Clock()
{
RtcDateTime now = Rtc.GetDateTime();
int dayOfWeek = now.DayOfWeek();
String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
lcd.setCursor(0,0);
char buffer[3];
lcd.print("Date: ");
lcd.print(now.Day());
lcd.print("/");
lcd.print(now.Month());
lcd.print("/");
lcd.print(now.Year());
lcd.setCursor(0,1);
lcd.print("Time: ");
sprintf(buffer, "%02d", now.Hour());
lcd.print(buffer);
lcd.print(":");
sprintf(buffer, "%02d", now.Minute());
lcd.print(buffer);
lcd.print(":");
sprintf(buffer, "%02d", now.Second());
lcd.print(buffer);
lcd.setCursor(0,1);
Serial.print("Time: ");
Serial.print(now.Hour());
Serial.print(":");
Serial.print(now.Minute());
Serial.print(":");
Serial.print(now.Second());
Serial.println();
Serial.print("Date: ");
Serial.print(now.Day());
Serial.print("/");
Serial.print(now.Month());
Serial.print("/");
Serial.print(now.Year());
Serial.println();
lcd.print("Time: ");
Serial.println(days[dayOfWeek]);
Serial.println();
AlarmActive();
delay(1000);
}
void loop ()
{
Clock();
}
1 Answer 1
I think the bug is that you're only polling the time every 1 second. (i.e. the 1 second delay at the end of clock() )
There is overhead with polling so you may miss a 1 second resolution alarm trigger. Change the delay in clock() to 1/2 second and you'll fix the occasional miss issue.
Another potential issue that may occur if you fully tested the alarm possibilities is the 3 second delay in Alarm(). Then you're only polling at a 4 second interval and alarms close to each other (within 4 seconds) would glitch. Your current alarms are far enough apart that this shouldn't be an issue.
Clock()
. Currently you are probably occasionally missing the relevant second of an alarm.Alarm()
has a 3-second delay. This can contribute to the issue you are seeing, as @chrisl mentioned. Check out an example called "blink without delay". That should give me some idea about how NOT to use delay.else
statements have no effect at all and can be removed.