Go Down

Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions (Read 78 times) previous topic - next topic

PaulS

Quote
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.

edmundssprogis

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;
      }
   }
"


Jack Christensen

#212
Feb 04, 2014, 12:16 am Last Edit: Feb 04, 2014, 12:19 am by Jack Christensen Reason: 1

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.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

spyking

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.

Code: [Select]

// 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
Code: [Select]

// 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?








LarryD

Just one comment, why are you not using the Time and TimeAlarms libraries?
The way you have it in your schematic isn't the same as how you have it wired up!

spyking

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.


PaulS

Quote
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.

spyking

Hahah! My bad!

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

#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");
}

PaulS

Quote
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.

spyking


Quote
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?




mem

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:
Code: [Select]
//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:
Code: [Select]
//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:
Code: [Select]
#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; 
}


spyking



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:
Code: [Select]
//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

mem

Good point!

try something like this:
Code: [Select]
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

mem

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

Code: [Select]
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;
}

spyking


Good point!

try something like this:
Code: [Select]
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.

Go Up