Shop OBEX P1 Docs P2 Docs Learn Events
Want a loop to run for a certain amount of time. What to do? — Parallax Forums

Want a loop to run for a certain amount of time. What to do?

SeanDFSeanDF Posts: 12
edited 2008-07-16 12:31 in BASIC Stamp
Hi Everyone,
New to the forum, and I am currently working on a project for a industrial application. The program should be very easy to write, and so far I am having success. I started using the Basic Stamp 2, as I do not know C or Assembly language. My knowledge of BASIC goes back to the Commodore 64 days. However, I did not spend much time messing with those machines, as I was 13 at the time. But I did remember it being pretty easy to learn, thus I selected the Basic Stamp for this industrial application.

I'll go ahead and list my simple code that I wrote. I'm using the BOE as of now, and my LED's are all working properly to indicate the desired functions.

' {$STAMP BS2}
' {$PBASIC 2.0}

TEST:
PAUSE 3000
INPUT 14
IF IN14 = 1 THEN IGNITION
IF IN14 = 0 THEN FAILED


IGNITION:
INPUT 14
IF IN14 = 0 THEN FLAME_ON
LOW 12
HIGH 13
PAUSE 2000
LOW 13
PAUSE 100
GOTO IGNITION


FAILED:
HIGH 12
HIGH 13
PAUSE 200
LOW 13
PAUSE 200
GOTO FAILED

FLAME_ON:
INPUT 14
IF IN14 = 1 THEN FAILED
IF IN14 = 0 THEN FLAME_ON
LOW 13
GOTO FLAME_ON


Now, what I'm looking for here is to make the IGNITION routine loop for 20 seconds, then GOTO FAILED, unless of course pin 14 goes to 0, whereas the program will go to the FLAME_ON·routine. Maybe I need a counter in that part of the program.? I'm not sure.·I have checked around for various examples of how to do this in the Syntax manual. I really cant find a way to make it possible. Also, I'm having another problem as well. If the program makes it to the FLAME_ON routine, and pin 14 goes to 1, after I make pin 14 0 again, it will delay before going to FAILED. I'm looking at almost 5 seconds of delay before going to FAILED. Why is that?

Thanks guys, and I really look forward

