DS3231 function

Hi.
I m using the following function in order to set up an Alarm 5 mins later than the current time.
Most of the times is working fine..
BUT
occasionally it triggers the alarm a few seconds later than the current time and NOT 5 mins later. :o
Can someone please tell me why ?

Thank you in advance.

void Set_Failure_Alarm()
{
  Clock.getTime(year, month, date, DoW, hour, minute, second);
  delay(1000);

  Clock.setA1Time(DoW, hour, minute+5, 00, 8, false, false, false);
  delay(100);
  Clock.turnOnAlarm(1);  // Turn on A1 alarm, with external interrupt
  delay(100);
 // Serial.println("FAILURE Alarm  1 SET +5 MINUTES");

  //Clock.setA2Time(00, hour, minute+10, 64, false, false, false);
  Clock.turnOffAlarm(2); // Turn on A2 alarm, with external interrupt
  delay(100);
  // Serial.println("FAILURE Alarm  2 TURNED OFF");
}
Clock.setA1Time(DoW, hour, minute+5, 00, 8, false, false, false);

What if it is 11:57?

You don't really expect reasonable answers based on that snippet, do you? It isn't even clear what Clock is an instance of.

You are right Paul…

#include <DS3231.h> // RTC
DS3231 Clock;

//---------------------- RTC ---------
byte year, month, date, DoW, hour, minute, second;
//---------------------- RTC ---------

I guess is more clear now.

Delta_G:

Clock.setA1Time(DoW, hour, minute+5, 00, 8, false, false, false);

What if it is 11:57?

I think that this is the problem.
But how can i solve it ?

What about this ?

void Set_Failure_Alarm()
{
  byte alm_hour , alm_min ;
  
  Clock.getTime(year, month, date, DoW, hour, minute, second);
  delay(1000);
  alm_min = minute+5 ;
  
if ( alm_min > 59 )
{
  alm_min = alm_min-60 ;
  Clock.setA1Time(DoW, hour+1, alm_min, 00, 8, false, false, false);
  delay(100);
  Clock.turnOnAlarm(1);  // Turn on A1 alarm, with external interrupt
  delay(100);
}
else
{
  Clock.setA1Time(DoW, hour, minute+5, 00, 8, false, false, false);
  delay(100);
  Clock.turnOnAlarm(1);  // Turn on A1 alarm, with external interrupt
  delay(100);
  
}
 // Serial.println("FAILURE Alarm  1 SET +5 MINUTES");

  //Clock.setA2Time(00, hour, minute+10, 64, false, false, false);
  Clock.turnOffAlarm(2); // Turn on A2 alarm, with external interrupt
  delay(100);
  // Serial.println("FAILURE Alarm  2 TURNED OFF");
}

Use the Time library.

tmElements_t timeAdjustSeconds(tmElements_t elements, long seconds)
{
  time_t t;
  tmElements_t u;

  t = makeTime(elements);
  breakTime(t + seconds, u);
  return u;
}

aarg:
Use the Time library.

tmElements_t timeAdjustSeconds(tmElements_t elements, long seconds)

{
 time_t t;
 tmElements_t u;

t = makeTime(elements);
 breakTime(t + seconds, u);
 return u;
}

Thank you for your answer ...but...How ?

Below is the last mod i made...
i guess that all cases covered....right ?

void Set_Failure_Alarm()
{
  byte alm_hour , alm_min ;
  
  Clock.getTime(year, month, date, DoW, hour, minute, second);
  delay(1000);
  alm_min = minute+5 ;
  
if ( alm_min > 59 )
{
  alm_min = alm_min-60 ;
  alm_hour = hour+1 ;
  if (alm_hour >23)
  {
    alm_hour = 0 ;
  }
  
  Clock.setA1Time(DoW, alm_hour, alm_min, 00, 8, false, false, false);
  delay(100);
  Clock.turnOnAlarm(1);  // Turn on A1 alarm, with external interrupt
  delay(100);
}
else
{
  Clock.setA1Time(DoW, hour, alm_min, 00, 8, false, false, false);
  delay(100);
  Clock.turnOnAlarm(1);  // Turn on A1 alarm, with external interrupt
  delay(100);
  
}
 // Serial.println("FAILURE Alarm  1 SET +5 MINUTES");

  //Clock.setA2Time(00, hour, minute+10, 64, false, false, false);
  Clock.turnOffAlarm(2); // Turn on A2 alarm, with external interrupt
  delay(100);
  // Serial.println("FAILURE Alarm  2 TURNED OFF");
}

No. If it's 23:57 on Monday the 3rd, your alarm will be set to 00:03 on Monday the 3rd.
And if it's 23:57 on December 31... well... :slight_smile:

In your scheme, my suggestion would work something like this.

tmElements_t  alarmTime = timeAdjustSeconds(now(), 5 * SECS_PER_MINUTE );
Clock.setA1Time(alarmTime.Day, alarmTime.Hour, alarmTime.Minute, 00, 8, false, false, false);

Yep...you are right !!! :wink:

Thank you aarg for your help !!

Remember to set the sync source of the time library to the RTC, or you will be getting the time from the AVR.