Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RTC clock timer problem

johnny78

Full Member level 3
Full Member level 3
Joined
Jun 28, 2017
Messages
174
Helped
0
Reputation
0
Reaction score
3
Trophy points
18
Activity points
1,288
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

thanks in advance
Johnny
 

FvM

Super Moderator
Staff member
Advanced Member level 7
Joined
Jan 22, 2008
Messages
51,002
Helped
14,633
Reputation
29,542
Reaction score
13,742
Trophy points
1,393
Location
Bochum, Germany
Activity points
291,790
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

Full Member level 3
Full Member level 3
Joined
Jun 28, 2017
Messages
174
Helped
0
Reputation
0
Reaction score
3
Trophy points
18
Activity points
1,288
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;
        }
      }
    }
  }
 

KlausST

Super Moderator
Staff member
Advanced Member level 7
Joined
Apr 17, 2014
Messages
23,279
Helped
4,742
Reputation
9,505
Reaction score
5,129
Trophy points
1,393
Activity points
154,222
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.
...what additional information he/she needs.

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
 

betwixt

Super Moderator
Staff member
Advanced Member level 7
Joined
Jul 4, 2009
Messages
15,799
Helped
5,088
Reputation
10,201
Reaction score
4,951
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
133,788
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
Advanced Member level 7
Joined
Jan 22, 2008
Messages
51,002
Helped
14,633
Reputation
29,542
Reaction score
13,742
Trophy points
1,393
Location
Bochum, Germany
Activity points
291,790
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
Full Member level 3
Joined
Jun 28, 2017
Messages
174
Helped
0
Reputation
0
Reaction score
3
Trophy points
18
Activity points
1,288
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

thanks in advance
--- 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.
...what additional information he/she needs.

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
Advanced Member level 7
Joined
Apr 17, 2014
Messages
23,279
Helped
4,742
Reputation
9,505
Reaction score
5,129
Trophy points
1,393
Activity points
154,222
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
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top