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
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;
}
}
}
}
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)
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.
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-
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)
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
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.