Shop OBEX P1 Docs P2 Docs Learn Events
How to calculate timing for precise 1Hz control action? — Parallax Forums

How to calculate timing for precise 1Hz control action?

JediJedi Posts: 5
edited 2013-08-14 13:36 in BASIC Stamp
Very simple program so hopefully the solution will be as well. Seems there
is some simple math I'm missing.

What this program does is trigger a 500ms pulse at precise 1 minute intervals to
drive a relay that controls a vintage "minute impulse clock" I'm restoring.

Logic would say if I allow 500ms for the logic output pulse, and 59500ms for
the delay that it would add up to a perfect minute. But it doesn't - and I'm
certain it's just simply due to processor cycle time. Using that number the
output pulses lag every so slightly - 4 minutes in 24 hours. I arbitrarily
used 59000 and confirmed it then ran fast by about the same amount.

So what would be the CORRECT number? Or is it just a "guessing game
until you get it" sort of thing, due to variations from one stamp to another?
Or is there a formula that takes code length into account or other factors
to actually calculate the right "real world" timing?

Code below - not that it's needed really but just in case.

Thanks for any help

Jedi

Minute Impulse.PNG
653 x 482 - 43K

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-08-14 11:38
    It may vary a tiny bit from Stamp to Stamp, since they use ceramic resonators for their time base, rather than a crystal. But the timing errors you're seeing are from interpreter overhead, not an inaccurate timebase. If you're off by 4 minutes in 24 hours, that's ten seconds per hour, or 1/6 second per minute. So correct your second pause by that amount, and try again, refining your corrections each time until it's spot on. For really fine corrections, you can also add a PULSOUT to an unused pin.

    -Phil
  • JediJedi Posts: 5
    edited 2013-08-14 13:36
    It may vary a tiny bit from Stamp to Stamp, since they use ceramic resonators for their time base, rather than a crystal. But the timing errors you're seeing are from interpreter overhead, not an inaccurate timebase. If you're off by 4 minutes in 24 hours, that's ten seconds per hour, or 1/6 second per minute. So correct your second pause by that amount, and try again, refining your corrections each time until it's spot on. For really fine corrections, you can also add a PULSOUT to an unused pin.

    -Phil

    Thanks for the prompt reply Phil! That's what I figured - it's going to be more "trial, error, correct"...
    likely even after the project is done and hanging on the wall.

    There of course will be more to the program - I need a "fast advance button" as well as a "hold time"
    button (for "fall back" time correction) - perhaps both run by code as well. Add to that an LED display
    for tens of seconds and a few jumbo LEDs flashing seconds - all just for the geeky cool of it.

    Thanks for the post.

    Oh, and Port Townsend is one of my favorite places anywhere - been going there on holidays for
    as long as I can remember and did numerous Centrum camps back in the 1970s in Jr. High and HS

    Jedi
Sign In or Register to comment.