Go Down

Topic: How to set a recurring Alarm using RTCZero and MKR1000 (Read 710 times) previous topic - next topic

RoddyJoff

I've been trying to understand the Alarm utilities of the RTCZero library over the last few days and am making little progress.

I have found numerous examples which all focus on setting an alarm time and then triggering an event at that alarm time - they work as expected.

However I'd like to set a recurring alarm that triggers every 15 minutes and am at a loss.

If I use rtc.enableAlarm(rtc.MATCH_SS); then whatever value I set the alarmMinutes to the event triggers every minute.

If I use rtc.enableAlarm(rtc.MATCH_MMSS); then there is no trigger.

My goal is to use RTCZero library to put an MKR1000 into low-power/sleep mode to conserve battery power.

Any help will be much appreciated.

(I'm using the printDate and printTime routines just to keep track of what's happening.)


Code: [Select]

#include <RTCZero.h>

RTCZero rtc;

bool matched = false;
int alarmMinutes = 15;

void setup()
{
  Serial.begin(9600);

  rtc.begin();
  rtc.setAlarmMinutes(alarmMinutes);
  rtc.enableAlarm(rtc.MATCH_SS);
//  rtc.enableAlarm(rtc.MATCH_MMSS);
  rtc.attachInterrupt(alarmMatch);
}

void loop()
{
  Serial.println("Waiting for alarm ....");
  printDate();
  printTime();
  Serial.println();
  delay(1000);
 
  if (matched) {
    matched = false;
    Serial.println("Bingo ................................................");
    printDate();
    printTime();
    Serial.println();
    delay(1000);
   
    rtc.setAlarmMinutes(alarmMinutes);
    rtc.enableAlarm(rtc.MATCH_SS);
    rtc.attachInterrupt(alarmMatch);

    rtc.standbyMode();    // Sleep until next alarm match
  }
}

void alarmMatch()
{
  matched = true;
}

void printTime()
{
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());
  Serial.println();
}

void printDate()
{
  Serial.print(rtc.getDay());
  Serial.print("/");
  Serial.print(rtc.getMonth());
  Serial.print("/");
  Serial.print(rtc.getYear());
  Serial.print(" ");
}

void print2digits(int number) {
  if (number < 10) {
    Serial.print("0");
  }
  Serial.print(number);
}

pylon

Quote
If I use rtc.enableAlarm(rtc.MATCH_MMSS); then there is no trigger.
That should trigger once an hour, so in your case 15 minutes after power up, 75 minutes after power up, 135 minutes after power up and so on.

The easiest way to achieve your goal is to read the RTC minutes after wake up, add 15 to it, take it modulo 60 and write that to the alarm minutes. This way it will wake up exactly 15 minutes after the last wake call.

RoddyJoff

Excellent, many thanks.

I'll try that this morning.

Cheers.

RoddyJoff

Hi Pylon,

Your suggestion works a treat!

Kind regards,

Go Up