Time and TimeAlarms Libraries – Ask here for help or suggestions

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.

Thanks for the response mem!

mem:
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.

That's a good idea, I'll try with Alarm.free, or I'll first try to call Alarm.enable to see if it helps.

mem:
Can you create a simple test sketch that just handles the alarm rescheduling and see if that works as expected.

I'll do that if I can't find a solution to this problem with Alarm.free.

edit:
Alarm.free+Alarm.create did just what I wanted, thanks your help! I'll see if I can find out why Alarm.write was not enough for this.

Hello Mem,

I am building a Hour Counter for my Air Compressor which works eventually. I need to keep a track on the running time in order to do a proper service.
I have the RTC module and it works fine with Time.h. I have developed the program upto some level which gives the elapsed time after 5 sec delay (That is 5 Sec) . but when I tried to do the same subtraction with a if condition corresponding to a button switch it gives 0 duration.

Can you please help me to over come this.

 /*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * 
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int se;
int minu;
int hr;
const int buttonPin = 8;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status


void setup()  {
  Serial.begin(9600);
  lcd.begin(20, 4);
    // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     lcd.println("Unable to");
  else
     lcd.println("RTC has set");      
}

void loop()
{
  int timse1;
  int timse2;
  int durse;
  int timmin1;
  int timmin2;
  int durmin;
  int timhr1;
  int timhr2;
  int durhr;
  
  int timsev1;
  int timsev2;
  int timminv1;
  int timminv2;
  int timhrv1;
  int timhrv2;

    buttonState = digitalRead(buttonPin);
   //setTime(12,44,0,3,6,14); 
  digitalClockDisplay();  
   delay(1000);
   lcd.clear();
 
 // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
    
  timsev1= getse();
  timminv1= getminu();
  timhrv1= gethr();
  lcd.print("ON");
  
//  
//  if (buttonState == LOW){
//  
//  
//   digitalWrite(ledPin, LOW); 
//    timsev2 = getse();
//    timminv2 = getminu();
//    timhrv2 = gethr();
//    lcd.print("OFF");
//  
//  }
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    timsev2 = getse();
    timminv2 = getminu();
    timhrv2 = gethr();
    lcd.print("OFF");
  }

  
  lcd.print(timse1);

durse = timse2-timse1;
durmin = timmin2-timmin1;
durhr = timhr2-timhr1;

lcd.setCursor(1, 2);
lcd.print(durhr);
lcd.print(':');
lcd.print(durmin);
lcd.print(':');
lcd.print(durse);

delay(1000);

}

int getse(){
    time_t t= now();
  se = second(t);
  
  return se;
}

int getminu(){
    time_t t= now();
  minu = minute(t);
 
  return minu;
  
}

int gethr(){
  time_t t= now();
  hr = hour(t);
  return hr;  
}


void digitalClockDisplay(){
  // digital clock display of the time
  lcd.print(hour());
  printDigits(minute());
  printDigits(second());
  
  /*Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); */
}


/*int duration (int ms, int mp){
  //function to find the varience b/w start time n stop time
  int hd;
  int md;
  
 // hd= hp-hs;
  md= mp-ms;
  
  //lcd.print(hd);
  printDigits(md);
}
*/


  



void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  lcd.print(":");
  if(digits < 10)
    lcd.print('0');
  lcd.print(digits);
}

Hi Samly_dixon, Its not clear on what you want to do and I did not see the code that you are trying to get working. Can you say more about exactly what you want to test with the if statement when a button is pressed.
I noticed that you have created functions to return the current hours, minutes and seconds. These functions already exist as follows: seconds(), minutes() , hours().
You have a function named duration that has the following comment:
//function to find the variance b/w start time n stop time
But it is not clear which argument is the start time and which is the stop time. It would be clearer if you used descriptive names for your variables instead of: ms, mp,hd, md etc.

To find the difference between two times, use the time_t returned from the now()function. For example:

time_t event = now();
// … some time later
long duration = now() – event; // the duration since the event in seconds

Good day all.

Please pardon me with this noob question/request.

I am trying this Time libraries with the samples but I'm always getting the 'time_t' does not name a type and 'set_Time' was not declared in this scope errors while compiling. Please note that I am not much into programming, however, if given a flawless sample, I think I can work through the codes and modify them to achieve what I need.

Here's my task.

Similar to the Alarm sample, I wanted to program my arduino micro paired with a 4-channel relay module to switch 4 different lights, the television and the radio at specific times of the day but differs on each day of the week. I had a previous project that I might be able to use for the television and radio On-Off by Infrared remote control. So all I need is to make the time and alarm work without any other hardware attached.

Could anyone please point me to the right direction as I have around 10 days to complete this project. I am bound to go for a vacation and hoping that with this project, I can keep my house safe from breaking and entering which happened last year.

Providing me with a code to start with will be even better.

Your help will be highly appreciated.

More power to you all.

Providing me with a code to start with will be even better.

You clearly already have code that you haven't posted.

You also haven't mentioned which version of the IDE you are using, or which version of the Time library (as in where you got it).