Comments

  • FranklinFranklin Posts: 4,747
    edited 2008-07-14 02:27
    Well, you have 2.1 seconds of pause in the routine so if you add for i = 1 to 10 and next around ignition you'll have approximately (but not exactly) 21 seconds.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • SeanDFSeanDF Posts: 12
    edited 2008-07-14 02:44
    OK, so how do I define what I represents? I'm guessing this will be a variable, as i tried to put that in, and it gave me and error, expected a variable. I understand what your saying, if I loop this for 10 times, it will be for 21 seconds, as there are 2.1 seconds in the whole routine.
  • SeanDFSeanDF Posts: 12
    edited 2008-07-14 02:52
    Ok, hehe, I figured it out. I included a variable line at the beginning, I·· VAR·· BYTE.

    I then added FOR I = 1 TO 10 at the beginning of the routine, and·replaced·GOTO IGNITION with next, and it works perfectly.

    Thanks for your help.
  • SeanDFSeanDF Posts: 12
    edited 2008-07-14 14:02
    Hi. I'm still working with this program. Seems that the program makes my BS2 work erratically. Upon power on, it just does what it wants. Also seems that processing of the Inputs is rather slow. And my program hangs up in other spots. I dont know why, seems like the program is rather easy. Any ideas anyone?
  • stamptrolstamptrol Posts: 1,731
    edited 2008-07-14 14:41
    So, to go from "working perfectly" to what sounds like not working very well at all in about 12 hours.

    Something has to have changed. Show us the code as it is now.

    Most likely its to do with what happens in your loop while waiting for for the 21 seconds to elapse. Because the Stamp doesn't do more than one thing at a time, you may be expecting a particular action which is triggered by something that is not being checked.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com
    ·
  • SeanDFSeanDF Posts: 12
    edited 2008-07-14 14:52
    Well at one timew I tyhought it was running perfectly, but then I starte dto notice that the stamp is doing some odd things. In programming theory, I would think this thing would be running just fine, but the program seems to stall out, and do ther things like whatever it wants upon reset.

    Heres the code as it is now.

    ' {$STAMP BS2pe}
    ' {$PBASIC 2.0}

    I VAR Byte

    Test:
    PAUSE 3000
    INPUT 14
    IF IN14 = 1 THEN IGNITION
    IF IN14 = 0 THEN FAILED


    IGNITION:
    FOR I = 1 TO 10
    INPUT 14
    IF IN14 = 0 THEN FLAME_ON
    LOW 12
    HIGH 13
    PAUSE 2000
    LOW 13
    PAUSE 100
    NEXT


    FAILED:
    HIGH 12
    HIGH 13
    PAUSE 175
    LOW 13
    PAUSE 175
    GOTO FAILED


    FLAME_ON:
    INPUT 14
    IF IN14 = 1 THEN FAILED
    IF IN14 = 0 THEN FLAME_ON
    LOW 13
    GOTO FLAME_ON



    Maybe ther's a simpler or better way to write this. I'm just begining to work with the STAMP, and I'm sure I have alot of learning to do. Thanks for your help!
    ·
  • FranklinFranklin Posts: 4,747
    edited 2008-07-14 15:16
    Perhaps you could explain what it is you are doing. I see that if it gets a failed condition it goes to failed and stays there forever and if it goes to flameon it will either stay there or go to failed and stay there forever.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • MorrolanMorrolan Posts: 98
    edited 2008-07-14 15:59
    Please find a small tidy-up of your code below. It might make it a little easier to follow the code if you name your pins and variables like I have done below:


    ' {$STAMP BS2pe}
    ' {$PBASIC 2.5}
    
    
    
    ' CONSTANTS for ease of readability
    LedOn          CON     1
    LedOff         CON     0
    
    Success        CON     0
    Failed         CON     1
    
    ' PINS
    led1           PIN     12
    led2           PIN     13
    ignition       PIN     14
    
    counter        VAR     Nib ' this was I previously, but as a Nib uses less RAM than a byte and can store anything from 0 - 15)
    
    
    
    
    
    TEST:              ' This label is never called so could be defined in a comment instead, as the program never performs a "GOTO" to this point.
       PAUSE 3000
       INPUT 14
           IF ignition = Failed THEN
               GOTO IGNITE
           ELSEIF ignition = Success THEN
               GOTO FAIL:
           ENDIF
    
    
    IGNITE:
       FOR counter = 1 TO 10
       'INPUT 14  ' no need as this is still set as in input from TEST
           IF ignition = Success THEN GOTO Flame_On
               led1 = LedOff
               led2 = LedOn
                   PAUSE 2000
               led2 = LedOff
               PAUSE 100
       NEXT
    
    
    FAIL:
       led1 = LedOn
       led2 = LedOn
           PAUSE 175
       led2 = LedOff
           PAUSE 175
       GOTO FAIL     ' This will then need to be reset before it will work again as once the program flow lands here, it will never leave!
    
    
    
    FLAME_ON:
       'INPUT 14       ' No need for this again
       IF ignition = Failed THEN GOTO FAIL
       IF ignition = Success THEN GOTO FLAME_ON
           led2 = LedOff
       GOTO FLAME_ON   ' This will then need to be reset before it will work again as once the program flow lands here, it will never leave!
    



    Instead of using GOTO's (most developers HATE them as it's too hard to follow program flow) perhaps in FAIL and FLAME_ON you might put "RETURN" at the end and use a GOSUB instead of a GOTO. This would then return to the next line of code in your program and would help prevent it from locking up.

    If you can please explain what pins 12, 13 and 14 are, I'll be able to help you make more sense of it as I've had to make assumptions in the code.

    Also, why were you using 2.0 PBASIC syntax instead of 2.5? Is there any reason for this as I run a BS2pe using 2.5 syntax fine?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Flying is simple. You just throw yourself at the ground and miss.

    "I think computer viruses should count as life. I think it says something about human nature that the only form of life we have created so far is purely destructive. We've created life in our own image."
    Stephen Hawking

    Post Edited (Morrolan) : 7/15/2008 8:12:41 AM GMT
  • stamptrolstamptrol Posts: 1,731
    edited 2008-07-14 16:58
    ·· Another way to analyze program performance is with a flow chart.

    ·· Attached is·what I got out of your original program. I think it confirms the previous post about its execution.

    ·· Cheers,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tom Sisk

    http://www.siskconsult.com


    Post Edited (stamptrol) : 7/14/2008 5:04:08 PM GMT
  • SeanDFSeanDF Posts: 12
    edited 2008-07-16 01:19
    Basically, what I would like to start with is simple. I would like the program to test a PIN for 3 seconds, and if at any time during the 3 seconds the pin receives a voltage, then I would like to jump the program to a looping FAIL routine.· I would like this routine to continue for the whole 3 seconds though, then determine if there was a a voltage present or not, THEN go to the proper routine.· So the program should loop for 3 seconds, then if it had a voltage at some point, goto·A or·goto B.

    I have read alot about polling, but this doesnt seem to give me the option of the 3 seconds.


    And, now i thought about this a lil bit further, after thinking that the STAMP can only process one command at a time, I came up with this idea. Just not sure how to code it.

    this would be ideal:

    power on
    test pin 0
    if pin 0 = 1 store·a constant·memory
    pause 100
    (repeat the "if - pause" commands 29 more times :30 x 100 = 3000, which gives the 3 second cycle)
    then look to the memory and if the value stored equals the value that was triggered by voltage on pin 0 in the test cycle, goto another routine, or if the stored value doesnt equate to a value that would be stored by a voltage on pin 0, then goto another routine.


    I hope this is making some sense of what im trying to do. Not sure at this point if this out of the STAMPS functionality or not. Im going to use a BS2PX....

    Any ideas anyone?







    Post Edited (SeanDF) : 7/16/2008 2:37:20 AM GMT
  • SeanDFSeanDF Posts: 12
    edited 2008-07-16 12:04
    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}


    A CON 1
    B CON 0
    C CON 1
    D CON 0
    STATUS VAR Byte
    TEMP VAR Byte
    TIME VAR Byte



    DEBUG "TESTING PILOT STATUS", CR
    INPUT 0
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100 '1 Second
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100 '2 Seconds
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100
    IF IN0 = 1 THEN PUT 25, A
    PAUSE 100 '3 Seconds
    GET 25, TEMP 'Access Scratch Pad RAM
    DEBUG DEC TEMP, CR
    IF TEMP = a THEN FAILED 'Determine RAM Value and GOTO Correct Routine

    IGNITION:
    HIGH 3 'Ignition Relay On
    HIGH 2 'Gas Relay On
    FOR TIME = 1 TO 10 'Time for 10 Cycles of 2.1 second per Cycle
    GOSUB FLAME_DET
    IF STATUS = C THEN FLAME_OK
    DEBUG "SPARK.", CR
    HIGH 1 'LED Blink Loop
    PAUSE 100
    LOW 1
    PAUSE 2000
    NEXT

    FAILED:
    DEBUG "PILOT HAS FAILED. RESTART REQUIRED.", CR
    LOW 3
    LOW 2
    HIGH 1
    PAUSE 175
    LOW 1
    PAUSE 175
    GOTO FAILED

    FLAME_DET:
    IF IN0 = 1 THEN PUT 27, C
    IF IN0 = 0 THEN PUT 27, D
    GET 27, STATUS
    RETURN

    FLAME_OK:
    DEBUG "FLAME DETECTED AT PILOT.", CR
    HIGH 1
    HIGH 2
    GOTO IGNITION




    Well, here is the latest program. This one seems to work alot better, although I'm still having a few quircks that I'm having. One is electronic I'm sure, as if my PIN 0 goes high, but never returns to LOW which it looks for (i.e., the PIN floats after going high), then the program might stall for a few seconds. I will be trying to fix this today.

    My next issue, which I'm not sure is program related, or just a quirck in the STAMP is on power on. Sometimes, the STAMP goes right to the first instruction, and works properly. And sometimes when reset, or cycled on and off, it will seem like it jumps right to FAIL routine. Other times, it won't do anything.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2008-07-16 12:31
    Look at page 76 and 77

    http://www.parallax.com/Portals/0/Downloads/docs/books/edu/Wamv2_2.pdf

    Do you have your input hook up this way you do not need the switch

    I would DEBUG your inputs to see if they are working the way you want

    them·to

    Ones the input are working right then you can·remove the DEBUG statements

    INPUT 7······························ ' Make P7 an input
    · DEBUG "State of P7: ",
    ······· BIN1 IN7, CR


    I am using this my self in a project that i am working on right now and this is

    how I trouble shoot what is not working

    I hope this helps you

    ·[font=Arial,Italic size=1][font=Arial,Italic size=1] [/font][/font]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ··Thanks for any·idea.gif·that you may have and all of your time finding them

    ·
    ·
    ·
    ·
    Sam

    Post Edited (sam_sam_sam) : 7/16/2008 12:50:58 PM GMT
Sign In or Register to comment.