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.

ds1307 timer problem

johnny78

Full Member level 5
Full Member level 5
Joined
Jun 28, 2017
Messages
270
Helped
1
Reputation
2
Reaction score
5
Trophy points
1,298
Activity points
3,461
hi Guys

im using arduino uno with ds1307 as a timer to turn on and off outputs

here is the code to get the time from the RTC

Code:
  tmElements_t tm;
  strZero = ("0");
  if (RTC.read(tm)) {
    strHours = tm.Hour;
    strMinutes = tm.Minute;
    if ((tm.Minute) < 10) {
      strMinutes = strZero + tm.Minute;
    }
    if ((tm.Hour) < 10) {
      strHours = strZero + tm.Hour;/////////// new to fix the time display after 00:59
    }
    strSeperator = (":");
    strTime = strHours + strSeperator + strMinutes;

here is the code to turn on & off at specified time

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) {
        out1TimeControl = 1;
      }
    }
    }

    if ((out1TimeControl) == 1 && (settingsDisplay1) == 0) {
    if ((tm.Hour) >= out1OffTimeSetHours) {
      if ((tm.Minute) >= out1OffTimeSetMinutes) {
        out1TimeControl = 0;
      }
    }
    }

the code is working but if i set on time (for example) to 23:00 & off time to 02:00 its not working
any idea to calculate the stop time will be great

thanks
Johnny
--- Updated ---

hi Guys

im using arduino uno with ds1307 as a timer to turn on and off outputs

here is the code to get the time from the RTC

Code:
  tmElements_t tm;
  strZero = ("0");
  if (RTC.read(tm)) {
    strHours = tm.Hour;
    strMinutes = tm.Minute;
    if ((tm.Minute) < 10) {
      strMinutes = strZero + tm.Minute;
    }
    if ((tm.Hour) < 10) {
      strHours = strZero + tm.Hour;/////////// new to fix the time display after 00:59
    }
    strSeperator = (":");
    strTime = strHours + strSeperator + strMinutes;

here is the code to turn on & off at specified time

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) {
        out1TimeControl = 1;
      }
    }
    }

    if ((out1TimeControl) == 1 && (settingsDisplay1) == 0) {
    if ((tm.Hour) >= out1OffTimeSetHours) {
      if ((tm.Minute) >= out1OffTimeSetMinutes) {
        out1TimeControl = 0;
      }
    }
    }

the code is working but if i set on time (for example) to 23:00 & off time to 02:00 its not working
any idea to calculate the stop time will be great

thanks
Johnny
i was thinking of calculating the hours & minutes difference between On & Off time
any better idea ?
 
Last edited:
Hi,

I´d
* use a software clock ... and every minute at second 00 update the outputs.
* Update the software clock from DS1307 once at start up and then maybe once a day at midnight.

***
But indeed building a alarm clock with DS1307 involved is done many thousands times before.
Thus there is code available ... for almost any clock feature ...
Documents, web pages, code examples, explanations ... even videos

Klaus
 
sorry for late reply & thank you guys

actually i was thinking of this issue long time ago & i've posted here about it before
now i have time to fix it & i need this fix
I suggest to visualize timer operation with pencil and paper to understand why it's not working in all cases.
that was the first suggestion i had to fix this
Hi,

I´d
* use a software clock ... and every minute at second 00 update the outputs.
* Update the software clock from DS1307 once at start up and then maybe once a day at midnight.
thats what im working on now
***
But indeed building a alarm clock with DS1307 involved is done many thousands times before.
Thus there is code available ... for almost any clock feature ...
Documents, web pages, code examples, explanations ... even videos
i have searched alot of videos or examples but i couldnt see something related to my issue
if you have an example for Arduino IDE it will b great

thanks
Johnny
 
maybe because the other codes are written correctly ... and thus don´t suffer from your issue at all.

Klaus
hi Klaus

isnt it better if you give me an example of correct way for this timer?
anyway i just woke up & you made me laugh like crazy & i feel ready to write new code

thanks
Johnny
 
Hi,
isnt it better if you give me an example of correct way for this timer?
Yes, I also think it would be better when someone else does my job for free.

I am happy to help you .. but you do the main job.
Internet search, running through documentation, doing tests ... for sure I could do this for you ... but you can do it, too.
You have the same internet, the same search tools ... and your time is not worth more than my time.

In case of a problem I will be here to help you to rectify it.

Klaus
 
Hi,

Yes, I also think it would be better when someone else does my job for free.

