Sleep RTC

hey,

I’m using an adafruti feather m0 and the RTC library: Arduino - RTC

I aim to call a function (here named: alarmMatch) every minute. When the function isn’t called, the microcontroller should “sleep” with “rtc.standbyMode();”.

It doesn’t work, but I wonder why. Thanks!

#include <RTCZero.h>

/* Create an rtc object */
RTCZero rtc;

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 00;
const byte hours = 17;

/* Change these values to set the current initial date */
const byte day = 17;
const byte month = 11;
const byte year = 15;

int ledState = LOW;

void setup()
{
  // open the serial port at 9600 bps:
  Serial.begin(9600);
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, ledState);
  //Serial.println("Buff is full, t.i. stopped"); 

  rtc.begin();

  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(day, month, year);

  rtc.setAlarmTime(17, 00, 10);
  rtc.enableAlarm(rtc.MATCH_SS);

  rtc.attachInterrupt(alarmMatch);

  rtc.standbyMode();
}

void loop()
{

}

void alarmMatch()
{

  if (ledState == LOW) ledState = HIGH;
  else ledState = LOW;

  digitalWrite(LED_BUILTIN, ledState);   // turn the LED on (HIGH is the voltage level)
  rtc.standbyMode();


}

I don’t want to do it like shown in the example: https://www.arduino.cc/en/Tutorial/SleepRTCAlarm

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

void alarmMatch()
{
  digitalWrite(LED_BUILTIN, HIGH);
}
...

Because I finally want to integrate it into another code, where a timer interrupt function should run as long as rtc.standbyMode(); is called

You shouldn't go in standby during an interrupt.

ToddL1962:
You shouldn't go in standby during an interrupt.

Thanks for your response!
I first want to stop the timer and then go into sleep mode.

Set a flag and do it in loop().

Yes, but declare needToSleep as ‘volatile’ and make sure it’s a single byte type, or else you have to turn off interrupts and make a copy in loop().

aarg:
Set a flag and do it in loop().

Good point, thanks!

You mean like:

.
.

void loop() {
   if (needToSleep) {
 Serial.println("Go to sleep mode"); 
 // clear the flag
 needToSend = false;
 rtc.standbyMode();    // Sleep until next alarm match
 }
}

void TC5_Handler (void) {


    //Stop the timer interrupt
  if (condition == true) { 
    
     // Set the flag and get out
    needToSend = true;
  }
  
 
  TC5->COUNT16.INTFLAG.bit.MC0 = 1; //Writing a 1 to INTFLAG.bit.MC0 clears the interrupt so that it will run again

}
.
.

aarg:
Yes, but declare needToSleep as 'volatile' and make sure it's a single byte type, or else you have to turn off interrupts and make a copy in loop().

Thanks for your help!