RTCZero.h

hey,

I try to built a code which “wakes” up my arduino board, call a function (here: “alarmMatch()”) and let the controller fall "asleep " afterwards.
Details:

  • I want to call the function “alarmMatch()”-function two times every minute.
  • First after 10 seconds and than 20 seconds later. (E.g. at 00:10 and 00:30, 01:10 and 01:30, …)

The first call (00:10, 01:10, 02:10, …) works, but not the second call (at: 00:30, 01:30, …)

Unfortunately I couldn’t find a mistake in my code…

#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;

// ISRs require them to be "volatile"
volatile bool needToSleep = false;
volatile bool sleeperChange = false;

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);

}

void loop() {
    //Change from Sleeper-awake-time "One" to "Two"  
  configSleeper();

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


}

void alarmMatch()
{

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

  digitalWrite(LED_BUILTIN, ledState);   // turn the LED on (HIGH is the voltage level)
  
  // set(change) the sleepingChange flag
  if (sleeperChange == false){
    sleeperChange = true;
  }
  if (sleeperChange = true) {
    sleeperChange = false;
  }

  // set the sleeping flag
  needToSleep = true;
}

void configSleeper()
{
  if (sleeperChange == false) {
    rtc.detachInterrupt();
    rtc.setAlarmTime(17, 00, 10);
    rtc.enableAlarm(rtc.MATCH_SS);

    rtc.attachInterrupt(alarmMatch);

    rtc.standbyMode();
  }
  else if (sleeperChange == true){
    rtc.detachInterrupt();
    rtc.setAlarmTime(17, 00, 30);
    rtc.enableAlarm(rtc.MATCH_SS);

    rtc.attachInterrupt(alarmMatch);

    rtc.standbyMode();    
  }

}

Thanks!

If you detatch the interrupt, what will wake it up?

Perehama:
If you detatch the interrupt, what will wake it up?

Good point! I thougt I need " rtc.detachInterrupt();" to "clear" the alarm in order to set a new one. But also without rtc.detachInterrupt(); it does not work

I found an ugly way in solving the problem:

#include <RTCZero.h>

/* Create an rtc object */
RTCZero rtc;

/* Change these values to set the current initial time */
byte seconds = 0;
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;

// ISRs require them to be "volatile"
volatile bool needToSleep = false;
volatile bool sleeperChange = false;

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() {
 if (needToSleep) {
   Serial.println("Go to sleep mode");
   // clear the flag
   needToSleep = false;
   

  if (sleeperChange == false) {
    seconds = 50; //means: call in 20sec (01:10 - 00:50)
    rtc.setTime(hours, minutes, seconds);
  }
  else if (sleeperChange == true){
    seconds = 30; //means: call in 40sec (01:10 - 00:30)
    rtc.setTime(hours, minutes, seconds);  
  }
  if (sleeperChange == false) sleeperChange = true;
  else sleeperChange = false;
   
   rtc.standbyMode();    // Sleep until next alarm match
 }
}

void alarmMatch()
{
  if (ledState == LOW) ledState = HIGH;
  else ledState = LOW;

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

  // set the sleeping flag
  needToSleep = true;
}