I am happy to help you .. but you do the main job.
Internet search, running through documentation, doing tests ... for sure I could do this for you ... but you can do it, too.
You have the same internet, the same search tools ... and your time is not worth more than my time.

In case of a problem I will be here to help you to rectify it.

Klaus
im starting to count minutes as 24 hour means 1440 min's
so lets say 23:30 means 1200 min's so 240 mins to 00:00
& use something as elapsed minutes to stop
i will post my results
if im thinking wrong way correct my way of thinking please

thanks
Johnny
 
Just a suggestion to make the math more understandable and convenient:

1. create a variable to hold up to 1440 (or 86400 is you want seconds accuracy).
2. read the time and store "(Hours * 60) + Minutes" in the variable. Or "(Hours * 3600) + (Minutes * 60) + Seconds".
3. Do the same with your required times.

Now all you have to do is a simple number comparison to match a time or find out if you are within a time range.
It also makes it easier to handle DST by adding or subtracting a fixed number of minutes or seconds and if you need to store the times in say EEPROM, all you do is save a number.

Brian.
 
Hi,

if im thinking wrong way correct my way of thinking please
I fully agree with FvM on the paper and pencil method. This helps to visualize and to "validate" the own thinking.

During the last decades I´ve automatically build some validation methods. I just show you how it works.
You are free to adopt it or not. It´s just an information how my brain works.

If you write:
"im starting to count minutes as 24 hour means 1440 min's"
and I read this ... I´m immediately validating this. First raw, like 60 x 20 = 1200 ... then add a bit .. so 1440 may be correct.

When you write this:
"so lets say 23:30 means 1200 min's so 240 mins to 00:00"
I also validate. First I see only the numbers. Here I see the odd order: [hh:mm], [mins], [mins], [hh:mm]
I´d rather had written down it it two lines like
"[24h] , [1440]
[23:30], [1200]
[(to 0:00 =) 0:30], [240]"
And here my brain automatically calls an alarm: 23:30 to 0:00 is just half an hour, so 30 minutes .... what has this to do with 240 minutes.

This all takes place in maybe one second. Because it´s automated .. I indeed can´t control this.

What I wanted to say: The first way to an easy validation is to write the numbers down in a good order.
This way you can validate them horizontally and also vertically.

***
I´d say there is a mistake. I can´t find out what´s the reason for this.
--> thus my hint for the future is: Use excel to wirte down your inputs in the mentioned order ... but let Excel do the math.

***
But indeed for the alarm clock problem .. I´m not sure about your approach.
* Sometimes you say timer .. it it sounds as you want a relative time to ON or OFF. ... so let´s say ON now (key press?) ... but OFF in 70 minutes.
* But sometimes it sounds if you want an alarm with absolute time to ON or OFF. ..like ON at 13:20 and OFF at 14:30.
Which function do you want?

***

Klaus
 
Hi,


I fully agree with FvM on the paper and pencil method. This helps to visualize and to "validate" the own thinking.
actually I've used this method & it drives my brain to test the minutes method
During the last decades I´ve automatically build some validation methods. I just show you how it works.
You are free to adopt it or not. It´s just an information how my brain works.

If you write:
"im starting to count minutes as 24 hour means 1440 min's"
and I read this ... I´m immediately validating this. First raw, like 60 x 20 = 1200 ... then add a bit .. so 1440 may be correct.

When you write this:
"so lets say 23:30 means 1200 min's so 240 mins to 00:00"
I also validate. First I see only the numbers. Here I see the odd order: [hh:mm], [mins], [mins], [hh:mm]
I´d rather had written down it it two lines like
"[24h] , [1440]
[23:30], [1200]
[(to 0:00 =) 0:30], [240]"
And here my brain automatically calls an alarm: 23:30 to 0:00 is just half an hour, so 30 minutes .... what has this to do with 240 minutes.
this was a mistake sorry
This all takes place in maybe one second. Because it´s automated .. I indeed can´t control this.

What I wanted to say: The first way to an easy validation is to write the numbers down in a good order.
This way you can validate them horizontally and also vertically.

***
I´d say there is a mistake. I can´t find out what´s the reason for this.
--> thus my hint for the future is: Use excel to wirte down your inputs in the mentioned order ... but let Excel do the math.
how to do this with excel ?
***
But indeed for the alarm clock problem .. I´m not sure about your approach.
* Sometimes you say timer .. it it sounds as you want a relative time to ON or OFF. ... so let´s say ON now (key press?) ... but OFF in 70 minutes.
* But sometimes it sounds if you want an alarm with absolute time to ON or OFF. ..like ON at 13:20 and OFF at 14:30.
Which function do you want?

