Pages: [1] 2   Go Down
Author Topic: Time comparision  (Read 1193 times)
0 Members and 1 Guest are viewing this topic.
Maastricht, The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Techno!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hello people!!

For a project I need to know the time in Arduino.
Therefore I've used a DS1306 RealTimeClock.

Everything is working okay, I can see time, date, etc... on my Arduino.

the (integer) variable HOUR keeps the hour of the day (0 to 23)
the (integer) variable MINUTE keeps the minutes (0 to 59)
the (integer) variable SECONDS keeps the seconds (0 to 59)

So for example:    (15:55:11)

HOUR = 15
MINUTE = 55
SECONDS = 11

Now I want to turn a LED on after 15 minutes.
How do I have to program this? ( I have to compare time, but how? )

Thanks in advance smiley-cool

Greetings from the Netherlands,

Atmoz
Logged

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

Quote
How do I have to program this?
We'd really have to see the code you have now.

If you have three int variables, onHour, onMin, onSec, what contain the time that the LED was turned on, you can determine when to turn it off, by adding to those variables. If the off time is 15 minutes after the on time, add 15 to onMin to get the off time. Of course, you'll need to make sure that offMin doesn't exceed 60. If it does, subtract 60, and add 1 to offHour. Of course, then you'll need to make sure that offHour remains reasonable.
Logged

Maastricht, The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Techno!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hi PaulS,

Thanks for the fast reply!!

That is the problem indeed. I have to "manually" check if it's not exceeding 59...
I was hoping that there was a "smart" or easy way to "compare" time and/or date, just like in Visual Basic or so smiley

Unfortunately I don't have any code regarding this. The only code I have is the one that nicely fills the 3 INT's (HOUR, MINUTES, SECONDS)

The "comparison" code isn't there. That's the number 1 reason from this topic.

Thanks again,

Atmoz
Logged

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

Code:
int onHour = 15;
int onMin = 55;
int onSec = 11;

int offHour = onHour;
int offMin = onMin + 15;
int offSec = onSec;

if(offMin > 59)
{
   offMin -= 60;
   offHour++;
}

if(offHour > 23) // Or 11, if using 12 hour mode
  offHour -= 24; // Or 12...

Nothing automatic, but not all that difficult, either. Now, if you want to make the LED stay on for 1 hour, 13 minutes, and 27 seconds, it becomes a little more complicated, but only marginally so.
Logged

Maastricht, The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Techno!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thanks again PaulS smiley-cool smiley-cool

That looks very nice!!

Why does it become a little more complicated if I want to turn on the LED 1:13:27 on?
Only because you have to "test" also the seconds, right? Or do I miss something important?

In the meanwhile I figured something else:

13:55 hour
if you want to add 15 minutes you do:

13x60 (to make the hours also minutes) then you add the minutes you already have AND the 15 minutes you want to add
At the end you got ((13x60 )+ 55 + 15)  = 850

850 / 60 = 14,166666667

14 are the hours
0,166666667 x 60 are the minutes (14:10 hours)

