Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alarms do not fire when time is changed #28

Open
kdhooper opened this issue Jun 29, 2017 · 3 comments
Open

Alarms do not fire when time is changed #28

kdhooper opened this issue Jun 29, 2017 · 3 comments

Comments

@kdhooper
Copy link

When changing the time in the Time library, TimeAlarms misses alarms.

The expected behavior is:

update() {
  if(last_update_time < alarm_time < present_update_time) {
    TriggerAlarmCallback();
  }
}

..in other words, every instance of the time passing through a recurring alarm should result in the alarm firing.

You can demonstrate this bug by

  • Set the time for 7:00AM,
  • Set a recurring alarm for 7:02AM
  • Watch it trigger
  • Set the time again to 7:00AM
  • The alarm will not trigger the second time

A more practical example of this bug is if the time is set to 7:00PM and you run the clock back to 7:00AM, the alarm will not trigger. This is highly likely to cause alarms (possibly for the whole day) to be missed, and is something a user of an alarm clock might do.

Workaround:
Update the alarm each instance of updating the time:

void saveTimeCurrentTime(uint8_t setHour, uint8_t setMinute) {
  setTime(setHour, setMinute, 0, day(), month(), year());
  Alarm.write(gAlarmID1, AlarmHMS(gTime1.hours, gTime1.minutes, 0));
}

Where gTime1 struct holds the hour and minute of the alarm as a global variable.

@luckyhacky
Copy link

I have the same issue. Alarms are omited if NTPClient updated time.

@luckyhacky
Copy link

I found my problem. Just a short sketch for everybody using a similar code:

int ntp_time = 0;
NTPClient ...

void setup()
{
setSyncProvider(ntp_set_time);
...
}


void loop()
{
...
}

long ntp_set_time()
{
  while (ntp_time < 1000000)
  {
    ntpClient.update();
    ntp_time = ntpClient.getEpochTime();
    delay(1000);
  }

  Serial.print(ntpClient.getFormattedTime());
  Serial.print(" [TIME ] Time to set : " );
  Serial.println(ntp_time);

  return ntp_time;
}

Issue arises, due to ntp_time being global. So time is not updated.
A local variable ntp_time in function ntp_set_time would block, if connection to NTP servers fails.

My current solution:

long ntp_set_time()
{
  ntpClient.update();
  ntp_time = ntpClient.getEpochTime();
  
  while (ntp_time < 1000000)
  {
    ntpClient.update();
    ntp_time = ntpClient.getEpochTime();
    delay(1000);
  }

  Serial.print(ntpClient.getFormattedTime());
  Serial.print(" [TIME ] Time to set : " );
  Serial.println(ntp_time);

  return ntp_time;
}

This is just for information and not related to bug mentioned above.

@luckyhacky
Copy link

A proposal for retriggering as you suggested may be luckyhacky@9b198a9
you can test and give feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants