Shop OBEX P1 Docs P2 Docs Learn Events
Multi minute delay or counter? — Parallax Forums

Multi minute delay or counter?

CismontguyCismontguy Posts: 10
edited 2014-11-08 15:13 in BASIC Stamp
Hello, I am just getting back into the Basic Stamp programming after 10 year of being away from programing. I want to create a routine that will turn a relay on for 10 minutes, off for 5 minutes. I was using a counter (sample for 2000 ms) but am looking for a better method of time interval control? Can you guys point me in the right direction for some instructions on delays? I do not need anyone to write the code for me. I am just trying to learn again the options available for delay?

Thanks,

Cismounguy

'Robotics with the Boe-Bot - Relaycontroller.bs2
'10 minute on 5 minute off timer.

' {$STAMP BS2}
' {$PBASIC 2.5}
DEBUG "Indicates that the program is running"

counter VAR Word

FREQOUT 4, 2000, 3000 ' signal that the program is running.
DO
GOSUB Running
GOSUB N_running
LOOP
Running:
FOR counter = 1 TO 2000
HIGH 13 ' drives pin 13 high
DEBUG ? counter ' displays counter .
NEXT
RETURN
N_running:
FOR counter = 1 TO 2000
LOW 13 ' drives pin 13 high
DEBUG ? counter ' displays counter .
NEXT
RETURN

Comments

  • ercoerco Posts: 20,256
    edited 2014-11-06 14:10
    Sure. Some timing loops with a PAUSE inside can be calibrated for the durations you need. For instance:


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    minutes VAR Byte
    seconds VAR Byte


    DEBUG "Program is running"
    FREQOUT 4, 2000, 3000 ' signal that program is running

    do ' start forever loop
    high 14 ' relay on
    for minutes=1 to 10
    for seconds=1 to 60
    pause 1000' pause 1 sec
    next
    next

    low 14' relay off
    for minutes=1 to 5
    for seconds=1 to 60
    pause 1000' pause 1 sec
    next
    next

    loop ' repeat forever
  • JonnyMacJonnyMac Posts: 9,014
    edited 2014-11-06 14:55
    I do this for long delays in the BASIC Stamp

    (fixed listing)
    Delay_Seconds:
      IF (seconds > 0) THEN
        PAUSE 1000
        seconds = seconds - 1
        GOTO Delay_Seconds
      ENDIF
      RETURN
    


    If seconds is a word, you can create very long delays.
  • CismontguyCismontguy Posts: 10
    edited 2014-11-07 04:26
    Thanks for both suggestions! I will try them out. Love this Basic Stamp.
  • stamptrolstamptrol Posts: 1,731
    edited 2014-11-07 04:29
    Depending on what else you need your Stamp to take care of, another technique is to have a subroutine loop as Jon described, but instead of PAUSE 1000, make it much shorter (perhaps 100 or 50).

    Back in your main program you count how many times you use the 100 ms loop. That way, your main loop doesn't have to sit waiting for a long PAUSE to expire before seeing that something else needs doing.

    Cherers,
  • GenetixGenetix Posts: 1,749
    edited 2014-11-07 07:46
    Chapter 7 of Industrial Control uses a Real Time Clock chip to turn a heater on and off.
    http://laspace.lsu.edu/aces/Document/Parallax%20Docs/Industrial%20Control.pdf
  • JonnyMacJonnyMac Posts: 9,014
    edited 2014-11-07 08:04
    If precise, long-term timing becomes an issue, consider the BS2's big brother: the Propeller. It has a multitude of features that allow one to time events. I have a commercial project at the moment that requires timing long and short-duration events, and I'm able to do that withou consuming a processor. Of course, I have apps where I turn one of the processors into a precision clock. If you like the BASIC Stamp, you may come to love the Propeller.

    Cismontguy wrote: »
    Thanks for both suggestions! I will try them out. Love this Basic Stamp.
  • JonnyMacJonnyMac Posts: 9,014
    edited 2014-11-07 08:12
    I've built some industrial controllers with the Stamp and it can be a little tricky creating a precise, 100ms loop -- you have to track and time all the possible paths the loop code can take, and pad everything such that the loop always runs at 100ms (I know, I've done this).

    This is where the Propeller shines. Need a 100ms main loop? No problem. This loop will always run 100ms -- as long as you loop code takes less than 100ms (that's the only rule).
    t := cnt
      repeat
    
        ' other loop code
    
       
      
        ' add sythesized RTC to loop
        '
        if (++tix == 10)
          tix := 0
          if (++seconds == 60)
            seconds := 0
            if (++minutes == 60)
              minutes := 0
              if (++hours == 24)
                hours := 0   
    
        waitcnt(t += (100 * MS_001))	' make sure loop runs every 100ms
    


    You see, Cismontguy, the Propeller is really fun and doesn't take much more effort than the BS2.

    For the record, I'm not trying to put you off the BS2 -- when I worked for Parallax I wrote a book called StampWorks. That is to say, I love the BS2 as well.


    stamptrol wrote: »
    Depending on what else you need your Stamp to take care of, another technique is to have a subroutine loop as Jon described, but instead of PAUSE 1000, make it much shorter (perhaps 100 or 50).

    Back in your main program you count how many times you use the 100 ms loop. That way, your main loop doesn't have to sit waiting for a long PAUSE to expire before seeing that something else needs doing.

    Cherers,
  • CismontguyCismontguy Posts: 10
    edited 2014-11-07 09:42
    Yes, I am trying to make a mixer controller for our investment casting slurry, so the mixer does not have to run 24/7. Next I want to tackle a rotating arm to hold the invested parts as they dry after each dip in the investment slurry.

    CismontGuy
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-11-07 10:28
    Since you asked for alternatives, the list should also include the option of an external real time clock chip. Underscoring Genetix' pointer in post#6. There are lots of examples available of how to hook up RTC chips such as the DS1302 or DS1307 to a Stamp and how to write the PBASIC code. Offloading that function to the external chip can help a lot as your program grows.
  • ercoerco Posts: 20,256
    edited 2014-11-07 12:11
    Jon: Beautiful short routine. Should the code read "seconds=seconds-1" instead of "delay=delay-1" or did I miss something?
    JonnyMac wrote: »
    I do this for long delays in the BASIC Stamp
    Delay_Seconds:
      IF (seconds > 0) THEN
        PAUSE 1000
        delay = delay - 1
        GOTO Delay_Seconds
      ENDIF
      RETURN
    


    If seconds is a word, you can create very long delays.
  • JonnyMacJonnyMac Posts: 9,014
    edited 2014-11-07 12:52
    Yep, my mistake. I've corrected the listing. Thanks for the catch.
  • GenetixGenetix Posts: 1,749
    edited 2014-11-07 13:45
    Parallax also has a more up to date text, called Process Control.
    http://www.parallax.com/sites/default/files/downloads/122-28176-Process-Control-Text-v1.0.pdf

    Here is Jon's StampWorks text - Experiment 27 is on controlling a Stepper Motor
    http://www.parallax.com/sites/default/files/downloads/27297-StampWorks-Manual-v2.1.pdf
  • CismontguyCismontguy Posts: 10
    edited 2014-11-08 12:55
    Here is what I have working so far with a 5V DC relay board controlling 120V AC <5A.

    Cismontguy

    'Robotics with the Boe-Bot - Relaycontroller.bs2
    '10 minute on 3 minute off timer.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DEBUG "Indicates that the program is running"

    minutes VAR Word
    Seconds VAR Word

    FREQOUT 4, 2000, 3000 ' signals that the program is running.
    DO 'start forever loop
    LOW 10 'relay on
    HIGH 12 ' Green LED on
    LOW 13 'red LED off
    DEBUG "Power ON"
    FOR minutes = 1 TO 10
    FOR seconds = 1 TO 60
    PAUSE 1000 ' pause 1 sec
    NEXT
    NEXT
    HIGH 10 'relay on
    LOW 12 ' Gren LED on
    HIGH 13 'Red LED on
    DEBUG "Power OFF"
    FOR minutes = 1 TO 3
    FOR seconds = 1 TO 60
    PAUSE 1000 ' pause 1 sec
    NEXT
    NEXT
    LOOP 'repeat forever
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2014-11-08 15:13
    If you really do want a granuarity of 1 minute, the delays can be condensed, e.g.,
    FOR minutes = 1 TO 10
       PAUSE 60000      ' pause 1 minute
    NEXT
    

    There is also a SLEEP command which can be used for long delays in units of seconds. It is quite accurate for long delays. It does have a drawback though, that the pin that is supposed to be a constant output actually turns into an input briefly every couple of seconds. The LEDs would appear to blink every 2.3 seconds. Your external relay control circuit would need a capacitor or something like that to carry it on or off through those glitches.
    DO
      LOW 10
      SLEEP 600 ' 10 minutes
      HIGH 10
      HIGH 180   ' 3 minutes
    LOOP
    
Sign In or Register to comment.