Shop OBEX P1 Docs P2 Docs Learn Events
Control loop- routine repeating after fixed time- help! — Parallax Forums

Control loop- routine repeating after fixed time- help!

dp81732dp81732 Posts: 1
edited 2005-11-04 05:26 in BASIC Stamp
What I really need to do is measured elapsed time during program execution.

I'm trying to set up a simple control loop that will endlessly loop.
I want to read some inputs (buttons) every 100ms and update the display every 200ms.
The problem is that I want the tasks to start after the 100ms or 200ms and continue, then run again at the next interval- instead of running all the code, then putting in a delay.

Any recommendations?
I was thinking of maybe using a simple oscillator to trigger an input.

Thanks.

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2005-11-03 19:20
    If your delay time really is like 100 mS, then the BS2 'runtime' will be a trivial addition to that time.

    I believe·http://www.rhombus-tek.com/co-processors.html makes a "Simple Multi-Tasking Co-processor" which can be set to output a signal every 100 mSec, that you can use to synchronize your process.
  • metron9metron9 Posts: 1,100
    edited 2005-11-04 04:53
    Just use an R/C timer, see help file RCTIME command

    I used an 890k or so resistor and .1 capacitor and got about 62337 so:
    62337 times 2 = 124,674 uS
    124,674 / 1000 = 124.67 mS

    So you need a bit less resistance to get the rctimer to return 50,000, to equal 100 mS
    Remember the capacitor has an error percentage that makes a big difference at the far end of the time scale
    You can make a timer to any number of mS you wish as you are just polling the pin for a low state
    and RCtim has a maximum of 65535 time units (BS2=2us per time unit)

    So when I comment out the rctime and uncomment the other code I get the loop to execute about 157 times in 124 mS


    '{$STAMP BS2} 'STAMP directive (specifies a BS2)
    '{$PBASIC 2.5}
    
    rESULT  VAR     Word
    
     HIGH 15                              ' charge the cap
     PAUSE 1                              '   for 1 ms
    
    'When tuning your resistor/capacitor timer using the rctime command comment out the line below 
     DIR15=0                              ' make pin input
    
     'To tune your resistor/capacitor combination so the result is 50,000 uncomment the line below
     'and comment out all other lines below except for tbe debug command
    
      'RCTIME 15, 1, result                   ' measure RC discharge time
    
     'Your main loop code checks pin state, when it's low the timer ran out and 100 ms elapsed
      result=0
      mainloop:
      result=result+1                'this code simply counts the number of times this loop executes
      IF IN15 = 1 THEN mainloop
    
      DEBUG DEC ? result                    ' display result
    
      END 'or do something and go back and startover again
    
    
    
  • metron9metron9 Posts: 1,100
    edited 2005-11-04 05:26
    I thought I would throw in a nice little clock subroutine as well

    Just set up an RC timeer resistor/capacitor circuit and add the subroutine to your program
    include the variables and set up code. Will keep track of 65535 seconds then rolls over.
    If you want a 24 hour clock use another variable and increment it by one in the timesub when seconds rolls over to 0

    With a basic stamp 2 you can execute about 150 instructions every 100 mS so this is like an interrupt routine except
    you must call the timesub routine manually. If you call it every 25 instructions you could be off time by 100/(150/25)
    that is it takes 100 ms for 150 instructions so 25 instructions takes 100/6 or 16.6 mS

    Now programs that have pause statements longer than 100 ms should be converted to polling the tenths variable instead

    All loops and for next routines you should add gosub timesub before then NEXT statement or goto loop statement

    Ok have fun.


    '{$STAMP BS2} 'STAMP directive (specifies a BS2)
    '{$PBASIC 2.5}
    
    tenths  VAR     Byte    '100 mS times 10 = 1 second (assumes external rctimer set at 100ms)
    seconds VAR     Word
    tmp     VAR     Byte
    
    
    'set up timer function for 18 hour timer
    DIR15=0
    tenths  = 10
    seconds = 0
    GOSUB timesub
    
     MAINLOOP:
    
     IF tmp<>tenths THEN   'every 1/10 of a second update display
      DEBUG CLS
      DEBUG DEC ? tenths
      DEBUG DEC ? seconds
      tmp=tenths
     ENDIF
    
     GOSUB timesub
     GOTO mainloop
    
    
    
     timesub:
      IF IN15 = 0 THEN
       HIGH 15            'reset rc timer
       PAUSE 1
       DIR15=0
       tenths=tenths-1
       IF tenths=0 THEN
        tenths=10
        seconds=seconds+1  'update seconds counter
       ENDIF
      ENDIF
      RETURN
    
    
Sign In or Register to comment.