Long Delays - Is there a valid way?


Closed Thread
Results 1 to 9 of 9
  1. #1
    rad's Avatar
    rad Guest

    Default Long Delays - Is there a valid way?

    If you want to have something occur every day at the same time, is there a reliable way to code in a very long delay?

    Say you want to turn on the pump once every 24 hours, or what ever. Besides using a variable and incrementing it after a small delay, is there anything more to be done here?

  2. #2
    Desterline's Avatar
    Desterline Guest


    Did you find this post helpful? Yes | No

    Default

    A large delay can be created by nesting several loops, like such:

    start:
    for hours = 0 to 23
    for minutes = 0 to 59
    for seconds = 0 to 59
    pause 1000
    next seconds
    next minutes
    next hours

    -your code here-

    goto start

    This will work, and be reliable, but it run a little slow. (I paused exactly a second, but didn't take into account the loop overhead)

    Other delay loops exist, many with better accuracy than you can get with this one. Any delay loop is subject to the accuracy of the oscilator for the PIC, as such you'll never get exactly 24.00000 hours delay. It'll run fast or slow and your event will migrate around the clock as time goes by.

    But I'm guessing that you actualy want the event to hapen at a set time each day. For that you need an RTC (real time clock). There are many options available. (check out the ones from Maxim, www.maxim-ic.com) Or if you have AC mains available you can set the PIC up to count the sine waves (they have excelent long-term timing accuracy, most plug-in clocks use this method).

    -Denny

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi rad,

      I just recently posted an Elapsed Timer Demo in the sample code forum http://www.picbasic.co.uk/forum/show...=&threadid=190

    This should make it really easy for you.

    Your whole program could look like this.
    Code:
        Include "Elapsed.pbp"
        Gosub ResetTime    ' Reset Time to  0d-00:00:00.00
        Gosub StartTimer   ' Start the Elapsed Timer
    
        Loop:
            If DaysChanged = 0 then Loop
            DaysChanged = 0
            ... Do the daily task here ...
        Goto Loop
    Can't get much easier than that.

    Like Desterline says, the accuracy depends on the crystal. But I've found that with an HC49U type crystal, it holds to around 3-4 seconds per day. Even the DS1302/1307 has a tolerance of about 4 seconds per day so this is pretty good.

    This doesn't have a battery backup, so if power is lost, the time will get reset. But then, there's a hardware solution for that too.

    HTH,
      Darrel

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Doing something every day depends on the criticality of your application. If it MUST happen exactly at the same time each day, then accurate crystals or RTC's are the way to go. If it's arbitrary (eg just running a waste pump that's clearing your sewage pit each day into your neighbours swimming pool) and it doesn't matter if it's plus or minus a few minutes, then a simple loop counter would be just fine.

    I use a 16F628 with internal oscillator in an application that times about fourteen months... my accuracy is about plus/minus six hours across that period.

  5. #5
    rad's Avatar
    rad Guest


    Did you find this post helpful? Yes | No

    Default

    Thanks all.

    This is turning on a filter flush valve once a day or once every few days.

    What I wanted was to do it at midnight, or say between midnight and 1am. That's as accurate as I need.

    I guess what I was really wondering was could this be done or jiggered to make work. And I wanted to make the worlds cheapest device too. So I intended to use the internal resonator.

    If there is an inaccuracy, will it be either gaining time or losing time? If this is the case, I can watch it, experiment, and put in fudge factor every three, or so many, loops to throw the thing back to the correct 60 minute window.

    Or do the inaccuracies lead to a device that gains and loses time?

    I can't imagine how the thing could be inaccurately slow for a while, then become inaccurately fast for a while. But I don't really know.

    It seems like a fudge factor would be a big help in adding to the accuracy or functionality in my case.

    I don't want to go all John Harrison-esque though !

    Thanks.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    If you need to cut something, and you have a Sharp knife in your hand. Why look for a Dull one to do the job?

    The Elapsed Timer Demo compiles to 149 words (without the daily task). So it easily fits into a 12F629 if you wanted. If you need the accuracy, use a crystal. If you don't, use the internal oscillator.

    After a year, it should only be off by about 24 minutes or less.

    I can't imagine how the thing could be inaccurately slow for a while, then become inaccurately fast for a while. But I don't really know.
    The oscillator will run faster or slower depending on the ambient temperature. So indoor applications can be more stable that outdoor ones.
    Last edited by Darrel Taylor; - 28th December 2003 at 19:27.

  7. #7
    Desterline's Avatar
    Desterline Guest


    Did you find this post helpful? Yes | No

    Default

    The PIC doesn't have an "internal resonator", it has an internal R-C oscilator (some require extrnal parts) an RC oscillator is much less acurate than a resonator or crystal. It also has a much greater temperature coeficient. Meaning that the speed of the osc will be related to the ambient temperature. And yes, it could run fast and slow depending on the temp.

    If the place where you intend to install this has a mostly steady temp, then you could probably get by with a "fudge factor" and get within a couple minutes a day. But remember the error will be cumulative. One minute a day is half an hour a month, six hours a year. To keep it near midnight you'll have to reset it about every 6 months.

    -Denny
    Last edited by Desterline; - 28th December 2003 at 22:07.

  8. #8
    rad's Avatar
    rad Guest


    Did you find this post helpful? Yes | No

    Default

    OK, thanks again.

    I meant interal oscillator and the ambiant temperature thing I didn't know.

    I'll play around with this and let you all know what I decide and how it works.

  9. #9
    electronicsuk's Avatar
    electronicsuk Guest


    Did you find this post helpful? Yes | No

    Default

    It's probably a bit late to mention this now, but can't you get real time clock modules that you could interface to your micro?

    Matthew

Similar Threads

  1. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  2. Serial communication fails after long pause
    By brid0030 in forum General
    Replies: 4
    Last Post: - 13th February 2008, 18:56
  3. Using RTC to generate long time delays
    By schlaray in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd January 2007, 00:31
  4. Long varible strings
    By flashg in forum Serial
    Replies: 0
    Last Post: - 14th October 2006, 04:40
  5. Is "PAUSE 0" a valid command
    By keithdoxey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 19th July 2006, 16:39

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts