Time and TimeAlarms Libraries – Ask here for help or suggestions

This is the code that gets the UNIX time.

No, it isn't. The code that MIGHT get the UNIX time is the getTime() function, which you didn't bother posting. Post ALL of your code.

Hi,

If someone is using time.cpp on Arduino Mega 2560 and concerns about board running longer than 49 days without restart, then

function now() first chunk "
while( millis() - prevMillis >= 1000){
sysTime++;
prevMillis += 1000;
"
needs to be replaced with this chunk "
unsigned long currtime = millis();
if (currtime > prevMillis) // if timer has not run over max value
{
if (currtime - prevMillis >= 1000) // if the second has passed
{
sysTime++;
prevMillis = currtime;
}
}
else // if timer has rolled over
{
if ((0xFFFFFFFF - prevMillis + currtime) >= 1000)
{
sysTime++;
prevMillis = currtime;
}
}
"

edmundssprogis:
If someone is using time.cpp on Arduino Mega 2560 and concerns about board running longer than 49 days without restart,

No one should be concerned in the slightest, there is absolutely no issue running the Time library for arbitrarily long periods. I didn't read your code, but whatever it is, it's totally unnecessary.

PS: Unless you're planning to be around in 2038.

Hi!

I am trying to have certain events occur during the day, and certain during the night. I have got that figured out, but need some pointers on how to check on initial start up if it's day or night.
This is my current approach.

// Alarm times
//Day
int dhh = 6;
int dmm = 30;
int dss = 0;
time_t Day_time = AlarmHMS(dhh,dmm,dss);


// Night
int nhh = 18;
int nmm = 30;
int nss = 0;
time_t Night_time = AlarmHMS(nhh,nmm,nss);

I tried

 // Check if it's Day
  time_t check_time = AlarmHMS(hour(),minute(),second());
  Serial.println(check_time);
  if(hour()>=dhh && minute()<=Night_time)
  { 
    Day();
  }
  else
  {
    Night();
  }

But I think I am going wrong with my understanding of the AlarmHMS() function.
How should I go about this?

Just one comment, why are you not using the Time and TimeAlarms libraries?

If that was addressed to me, I am using the libraries to trigger the alarms once per day.

But on initial power up, I need to check based on the alarm times, if the state should be on or off.

But on initial power up, I need to check based on the alarm times, if the state should be on or off.

My crystal ball is saying line 27 of WeirdStuff.java needs to be fixed. I can't see how that could possibly be true, so you probably need to post your code.

Hahah! My bad!

Here is the setup() snippet.
I tried a new approach, but It's not perfect.

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

boolean daytime;

// RelayPins
int LightRelay_1 = 2;
int LightRelay_2 = 3;
int PumpRelay = 4;

// Alarm times
tmElements_t dtm,ntm;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16, 2);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");      

  // Set output mode
  pinMode(LightRelay_1, OUTPUT);   
  pinMode(LightRelay_2, OUTPUT);  
  pinMode(PumpRelay, OUTPUT);  
  //Day
  dtm.Hour = 6;
  dtm.Minute = 30;
  dtm.Second = 0;
  dtm.Day = day();
  dtm.Month = month();
  dtm.Year = year()-1970;
  time_t day_time = makeTime(dtm);
  Serial.println (day_time);

  // Night
  ntm.Hour = 18;
  ntm.Minute = 30;
  ntm.Second = 0;
  ntm.Day = day();
  ntm.Month = month();
  ntm.Year = year()-1970;
  time_t night_time = makeTime(ntm);
  Serial.println(night_time);


  // Set up Alarms
  Alarm.alarmRepeat(dtm.Hour,dtm.Minute,dtm.Second, Day);
  Alarm.alarmRepeat(ntm.Hour,ntm.Minute,ntm.Second, Night);

  // Check if it's Day
  time_t check_time = now();
  Serial.println(check_time);
  if(check_time>=day_time && check_time<=night_time)
  { 
    Day();
  }
  else
  {
    Night();
   }
}
void loop()
{//Display stuff removed
}
void Day()
{
 Serial.println("Day");
}
void Night()
{
 Serial.println("Night");
}

I tried a new approach, but It's not perfect.

The code does something. You have not said what it does.
You want it to do something. You have not said how that differs from what it actually does.

Your response wasn't perfect, either, and now you know why. To bad we still don't know what your problem is.

PaulS:

I tried a new approach, but It's not perfect.

The code does something. You have not said what it does.
You want it to do something. You have not said how that differs from what it actually does.

Your response wasn't perfect, either, and now you know why. To bad we still don't know what your problem is.

I am trying to have certain events trigger during the day, and certain events trigger during the night.

On initial stat up, I need to determine if it's night or day based on the current time.
ie. Suppose Day is set to start at 06:00 and night starts at 18:00.
On initial start up, based on the current time returned from the RTC, for example 13:00 then it should be day.

Initially I was trying to get the time_t based on the AlarmHMS() function for both the alarm times, and check it against the value of AlarmHMS() with the current hour,minute,second. But I think I wasn't implementing that correctly.

The second approach is to use maketime() to get the time elapsed for each of the alarms and check it against now(). It works for most of the part, but not if I swap the day - night alarm times.
ie. If Day is to start at 18:00 and night begins at 6:00, then since In makeTime() I use the current day() to determine the time from 1970.

In summery,
What would be good way to determine if the current time is in between the two set alarm times?
if dayalarm_time<current_time<nightalarm_time

Hope that makes more sense now?

Hello spyking,

If you want to know if the current time is day or night, you could do something like the following function. In this example, day starts at 6AM and ends at 6PM:

//returns true if it is day time now
boolean isDaytimeNow()
{
  // in this example, day starts at 6AM and ends at  6 PM
  int startDayHour = 6; // 6 am
  int endDayHour = 18; // 6PM
  
  int thisHour = hour();
  
  // the following determines if the current hour is within the day start and end times
  return thisHour >= startDayHour && thisHour < endDayHour;  
}

Calling that function will return true if its day and false if its night
Here is the code extended to support to start and end times of any hour and minute:

//returns true if it is day time now
boolean isDaytimeNow()
{
  // in this example, day starts at 6:30AM and ends at  6:30 PM
  int startDayHour = 6; // 6 am
  int startDayMinute = 30;
  int endDayHour = 18; // 6PM
  int endDayMinute = 30;
  
  int thisHour = hour();
  int thisMinute = minute();
  
  // the following determines if the current hour and minute times are within
  // the day start and end times
  return thisHour >= startDayHour &&
         thisMinute >= startDayMinute &&
         thisHour < endDayHour &&
         thisMinute < endDayMinute;  
}

I guess that what you are looking to do is to set the daytime flag at at startup and then toggle this off and on using the alarms, perhaps something like this:

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <TimeAlarms.h>

boolean daytime;

// Alarm times
tmElements_t dtm,ntm;

void setup()
{
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  //Day
  dtm.Hour = 6;
  dtm.Minute = 30;
  dtm.Second = 0;

  // Night
  ntm.Hour = 18;
  ntm.Minute = 30;
  ntm.Second = 0;

  // Set up Alarms
  Alarm.alarmRepeat(dtm.Hour,dtm.Minute,dtm.Second, Day);
  Alarm.alarmRepeat(ntm.Hour,ntm.Minute,ntm.Second, Night); 
}

void loop()
{
}

void Day()
{
  Serial.println("Day");
  daytime = true;
}

void Night()
{
  Serial.println("Night");
  daytime = false;
}

//returns true if it is day time now
boolean isDaytimeNow()
{  
  int thisHour = hour();
  int thisMinute = minute();
  
  // the following determines if the current hour and minute times are within
  // the day start and end times
  return thisHour >=  dtm.Hour &&
         thisMinute >= dtm.Minute &&
         thisHour < ntm.Hour &&
         thisMinute < ntm.Minute;  
}

mem:
Calling that function will return true if its day and false if its night
Here is the code extended to support to start and end times of any hour and minute:

//returns true if it is day time now

boolean isDaytimeNow()
{
  // in this example, day starts at 6:30AM and ends at  6:30 PM
  int startDayHour = 6; // 6 am
  int startDayMinute = 30;
  int endDayHour = 18; // 6PM
  int endDayMinute = 30;
 
  int thisHour = hour();
  int thisMinute = minute();
 
  // the following determines if the current hour and minute times are within
  // the day start and end times
  return thisHour >= startDayHour &&
         thisMinute >= startDayMinute &&
         thisHour < endDayHour &&
         thisMinute < endDayMinute; 
}

This is how I initially started out, but what would happen if the the current hour is lesser than the day start, but the minute isn't?
ie.
day start hour = 6
day start minute = 30
current time = 12:20

Good point!

try something like this:

boolean isDaytimeNow()
{
  // in this example, day starts at 6:30AM and ends at  6:30 PM
  int startDayHour = 6; // 6 am
  int startDayMinute = 30;
  int endDayHour = 18; // 6PM
  int endDayMinute = 30;

  int thisHour = hour();
  int thisMinute = minute();

  boolean result = false;

  // the following determines if the current hour and minute times are within
  // the day start and end times
  if( thisHour >= startDayHour) {
    if( thisHour == startDayHour){
      result = thisMinute >=  startDayMinute;
    }
    else{
      if( (thisHour < endDayHour) || (thisHour == endDayHour && thisMinute < endDayMinute) ){
        result = true;
      }
    }                 
  }
  return result;
}

like my previous post, this is untested but hopefully will get you going in the right direction

Edit: changed test for minutes from:
thisMinute > startDayMinute
to
thisMinute >= startDayMinute; // test for greater than or equal to

here is another approach to determining if its day or night:

time_t startOfDay;
time_t startOfNight;

void setup()
{
  startOfDay = timeToSeconds(6,30,0); // Hour,Min,Secs for start of day
  startOfNight = timeToSeconds(18,30,0); // Hour,Min,Secs for start of Night
}

boolean isDaytimeNow()
{
    time_t t = timeToSeconds(hour(), minute(), second());
    return (t >= startOfDay && t < startOfNight);     
}

time_t timeToSeconds(time_t H, time_t M, time_t S)
{
 return (H * SECS_PER_HOUR) + (M * SECS_PER_MIN) + S;
}

mem:
Good point!

try something like this:

boolean isDaytimeNow()

{
  // in this example, day starts at 6:30AM and ends at  6:30 PM
  int startDayHour = 6; // 6 am
  int startDayMinute = 30;
  int endDayHour = 18; // 6PM
  int endDayMinute = 30;

int thisHour = hour();
  int thisMinute = minute();

boolean result = false;

// the following determines if the current hour and minute times are within
  // the day start and end times
  if( thisHour >= startDayHour) {
    if( thisHour == startDayHour){
      result = thisMinute >  startDayMinute;
    }
    else{
      if( (thisHour < endDayHour) || (thisHour == endDayHour && thisMinute < endDayMinute) ){
        result = true;
      }
    }                 
  }
  return result;
}




like my previous post, this is untested but hopefully will get you going in the right direction

Neat! Will test it out when I get home.

Note that I updated to posted code to so the minute tests for greater than or equal to the day start minute.

I finally managed to get the TimerAlarm work for me.

Here is the code snippet from my setup function:
Alarm.alarmRepeat (13,31,0, OneThirtyPmEveryDay);
Alarm.timerRepeat (2, RepeatEveryTwoSeconds);

I have currently hard-coded the alarm Time. I would like to provide an LCD interface that will allow the user to change the time.
My concern is every time the user sets a different time, a call to alarmRepeat will create a new alarm. How can I reuse the existing alarm and update the changed time alone. I didn't see any function that allows me to change the time.
The option is to disable the Alarm on the current time, and create another Alarm but I'm afraid that I will needlessly waste memory as the Alarm on the old time is no longer useful but is sitting there occupying memory.
Can you help?

Look at the alarmRepeat() and timerRepeat() methods. They return values that you might not want to discard.

Hi,
Thanks for the nice library! I made some changes to it (the callback parameter), so keep that in mind when reading this---I may have broken something there.

I'm having trouble with rewriting my alarms. I'm switching on 12 lamps with separate timers. Here's a snippet of my code (inside loop()):

} else if (command == MESSAGE_TIMER_ON) {
      int floors=message[0];
      int lamps=message[1];
      int h=message[2];
      int mi=message[3];
      int sec=message[4];
      char msg;
      if (tons[floors][lamps]==0) {
        theOnTimers[floors][lamps]=Alarm.alarmRepeat(h,mi,sec,&alarm_cb,&theOnAlarms[floors][lamps]);
        tons[floors][lamps]=1;
        msg=0x03;
        writeMessage(MESSAGE_OK,&msg,1);
      } else {
        Alarm.write(theOnTimers[floors][lamps],AlarmHMS(h,mi,sec));
        msg=0x04;
        writeMessage(MESSAGE_OK,&msg,1);
      }
    }

The first time my program reaches this point (for each lamp), everything works nicely: Alarm.alarmRepeat is used and timer works as intended (light switches on). The following times the correct code block gets called (determined from the response message 0x04), but the timer does not turn the light on.

Am I missing something obvious? Should I re-enable the alarm after Alarm.write? I'll go re-check my changes to the library if there's nothing obviously wrong in this block of my code.

Thanks for any comments,
Tevko

edit:
The id variable is set up as

AlarmID_t theOnTimers[3][4];

And is not tampered with anywhere else.

tevko:
I'm having trouble with rewriting my alarms. I'm switching on 12 lamps with separate timers ...

One thing you can try is to replace the Alarm.write with a call to Alarm.free with the ID you want to change and then call Alarm.create with the new time. Alarm.write sets the alarm time and activates the alarm but it does not update the scheduler.

Otherwise, there is a lot going on there and It’s difficult to see exactly what is happening with the alarms. Can you create a simple test sketch that just handles the alarm rescheduling and see if that works as expected.