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, CERtcDS1302<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(); }- 2Please try delaying only for like half a second instead of a full second in
Clock(). Currently you are probably occasionally missing the relevant second of an alarm.chrisl– chrisl2023-06-26 08:54:48 +00:00CommentedJun 26, 2023 at 8:54 - Thank you. Will give it a try.Nic Kemp– Nic Kemp2023-06-26 09:00:40 +00:00CommentedJun 26, 2023 at 9:00
- 1Also,
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.Fahad– Fahad2023-06-26 10:19:22 +00:00CommentedJun 26, 2023 at 10:19 - 1@Fahad: You changed the code while reformatting it. Maybe you should explain to the OP that the empty
elsestatements have no effect at all and can be removed.Edgar Bonet– Edgar Bonet2023-06-26 14:43:22 +00:00CommentedJun 26, 2023 at 14:43 - 1forget the seconds ... trigger the alarm on day, hour, minute ... insert a 60 second delay at end of alarm() so that it triggers only once every alarm timejsotola– jsotola2023-06-26 15:20:57 +00:00CommentedJun 26, 2023 at 15:20
1 Answer1
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.
Explore related questions
See similar questions with these tags.
