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

Status
Not open for further replies.

johnny78

Full Member level 5
Full Member level 5
Joined
Jun 28, 2017
Messages
264
Helped
1
Reputation
2
Reaction score
5
Trophy points
1,298
Activity points
3,421
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
 

Hi,

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

Klaus
 

    johnny78

    Points: 2
    Helpful Answer Positive Rating
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
    Helpful Answer Positive Rating
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;
        }
      }
    }
  }
 

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
 

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
 

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.
 

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

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
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top