Shop OBEX P1 Docs P2 Docs Learn Events
Getting accurate time at a ms resolution? — Parallax Forums

Getting accurate time at a ms resolution?

patterson7019patterson7019 Posts: 25
edited 2008-07-08 05:30 in BASIC Stamp
I'm liking basic stamp. Nice piece of work!

In my main loop, I don't want to have to guess how long it has been since I sent a pulse to a servo. If I were writting for my Mac, I would use a system timer, like the following. From my reading here, the DS1302 won't work as that only goes down to seconds. I haven't used a 555 timer, but can it be set to make an i/o pin high, then low once a certain amount of time has passed?

        long lastUpdate=0;
        long now=0;
        while(true)
        {
            now = System.currentTimeMillis();
            if(now-lastUpdate > 20)
            {
                updateServos();
                lastUpdate = now;
            }
            dosomething();
        }




Thanks,
Lee

Post Edited (patterson7019) : 7/4/2008 8:36:49 PM GMT

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-04 20:04
    The Basic Stamp does not have a system timer like the Mac. There's no way without adding an external timer to keep track of the time while the Stamp is doing other things. The PAUSE statement is very accurate to milliseconds, but the Stamp can't do anything else at the time. The DS1307 clock chip has a 32KHz squarewave output that can be enabled. This can be fed into an external counter which could be reset and read from the Stamp for an elapsed time with a resolution of better than 100uS.
  • patterson7019patterson7019 Posts: 25
    edited 2008-07-05 15:37
    Thanks. I see the professional board has the chip on parallax's site, but I didn't find just the chip on there site. I did find it at technological arts. Any other suggestions for a good online electronics store?
  • Mike GreenMike Green Posts: 23,101
    edited 2008-07-05 16:05
    Jameco, Mouser, and DigiKey all have webstores that take small orders and all have excellent service.
  • kenwtnkenwtn Posts: 250
    edited 2008-07-05 16:12
    You might want to consider a servo controller they are inexpensive and you give them a command and forget. It sure take any guess work out of the equation. I have this one and works great. http://www.pololu.com/catalog/product/207

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Keep buying replacement parts and sooner or later you will get it RIGHT!
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-07-07 14:54
    You might try this external chip: http://www.rhombusinc.com/co-processors.html

    For $6 or so, you get a programmable clock (50 mSec typical) as well as an 11-byte recieve-only serial buffer, in an 8 pin chip.

    You can then use the programmable clock as a 'tick' generator for your own multi-tasking BS2 program.

    And I believe the clock is very accurate.

    Post Edited (allanlane5) : 7/8/2008 4:57:34 PM GMT
  • patterson7019patterson7019 Posts: 25
    edited 2008-07-08 04:31
    Thanks Mike.

    The coprocessors looks rather interesting. Have you actually used them allan? Thanks kenwtn, after working with the servos directly, which is a really good exercise, I plan to use one for my V2 robot. For my first robot the BS2 will be doing it all. But now I don't know which I like better, that little servo controller from pololu or the one from parallax!
  • BobLowryBobLowry Posts: 12
    edited 2008-07-08 05:30
    Those external hardware options would probably be nicest to work with. Another method, which could get pretty messy and annoying, would be to use a variable that represents time, and continually incrementally update the variable to reflect time passing. If you can work out how long each section of your code takes to run, this may be viable.

    perhaps something along these lines:

    time VAR word
    time = 0

    Main:
    PULSOUT 5, 1000
    time = time + 2

    PAUSE 5
    time = time + 5

    If time > 20 then ServoUpdate
    goto Main



    EDIT: Yes you could use the 555 in the way you describe. In "monostable" mode you would send the 555 a trigger pulse, this would cause the 555 to output one squarewave pulse and you could then monitor this with the Stamp.

    Post Edited (BobLowry) : 7/8/2008 5:55:47 PM GMT
Sign In or Register to comment.