Pages: 1 ... 13 14 [15] 16 17 18   Go Down
Author Topic: Time and TimeAlarms Libraries – Ask here for help or suggestions  (Read 71709 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 03, 2014, 06:19:51 pm by Jack Christensen » Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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







Logged

Offline Offline
Faraday Member
**
Karma: 67
Posts: 2691
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

The way you have it in your schematic isn't the same as how you have it wired up!

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hahah! My bad!

Here is the setup() snippet.
I tried a new approach, but It's not perfect.
Code:
#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");
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?



Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//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:
//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:
#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; 
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
//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
 
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good point!

try something like this:
Code:
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
« Last Edit: February 16, 2014, 08:11:18 am by mem » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good point!

try something like this:
Code:
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.
Logged

Pages: 1 ... 13 14 [15] 16 17 18   Go Up
Jump to: