Shop OBEX P1 Docs P2 Docs Learn Events
How do you run a background timer? — Parallax Forums

How do you run a background timer?

MagnetsMagnets Posts: 2
edited 2013-10-23 07:31 in BASIC Stamp
I'm relatively new to the BASIC language so this might be a problem in not knowing enough commands. I need to start a timer that when started from one point in the program can count up from zero while the program goes on and does whatever else it needs to do. Each time control passes by a particular part of the code it should be able to check to see if the timer is greater than of equal to a time value and then if so reset the timer and make it able to start again, while if not then going on and checking later, when it eventually gets back to the part where it starts the timer.

Just as clarification I know how to do an if . . . else statement. I just can't make a timer that doesn't require continuous command to save my life, one that can be run in the background of the rest of the program.

Thanks to anyone who can help

Comments

  • Clive WakehamClive Wakeham Posts: 152
    edited 2013-10-19 19:01
    You can't basically since the Basic Stamp is a linear processing type processor.
    To get around it you can add an external timing device and then refer to it at times during your program.

    If you have the Basic Stamp that has the inbuilt I2C commands then a I2C RTC/Timer like the PCF8563 would do.
    If you don't have the Basic Stamp with I2C commands then you can use a serial RTC device like the DS-1302 that is sold by Parallax (Product ID 604-00005)
  • ercoerco Posts: 20,257
    edited 2013-10-19 20:40
    There's no RTC built in, but you can do a workaround by running your program in a calibrated loop. FOR/NEXT, DO/LOOP, even main:/ goto main. Basically you execute your code in linear style, then loop back and increment a counter (FOR/NEXT does this automatically). Exit your loop when the counter hits a certain number. You can add some pauses in each loop and adjust the number of loops to calibrate to hit your timing goal.
  • MagnetsMagnets Posts: 2
    edited 2013-10-20 09:35
    Thanks Clive, that should work nicely. And erco, I already tried that and it won't work because the rest of the program needs to do anything for any random amount of time which will be generally less than 2min. But thanks anyways.
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-10-20 15:48
    Just for the record, you don't necessarily need a Basic Stamp with built-in I2C to achieve I2C communication, so don't let this limit your decision.

    See this thread:
    http://forums.parallax.com/showthread.php/82908-I2C-On-A-BASIC-Stamp?p=567240&viewfull=1#post567240
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-20 21:48
    I recently purchased a couple of these RTC. I think they're supposed to be more accurate than many of the other options. I also thought it was a pretty good price.

    I would second Beau's advice:
    . . don't let this limit your decision.

    Speaking of which, you might want to read a suggestion by Phil Pilgrim (one of the forum's smart guys). (See post #29)

    If you take Phil's advice, there are lots of us willing to help. There are also lots willing to help if you prefer your cozy straitjacket.

    In case you're interested, I have links to tutorials in post #3 of my index (see signature). I hope you checkout some of my projects listed in post #2 while you're there.

    Even though I didn't explicitly say the phase "YRSUTTP" I doubt I'll completely escape erco's wrath.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2013-10-20 22:30
    That was me two years ago in one of my "soapbox" moments. But look what those two years have wrought, emphasis-wise. :)

    -Phil
  • ercoerco Posts: 20,257
    edited 2013-10-22 17:36
    Back in the day, I would have suggested a fine external LM555 one shot timer circuit, which could be built for a fairly consistent 2-minute interval. Two Stamp pins, one to trigger, one to monitor the output. (Or, you could probably do it all on one pin. ) Not much to the circuit... It's easy to slap an LED in so you can see when the timing interval starts & stops.

    Heck, I may have to do this just for fun.
    379 x 281 - 14K
  • ercoerco Posts: 20,257
    edited 2013-10-22 17:40
    @DD (AKA YRSUTTP): As I said elsewhere, I love the idea of 8 processors and lots of IO pins. Too bad eight BS1s cost $232, huh?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-22 20:30
    erco wrote: »
    @DD (AKA YRSUTTP): As I said elsewhere, I love the idea of 8 processors and lots of IO pins. Too bad eight BS1s cost $232, huh?

    That's so last decade. Try to keep up with the current state of the art.

    Oh, no. I think that was another YRSUTTP comment in the Basic Stamp forum. I claim entrapment.
  • ercoerco Posts: 20,257
    edited 2013-10-23 07:31
    Yrsdtts!!!
Sign In or Register to comment.