***

Klaus
hi Klaus

yes I'm working on On & Off timer on specified times
i will continue work on it today & post the results

thanks
Johnny
 
yes I'm working on On & Off timer on specified times
i will continue work on it today & post the results
just say "absolute" or "relative"..

Excel has math functions an different time formats like hh:mm and it has a help to show you how to use it.

Klaus
 
just say "absolute" or "relative"..

Excel has math functions an different time formats like hh:mm and it has a help to show you how to use it.

Klaus
its absolute timer
actually i dont know what that means but i guess its an absolute

here is the code i have made few tests on different times & it works
so would you please check it once if you see a mistake & can make error on specified time

Code:
  clockCurrentMinutes = tm.Hour * 60 + tm.Minute;
  tmr1OnMinutes = out1OnTimeSetHours * 60  + out1OnTimeSetMinutes;
  tmr1OffMinutes = out1OffTimeSetHours * 60  + out1OffTimeSetMinutes;

  if ((out1TimeControl) == 0 && (settingsDisplay1) == 0) {
    if ((tmr1OnMinutes) < tmr1OffMinutes) {
      if ((tmr1OnMinutes) <= clockCurrentMinutes && tmr1OffMinutes > clockCurrentMinutes) {
        out1TimeControl = 1;
      }
    }
  }
  if ((out1TimeControl) == 1 && (settingsDisplay1) == 0) {
    if ((tmr1OnMinutes) < tmr1OffMinutes) {
      if ((tmr1OnMinutes) > clockCurrentMinutes || tmr1OffMinutes <= clockCurrentMinutes) {
        out1TimeControl = 0;
      }
    }
  }

  if ((out1TimeControl) == 0 && (settingsDisplay1) == 0) {
    if ((tmr1OnMinutes) > tmr1OffMinutes) {
      if ((clockCurrentMinutes) >= tmr1OnMinutes){
        out1TimeControl = 1;
      }
      else if ((clockCurrentMinutes) > 0 && clockCurrentMinutes < tmr1OffMinutes) {
        out1TimeControl = 1;
      }
    }
  }
 
  if ((out1TimeControl) == 1 && (settingsDisplay1) == 0) {
    if ((tmr1OnMinutes) > tmr1OffMinutes) {
      if ((clockCurrentMinutes) == tmr1OffMinutes) {///////////////////////
        out1TimeControl = 0;
      }
    }
  }

thanks
Johnny
 
its absolute timer
actually i dont know what that means but i guess its an absolute
You don´t know the difference between relative and absolute?
And you are not able to find a useful description in the internet?

In short:
* Relative: if you want to bake a cake. It needs to be in the oven for 30 minutes. This is a relative time. Relative with respect to "when you put the dough into the oven".
You could use a hour glas for it.
You don´t want to take out the cake every day at 3PM.

* Absolute: if you want to wake up in the morning (to go to work). You need to wake up at 6AM ... independent of when you went to bed.
You could use a sundial for it
You don´t want to sleep for "7 hours" ..no matter whether you go to bed at 10PM or 1AM

******

There are several problems to consider.
* Accuracy and precision:
To bake a cake (relative time) accuracy is not a big issue. If the 30 min interval is off by 1%, then it is just an error of 18 seconds. The cake won´t care. The error does not add up. In software you could use a delay_ms(30x60x1000)
An alarm clock (absolute time, to wake you up in the morning) needs a way higher accuracy. If it is OFF by 1% then in the 7 hours you sleep it is OFF by 4 minuts and 20s. You may say: not a big problem if I wake up at 6:04:20 AM. But this is only true for the first day, since the error adds up. So one day (24h) later the alarm goes at 6:18:44 .. and the next day: 6:32:08 ...and so on. Every day it adds more than 14 minutes of error.

And the problem when daylight saving begins / ends is the next issue. Normally when you go to bed at 11:00PM and want to wake up at 6:AM you have an interval of 7 hours. But not at two days in a year: then it´s an interval of 6hours and 8hours respectively.

* About delay_ms():
This is a relative time. It is very precise (unless interrupts cause errors), but not very accurate, since the overhead may not be constant. The bigger problem is the software "around" the delay() function.
like:
Code:
while(1) {
  delay_ms(100);
  do_something();
  }
Then the "while loop" and the "do_something routine" kills your accuracy. Both cause a (non constant) error, and this error adds up.
This means: if you run the loop 1000 times .. the total time is (much) more than 100.000 milliseconds.

***
If you want someone to check your code: (as always)
* add comments
* post complete code

Klaus
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top