Continue to Site

# RTC clock timer problem

#### johnny78

##### Full Member level 3
hi Guys

im working on a timer project using Arduino & ds1307RTC 24h mode
my code is working great for starting & stopping the desired output
BUT my brain stopped working when i thought of the hour 23:59

Code:
  if ((out1TimeControl) == 0 && (settingsDisplay1) == 0) {
if ((tm.Hour) > out1OnTimeSetHours) {
if ((tm.Hour) <= out1OffTimeSetHours) {
out1TimeControl = 1;
}
}
if ((tm.Hour) >= out1OnTimeSetHours && (tm.Minute) >= out1OnTimeSetMinutes) {
if ((tm.Hour) <= out1OffTimeSetHours) {
if ((tm.Minute) <= out1OffTimeSetMinutes) {
out1TimeControl = 1;
}
}
}
}
if ((out1TimeControl) == 1 && (settingsDisplay1) == 0) {
if ((tm.Hour) >= out1OffTimeSetHours) {
if ((tm.Minute) >= out1OffTimeSetMinutes) {
out1TimeControl = 0;
}
}
}
any idea to correct my code?
its ok for time between 00:00am - 23:59pm
but how to solve it for the time after 23:59

Johnny

#### KlausST

##### Super Moderator
Staff member
Hi,

I don't see a problem description not a clear question.

Klaus

### johnny78

Points: 2

#### FvM

##### Super Moderator
Staff member
Suggest to sketch the problem with pencil and paper before writing code.

You're apparently trying to make a 24 h switch clock. You'll see that for t_off < t_on the switching action has to be inverted.

In the regular case t_off > t_on, output is on for t_on > t > t_off, in the inverted case, it's off for t_off > t > t_on.

### johnny78

Points: 2

#### johnny78

##### Full Member level 3
hi Guys
my code is working BUT
lets say i set the on time to 22:00 pm & i turned on the MCU at 5am
in this case it will not see that the actual time is bigger or equal to out2OnTimeSetHours

Code:
    if ((tm.Hour) >= out2OnTimeSetHours && (tm.Minute) >= out2OnTimeSetMinutes) {
if ((tm.Hour) <= out2OffTimeSetHours) {
if ((tm.Minute) <= out2OffTimeSetMinutes) {
out2TimeControl = 1;
}
}
}
}

#### FvM

##### Super Moderator
Staff member
I would expect a comparison of t_on and t_off, see above.

#### johnny78

##### Full Member level 3
I would expect a comparison of t_on and t_off, see above.
Hi

would you please explain what you mean?

thanks
--- Updated ---

maybe i need to make a AM-PM variable i guess

#### KlausST

##### Super Moderator
Staff member
Hi,

AM PM does not play a role. You could easily see when you did what FvM recommended: use paper and pencil

You could even "play this game" with your partner. Give him/her just the same informations you give the MC...and see how he/she reacts.

You could also extend the game:
* here in Europe we have daylight saving ..... so there's a day in year with only 23 hours and one with 25 hours.
* or say your "ON" is in november and "OFF" is in april (like I do with central heating)

Klaus

#### FvM

##### Super Moderator
Staff member
The complete explanation is in post #3.

#### betwixt

##### Super Moderator
Staff member
An alternative approach is to convert all the times to minutes (or whatever smallest units you want to use). It breaks the problem into two parts, first to do the conversion so you always have an integer number to work with and secondly to compare the integers. It also makes things like daylight savings much easier because all you have to do is add an hours worth of minutes when needed.

DayMinutes = (clock hours * 60) + clock minutes. It always fits in an integer variable.

Brian.

#### FvM

##### Super Moderator
Staff member
Working with integer minutes is also my preferred way to implement timer calculations. I presume the tm.xx values used above are BCD values (native RTC coding), so it involves additional conversions. For a simple switch clock it's probably not necessary. In any case it doesn't solve the problem discussed above, just simplifies comparison-

#### johnny78

##### Full Member level 3
Suggest to sketch the problem with pencil and paper before writing code.

You're apparently trying to make a 24 h switch clock. You'll see that for t_off < t_on the switching action has to be inverted.

In the regular case t_off > t_on, output is on for t_on > t > t_off, in the inverted case, it's off for t_off > t > t_on.
hi
im trying to understand your idea BUT would you please explain this for me using my variables ?

tm.Hour = RTC hour
tm.minute = RTC minute
out1OnTimeSetHours = start set hour
out1OnTimeSetMinutes = start set minute
out1OffTimeSetHours = stop set hour
out1OffTimeSetMinutes = stop set minute

--- Updated ---

Hi,

AM PM does not play a role. You could easily see when you did what FvM recommended: use paper and pencil

You could even "play this game" with your partner. Give him/her just the same informations you give the MC...and see how he/she reacts.

You could also extend the game:
* here in Europe we have daylight saving ..... so there's a day in year with only 23 hours and one with 25 hours.
* or say your "ON" is in november and "OFF" is in april (like I do with central heating)

Klaus
hi

actually when it comes to coding you guys on this Forum are my friends
so thats why i ask you.
everyone has his own way of thinking & maybe i'm stuck with the way i think about this problem.
its more than 3 hours im running in the same circle talking to my wall clock & drawing on my paper

anyway i think i should drink something to wake me up

Last edited:

#### KlausST

##### Super Moderator
Staff member
Hi,

Is it really that difficult?

If you build a 24 hour alarm clock .. on startup you need to check the previous 24 hours of alarm time
If you build a week time alarm clock .. on startup you need to check the previous week of alarm time
If you build a year alarm clock .. on startup you need to check the previous year of alarm time
And so on.

Klaus