sleep for short period with MKR 1400 GSM

Hi,
I have a project where i need to put an MKR 1400 GSM board to sleep for 5s at a time, wake up, check a few things then go back to sleep for 5s etc etc. I have some test code to do this (see below) which works but only for less than a minute then hangs.

Or is there a better approach to getting short (few seconds) sleep delays with the rtc.standbyMode().

Any help is much appreciated.

Cheers.

//-----Includes-----//
#include <MKRGSM.h>
#include <RTCZero.h>

/* Create an rtc object */
RTCZero rtc;

#define LED LED_BUILTIN

//low power setup
const int lowPwrPin = A3;
int lowPwrPinState = 0; 

//-----default setup options
boolean sleeping=0;
bool matched = true;

void setup() {
  pinMode(LED, OUTPUT);
  
  //low pwr pin
  pinMode(lowPwrPin, INPUT);

  //Initialise the serial connection
  Serial.begin(9600);
  while ((!Serial) && (millis() < 10000)) {
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED, LOW);
    delay(500);
  }
  Serial.println("Serial monitor opened...");

  rtc.begin();
  
  lowPwrPinState = digitalRead(lowPwrPin);
  if (lowPwrPinState == HIGH) {sleeping=1;}
  else {sleeping=0;}
  
  if (sleeping==1) {
    Serial.println("Sleep mode ON");
    
    rtc.enableAlarm(rtc.MATCH_HHMMSS);
    rtc.attachInterrupt(alarmMatch);
  }
  else {Serial.println("Sleep mode OFF");}

}

void loop() {
  
  digitalWrite(LED, HIGH);
  delay(20);
  digitalWrite(LED, LOW);

  if (sleeping==1) {
    if (matched) {
      matched = false;
      int alarmSeconds = rtc.getSeconds();
      alarmSeconds += 5;
      if (alarmSeconds >= 60) {
        alarmSeconds -= 60;
      }
    
      rtc.setAlarmTime(rtc.getHours(), rtc.getMinutes(), alarmSeconds);
      rtc.standbyMode();    // Sleep until next alarm match
    }
  }
  else {
    Serial.println("Sleeping");
    //delay for 5 secs
    delay(5000);
  }
}

//=======Time=======//
void alarmMatch() {
  matched = true;
}

It looks that in the following segment of code you are not accounting for seconds rollover in the minutes of your alarm time setting which means after 1 minute you are setting the alarm for 1 minute in the past. Similarly you should account for minutes rollover and hour rollover as well.

For example, if seconds rollover you need to increment the minutes. When you increment the minutes you need to check to see if it rolls over and if so, increment the hour. Then you need to check to see if the hour rolls over past midnight and adjust for that.

    if (matched) {
      matched = false;
      int alarmSeconds = rtc.getSeconds();
      alarmSeconds += 5;
      if (alarmSeconds >= 60) {
        alarmSeconds -= 60;
      }
    
      rtc.setAlarmTime(rtc.getHours(), rtc.getMinutes(), alarmSeconds);
      rtc.standbyMode();    // Sleep until next alarm match
    }

Alternatively you can just use the millis() time call to see if 5 seconds have passed and then perform your checks.

void loop()
{
   static unsigned long prevTime = millis();

  if (millis() - prevTime >= 5000)
  {
     // TBD: perform checks

     prevTime = millis();
  }
}