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? )
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.
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
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.
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.
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)
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.
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!!
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!
Atmoz:
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.
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.
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
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
if (SprMinute< minuten +2 || SprMinute> minuten -2){
but this doesn't seem to work.
This is also for "checking" the time...
Atmoz:
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.
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]
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.
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
Yep, about 1316107515 right now ... see http://www.unixtimestamp.com/. There are parties when it hits a notable value, like 1234567890.
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
if (SprMinute< minuten +2 || SprMinute> minuten -2){
but this doesn't seem to work.
This is also for "checking" the time...
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!"
At http://fjkraan.home.xs4all.nl/digaud/arduino/index.html is an example of a timer clock based on the DS1307 switching a relay on time and light conditions. For each time comparison I convert the time from the RTC to seconds (on this day).
The code structure could be better, but I ran into RAM limitations, so I left it as is. It is good enough for me