2

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();      }
Greenonline's user avatar
Greenonline
3,1527 gold badges37 silver badges49 bronze badges
askedJun 26, 2023 at 8:51
Nic Kemp's user avatar
10
  • 2
    Please try delaying only for like half a second instead of a full second inClock(). Currently you are probably occasionally missing the relevant second of an alarm.CommentedJun 26, 2023 at 8:54
  • Thank you. Will give it a try.CommentedJun 26, 2023 at 9:00
  • 1
    Also,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.CommentedJun 26, 2023 at 10:19
  • 1
    @Fahad: You changed the code while reformatting it. Maybe you should explain to the OP that the emptyelse statements have no effect at all and can be removed.CommentedJun 26, 2023 at 14:43
  • 1
    forget 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 timeCommentedJun 26, 2023 at 15:20

1 Answer1

0

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.

answeredJul 14 at 16:03
Jeff Wahaus's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.