arduino-libraries / RTCZero

RTC Library for SAMD21 based boards
http://arduino.cc/en/Reference/RTC
GNU Lesser General Public License v2.1
78 stars 78 forks source link

An alarm for every hour #94

Open botkero opened 2 years ago

botkero commented 2 years ago

Hello, I have trouble setting an alarm for each hour (because after 23/24h is over). Sometimes it works and sometimes not. I would like to have some certainty. With enum Alarm_Match you select a scope. Example Match_SS

If I now set setAlarmSeconds(10), then the internal timer must land on a 10 and an alarm is triggered. The internal timer is not reset and counts up to 60 and then goes to 0, right?

The same thing happens with minutes. But for hours/day, I should expect the reset point to be at 24? In the RTCZero.h I find the comments a bit misleading, or I misunderstood something. In the end, aren't the comments the maximum possible value?

MATCH_SS: Max. 60 seconds → 1 minute
MATCH_MMSS: Max. 60 minutes → 1 hour
MATCH_HHMMSS: Max. 24 hours? → 1 day?

Here a code-snippet

// includes etc.
//...
bool trigger = true;

void setup() {
  // init rtc
  rtc.begin();
  rtc.setTime(0, 0, 0);
  rtc.setDate(1, 1, 2022);
  rtc.enableAlarm(rtc.MATCH_MMSS); // <--- for every hour, right?
  rtc.attachInterrupt(alarmMatch);
}

void loop() {

  if (trigger) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    getSensorData(data); // temp, hum, weight
    digitalWrite(LED_BUILTIN, LOW);

    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    getBatteryLevel(level); // voltage reader
    digitalWrite(LED_BUILTIN, LOW);

    digitalWrite(LED_BUILTIN, HIGH);
    delay(200);
    sendPacket(); // LoRaWAN
    digitalWrite(LED_BUILTIN, LOW);

    trigger = false;

    setNextAlarm(1);
    rtc.standbyMode(); // <-- is the location right?
  }
}

void alarmMatch() {
  trigger = true;
}

void setNextAlarm(uint8_t time) {
  uint8_t next = (rtc.getAlarmMinutes() + time) % 60; // <-- do I need modulo 60 or 24?
  rtc.setAlarmMinutes(next);
}

With my code snippet, I don't understand why I have an hourly rhythm with this. I would expect getAlarmMinutes to return me minutes and when I call setNextAlarm I pass a 1 and use setAlarmMinutes to set the next alarm to the next minute. But I don't get a minutely feedback from the Arduino, but hourly. And if it is now so wanted that this should be hourly, must my modulo be set down to 24?