But then I have to figure out how I can fill variable HOUR with all the things before the comma, and variable MINUTES with al the thing after the comma... (and I don't know how to do this)

So your idea is better smiley

Thanks!!


Logged

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

Quote
Only because you have to "test" also the seconds, right? Or do I miss something important?
You need to increment three variables, and perform three tests - seconds, then minutes, then hours. So, more complex, but, as I said, only marginally so.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18816
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Now I want to turn a LED on after 15 minutes.
How do I have to program this? ( I have to compare time, but how? )

You don't need to know the time-of-day to turn on an LED after 15 minutes, any more than you need to know the time-of-day to time a 3-minute egg.

You can use the millis () function to find when 15 minutes are up.
Logged


Maastricht, The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Techno!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Only because you have to "test" also the seconds, right? Or do I miss something important?
You need to increment three variables, and perform three tests - seconds, then minutes, then hours. So, more complex, but, as I said, only marginally so.

Okay! Got it. Thanks!!


You don't need to know the time-of-day to turn on an LED after 15 minutes, any more than you need to know the time-of-day to time a 3-minute egg.

You can use the millis () function to find when 15 minutes are up.

Also a nice option indeed!!
But after +/- 50 days the millis counter goes back to 0, so in that case you also have to "test" the value to make a good timing to power on the LED.
It's a lot simpler and easier though! 

Thanks both smiley

Atmoz
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
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

But after +/- 50 days the millis counter goes back to 0, so in that case you also have to "test" the value to make a good timing to power on the LED.

Actually not!

One more alternative is the Time library, the time_t data type is seconds since 01Jan1970.
Logged

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

Maastricht, The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Techno!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But after +/- 50 days the millis counter goes back to 0, so in that case you also have to "test" the value to make a good timing to power on the LED.

Actually not!


How do you mean?
From: http://arduino.cc/en/Reference/Millis

Quote
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.


Quote
One more alternative is the Time library, the time_t data type is seconds since 01Jan1970.

Thanks!! Also a good one.
That must be a large number in these days smiley-mr-green

Now I'm stuck again:

How can I "test" a number let's say 11 if it's at least 2 digits/counts away from another number?

So if I have a = 11

and b = 14 OR for example b = 10

how can I "check" if a is > b+2 OR a < b-2

At the moment I use

Code:
if (SprMinute< minuten +2 || SprMinute> minuten -2){

but this doesn't seem to work.
This is also for "checking" the time...

Thanks!!

Atmoz

[edit]

I found it. I had to swap + and - smiley
« Last Edit: September 15, 2011, 09:10:57 am by Atmoz » Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



850 / 60 = 14,166666667

14 are the hours
0,166666667 x 60 are the minutes (14:10 hours)

But then I have to figure out how I can fill variable HOUR with all the things before the comma, and variable MINUTES with al the thing after the comma... (and I don't know how to do this)

So your idea is better smiley

Thanks!!




Look at the modulous "%" operator.  It will do exactly what you want.

850 / 60 = 14 hours (using ints no decimals rounds down)

850 % 60 = 10 minutes
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4086
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

But after +/- 50 days the millis counter goes back to 0, so in that case you also have to "test" the value to make a good timing to power on the LED.

Actually not!


How do you mean?
From: http://arduino.cc/en/Reference/Millis

Quote
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

Of course it does overflow, but because it's an unsigned (long) integer any two values can be subtracted and will give the right answer, as long as you're not trying to time a delta of more than ~50 days.

Quote
One more alternative is the Time library, the time_t data type is seconds since 01Jan1970.
Quote

Thanks!! Also a good one.
That must be a large number in these days smiley-mr-green

Yep, about 1316107515 right now ... see http://www.unixtimestamp.com/. There are parties when it hits a notable value, like 1234567890.

Quote
Now I'm stuck again:

How can I "test" a number let's say 11 if it's at least 2 digits/counts away from another number?

So if I have a = 11

and b = 14 OR for example b = 10

how can I "check" if a is > b+2 OR a < b-2

At the moment I use

Code:
if (SprMinute< minuten +2 || SprMinute> minuten -2){

but this doesn't seem to work.
This is also for "checking" the time...

Thanks!!

Atmoz

[edit]

I found it. I had to swap + and - smiley

Or use the abs() function.
Logged

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

Maastricht, The Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
Techno!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Of course it does overflow, but because it's an unsigned (long) integer any two values can be subtracted and will give the right answer, as long as you're not trying to time a delta of more than ~50 days.

Thanks for the explanation!!

Quote
Yep, about 1316107515 right now ... see http://www.unixtimestamp.com/. There are parties when it hits a notable value, like 1234567890.

Quote
Or use the abs() function.

Also thanks for that one.
I had looked at this before, but didn't directly understand how it works.
I'll have a look at it tomorrow!!


@Delta_G

THANKS!

That I was searching.
very very long ago I already uses this, but I was completely forgotten it.

Everything is working how it should be.

Very nice smiley-cool Thanks smiley-grin

Atmoz
Logged

Tucson, AZ
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone have advice for a working clock?  I have an RTC module, but I don't know how to program it to remember time.
Logged

Offline Offline
God Member
*****
Karma: 4
Posts: 813
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The RTC module needs a battery to "remember" time. Once you insert the battery, it will remember time (and keep it ticking) even when turned off. Just make sure you don't re-write a bad time in your setup() function!

Also, I've found that, when dealing with timed events, it's better to compare seconds intervals than specific times. So, for example, if you want to turn something on 15 minutes after something else, then calculate the seconds (epoch) value for the first time, add 15*60, and then when the seconds (epoch) value goes past that value, you turn on the thing. You can check this each time the timer changes, and there's no chance you will miss it -- even if the chip is turned off, and then turned on in the middle of the timer interval, the check will then see "oh, it's greater than start time -- turn on!"
Logged

Pages: [1] 2   Go Up
Jump to: