Shop OBEX P1 Docs P2 Docs Learn Events
Looping a program — Parallax Forums

Looping a program

ArchiverArchiver Posts: 46,084
edited 2003-07-09 16:36 in General Discussion
I am a newbie so forgive. I am trying to loop through a stamp set of
instructions several times (to get triplicate readings). I am using
the FOR-NEXT code but am unable to program any loops that make
sense.The main body or the program works fine for my purposes.

Any hints would be appreciated.

Thanks Mike Z




'{$STAMP BS2}
'
Define variables, set status of pins
reps VAR WORD
x VAR WORD
y VAR WORD
z VAR WORD
cnt VAR WORD
cmt VAR WORD
clt VAR WORD
S0 CON 0
S1 CON 1
S2 CON 2
S3 CON 3
LED1 CON 11
LED2 CON 12
LOW S0 'Sets status of TSL230 pins
HIGH S1
HIGH S2
LOW S3
LOW 4 'Toggle state to enable/disable chip – change this if there is
no
'output from the TSL230
'
Main program loop
`


ROUTINE1
LOW LED1
PAUSE 2000
FOR x = 1 TO 2000
COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt
DEBUG DEC cnt, CR 'Send value of cnt, as a decimal to the computer
NEXT 'Do it all over again, 2000 times
IF x=2000 THEN ROUTINE2

ROUTINE2:
PAUSE 2000
FOR y = 1 TO 2000
COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt
DEBUG DEC cmt, CR 'Send value of cmt, as a decimal to the computer
NEXT 'Do it all over again, 2000 times
IF y=2000 THEN ROUTINE3


ROUTINE3:
HIGH LED1
LOW LED2
PAUSE 2000
FOR z = 1 TO 2000
COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer
NEXT 'Do it all over again, 2000 times
HIGH LED2
IF z = 2000 THEN ROUTINE4


ROUTINE4
FOR reps = 1 TO 3
GOTO ROUTINE1
NEXT
END

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-07-08 17:17
    Like skinning a cat (no flames, please -- I do like cats), there are
    many ways to construct a loop in BASIC.

    1) To loop forever, use GOTO Label or DO-LOOP

    Main:
    '
    ' looped statements
    '
    GOTO Main


    Main:
    DO
    '
    ' looped statements
    '
    LOOP

    I tend to use GOTO when there are a lot of loop statements, DO-LOOP when
    there are just a few.


    2) To loop a specific number of times, use FOR-NEXT

    Main:
    FOR idx = 1 TO 5
    '
    ' loop statements (that will run five times)
    '
    NEXT



    3) To loop while some condition is true:

    Main:
    DO WHILE (condition)
    '
    ' loop statements
    '
    LOOP

    The loop statements will not run if the condition is false when the loop
    is encountered.


    4) To loop until a condition becomes true

    Main:
    DO
    '
    ' loop statements
    '
    LOOP UNTIL (condition)

    Since the test is at the end of the loop, the loop statements will run
    at least one time using this construct.

    Okay, by now your head is probably spinning. Take a deep breath, talk
    through your program (out load -- this really helps), then loop through
    the templates above to see what will work best. Make sure that you have
    the latest BASIC Stamp compiler installed so you can consult the help
    file for additional details.

    Good luck and have fun.

    -- Jon Williams
    -- Parallax


    Original Message
    From: mziegler1485 [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=wh-Cwb1wasuLnWTxAw8s3jOZMWfxCjTABM-mV4KULA_vSga2kEbMiI6pDCMr7YxMnzO7FfqSsC32bUo]mziegler@h...[/url
    Sent: Tuesday, July 08, 2003 10:12 AM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Looping a program


    I am a newbie so forgive. I am trying to loop through a stamp set of
    instructions several times (to get triplicate readings). I am using
    the FOR-NEXT code but am unable to program any loops that make
    sense.The main body or the program works fine for my purposes.

    Any hints would be appreciated.

    Thanks Mike Z




    '{$STAMP BS2}
    '
    Define variables, set status of pins
    reps VAR WORD
    x VAR WORD
    y VAR WORD
    z VAR WORD
    cnt VAR WORD
    cmt VAR WORD
    clt VAR WORD
    S0 CON 0
    S1 CON 1
    S2 CON 2
    S3 CON 3
    LED1 CON 11
    LED2 CON 12
    LOW S0 'Sets status of TSL230 pins
    HIGH S1
    HIGH S2
    LOW S3
    LOW 4 'Toggle state to enable/disable chip - change this if there is
    no
    'output from the TSL230
    '
    Main program loop
    `


    ROUTINE1
    LOW LED1
    PAUSE 2000
    FOR x = 1 TO 2000
    COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt DEBUG DEC
    cnt, CR 'Send value of cnt, as a decimal to the computer NEXT 'Do it all
    over again, 2000 times IF x=2000 THEN ROUTINE2

    ROUTINE2:
    PAUSE 2000
    FOR y = 1 TO 2000
    COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt DEBUG DEC
    cmt, CR 'Send value of cmt, as a decimal to the computer NEXT 'Do it all
    over again, 2000 times IF y=2000 THEN ROUTINE3


    ROUTINE3:
    HIGH LED1
    LOW LED2
    PAUSE 2000
    FOR z = 1 TO 2000
    COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
    DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer NEXT
    'Do it all over again, 2000 times HIGH LED2 IF z = 2000 THEN ROUTINE4


    ROUTINE4
    FOR reps = 1 TO 3
    GOTO ROUTINE1
    NEXT
    END






    To UNSUBSCRIBE, just send mail to:
    basicstamps-unsubscribe@yahoogroups.com
    from the same email address that you subscribed. Text in the Subject
    and Body of the message will be ignored.


    Your use of Yahoo! Groups is subject to
    http://docs.yahoo.com/info/terms/




    This message has been scanned by WebShield. Please report SPAM to
    abuse@p....
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-08 18:25
    One problem: Routine4:
    'GOTO' is not a 'GOSUB'. Once you 'GOTO' someplace,
    you don't return, so Routine4 will enter once and
    then Jump to Routine1, never to return.



    --- In basicstamps@yahoogroups.com, "mziegler1485" <mziegler@h...>
    wrote:
    > I am a newbie so forgive. I am trying to loop through a stamp set
    of
    > instructions several times (to get triplicate readings). I am using
    > the FOR-NEXT code but am unable to program any loops that make
    > sense.The main body or the program works fine for my purposes.
    >
    > Any hints would be appreciated.
    >
    > Thanks Mike Z
    >
    >
    >
    >
    > '{$STAMP BS2}
    > '
    Define variables, set status of pins
    > reps VAR WORD
    > x VAR WORD
    > y VAR WORD
    > z VAR WORD
    > cnt VAR WORD
    > cmt VAR WORD
    > clt VAR WORD
    > S0 CON 0
    > S1 CON 1
    > S2 CON 2
    > S3 CON 3
    > LED1 CON 11
    > LED2 CON 12
    > LOW S0 'Sets status of TSL230 pins
    > HIGH S1
    > HIGH S2
    > LOW S3
    > LOW 4 'Toggle state to enable/disable chip – change this if there
    is
    > no
    > 'output from the TSL230
    > '
    Main program loop
    `
    >
    >
    > ROUTINE1
    > LOW LED1
    > PAUSE 2000
    > FOR x = 1 TO 2000
    > COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt
    > DEBUG DEC cnt, CR 'Send value of cnt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > IF x=2000 THEN ROUTINE2
    >
    > ROUTINE2:
    > PAUSE 2000
    > FOR y = 1 TO 2000
    > COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt
    > DEBUG DEC cmt, CR 'Send value of cmt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > IF y=2000 THEN ROUTINE3
    >
    >
    > ROUTINE3:
    > HIGH LED1
    > LOW LED2
    > PAUSE 2000
    > FOR z = 1 TO 2000
    > COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
    > DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > HIGH LED2
    > IF z = 2000 THEN ROUTINE4
    >
    >
    > ROUTINE4
    > FOR reps = 1 TO 3
    > GOTO ROUTINE1
    > NEXT
    > END
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-08 18:39
    Another problem:
    FOR X = 1 TO 1000
    ' Do Stuff
    NEXT

    ' Do More Stuff

    It's good software practice not to depend on 'X'
    having ANY particular value in the 'Do More Stuff'
    part. However, if you decide to do so, it's value
    is probably 1001 in this example, as the 'FOR'
    loop won't terminate until 'X' is GREATER THAN
    the last value.

    In your code below, you check if it's EQUAL to
    the last value -- which it won't be. This does
    no harm in your code fragment, since the GOTO
    and the 'fall-through' go to the same code.
    but this could cause problems were that not
    the case.

    --- In basicstamps@yahoogroups.com, "Allan Lane" <allan.lane@h...>
    wrote:
    > One problem: Routine4:
    > 'GOTO' is not a 'GOSUB'. Once you 'GOTO' someplace,
    > you don't return, so Routine4 will enter once and
    > then Jump to Routine1, never to return.
    >
    >
    >
    > --- In basicstamps@yahoogroups.com, "mziegler1485" <mziegler@h...>
    > wrote:
    > > I am a newbie so forgive. I am trying to loop through a stamp set
    > of
    > > instructions several times (to get triplicate readings). I am
    using
    > > the FOR-NEXT code but am unable to program any loops that make
    > > sense.The main body or the program works fine for my purposes.
    > >
    > > Any hints would be appreciated.
    > >
    > > Thanks Mike Z
    > >
    > >
    > >
    > >
    > > '{$STAMP BS2}
    > > '
    Define variables, set status of pins
    > > reps VAR WORD
    > > x VAR WORD
    > > y VAR WORD
    > > z VAR WORD
    > > cnt VAR WORD
    > > cmt VAR WORD
    > > clt VAR WORD
    > > S0 CON 0
    > > S1 CON 1
    > > S2 CON 2
    > > S3 CON 3
    > > LED1 CON 11
    > > LED2 CON 12
    > > LOW S0 'Sets status of TSL230 pins
    > > HIGH S1
    > > HIGH S2
    > > LOW S3
    > > LOW 4 'Toggle state to enable/disable chip – change this if there
    > is
    > > no
    > > 'output from the TSL230
    > > '
    Main program loop
    `
    > >
    > >
    > > ROUTINE1
    > > LOW LED1
    > > PAUSE 2000
    > > FOR x = 1 TO 2000
    > > COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt
    > > DEBUG DEC cnt, CR 'Send value of cnt, as a decimal to the computer
    > > NEXT 'Do it all over again, 2000 times
    > > IF x=2000 THEN ROUTINE2
    > >
    > > ROUTINE2:
    > > PAUSE 2000
    > > FOR y = 1 TO 2000
    > > COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt
    > > DEBUG DEC cmt, CR 'Send value of cmt, as a decimal to the computer
    > > NEXT 'Do it all over again, 2000 times
    > > IF y=2000 THEN ROUTINE3
    > >
    > >
    > > ROUTINE3:
    > > HIGH LED1
    > > LOW LED2
    > > PAUSE 2000
    > > FOR z = 1 TO 2000
    > > COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
    > > DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer
    > > NEXT 'Do it all over again, 2000 times
    > > HIGH LED2
    > > IF z = 2000 THEN ROUTINE4
    > >
    > >
    > > ROUTINE4
    > > FOR reps = 1 TO 3
    > > GOTO ROUTINE1
    > > NEXT
    > > END
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-09 03:57
    It's a DFNN (definite fucking NO NO) to jump out of a For/Next loop
    with a GoTo. First, use GOSUBs in the loop (with RETURNs in the
    appropriate places at the routine end) and, second, place your RoutineX
    stuff BELOW the main calling loop. you need to trace your code line by
    line to see the error of your ways.
    PS-We ALL start by making spaghetti code, but it's a good habit to
    break. You might look at the BLOCK IF in BS2.5 to help you.
    On Tuesday, July 8, 2003, at 11:11 AM, mziegler1485 wrote:

    > I am a newbie so forgive. I am trying to loop through a stamp set of
    > instructions several times (to get triplicate readings). I am using
    > the FOR-NEXT code but am unable to program any loops that make
    > sense.The main body or the program works fine for my purposes.
    >
    > Any hints would be appreciated.
    >
    > Thanks Mike Z
    >
    >
    >
    >
    > '{$STAMP BS2}
    > '
    Define variables, set status of pins
    > reps VAR WORD
    > x VAR WORD
    > y VAR WORD
    > z VAR WORD
    > cnt VAR WORD
    > cmt VAR WORD
    > clt VAR WORD
    > S0 CON 0
    > S1 CON 1
    > S2 CON 2
    > S3 CON 3
    > LED1 CON 11
    > LED2 CON 12
    > LOW S0 'Sets status of TSL230 pins
    > HIGH S1
    > HIGH S2
    > LOW S3
    > LOW 4 'Toggle state to enable/disable chip – change this if there is
    > no
    > 'output from the TSL230
    > '
    Main program loop
    `
    >
    >
    > ROUTINE1
    > LOW LED1
    > PAUSE 2000
    > FOR x = 1 TO 2000
    > COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt
    > DEBUG DEC cnt, CR 'Send value of cnt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > IF x=2000 THEN ROUTINE2
    >
    > ROUTINE2:
    > PAUSE 2000
    > FOR y = 1 TO 2000
    > COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt
    > DEBUG DEC cmt, CR 'Send value of cmt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > IF y=2000 THEN ROUTINE3
    >
    >
    > ROUTINE3:
    > HIGH LED1
    > LOW LED2
    > PAUSE 2000
    > FOR z = 1 TO 2000
    > COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
    > DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > HIGH LED2
    > IF z = 2000 THEN ROUTINE4
    >
    >
    > ROUTINE4
    > FOR reps = 1 TO 3
    > GOTO ROUTINE1
    > '*************************************************************
    > NEXT
    > END
    >
    >
    >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject
    > and Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to
    > http://docs.yahoo.com/info/terms/
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-09 14:31
    Thanks to all who replied! Mike Smith at the Selmaware group gave me
    the simplest if not most elegant solution which was to include the
    whole program in a FOR-NEXT sequence with minor alterations.

    Mike Z
  • ArchiverArchiver Posts: 46,084
    edited 2003-07-09 16:36
    If I understand what you want to do, execute ROUTINE1, ROUTINE2, and
    ROUTINE3 three times, the simplest way to do this would look like
    this:

    FOR reps = 1 to 3

    LOW LED1
    PAUSE 2000
    FOR x = 1 TO 2000
    COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt
    DEBUG DEC cnt, CR 'Send value of cnt, as a decimal to the computer
    NEXT 'Do it all over again, 2000 times

    PAUSE 2000
    FOR y = 1 TO 2000
    COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt
    DEBUG DEC cmt, CR 'Send value of cmt, as a decimal to the computer
    NEXT 'Do it all over again, 2000 times

    HIGH LED1
    LOW LED2
    PAUSE 2000
    FOR z = 1 TO 2000
    COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
    DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer
    NEXT 'Do it all over again, 2000 times
    HIGH LED2

    NEXT 'do this three times
    END

    When you exit a FOR NEXT loop, the variable should be one greater
    than the loop. For example at the bottom of ROUTINE1 where the IF
    statement is, x will be 2001. The IF statement is not needed because
    the program will execute the next line anyway.

    The comments you added in this program are helpful. You may also find
    it helpful to indent any code in a loop or an if statement to make it
    easier to see what is supposed to happen where. This is especially
    true if you have loops inside of loops.

    FOR x = 1 to 4
    FOR y = 1 to 8
    do some stuff here 'this stuff gets done 32 times
    FOR z = 1 to 10
    do some other stuff here 'this stuff gets done 320 times
    NEXT 'this NEXT lines up with FOR z so you can see it
    NEXT 'this NEXT lines up with FOR y
    NEXT 'this NEXT lines up with FOR x

    In a longer program with more code it is easy to miss a NEXT or some
    other end of condition statement. I hope this helps.

    Paul Caesar


    --- In basicstamps@yahoogroups.com, "mziegler1485" <mziegler@h...>
    wrote:
    > I am a newbie so forgive. I am trying to loop through a stamp set
    of
    > instructions several times (to get triplicate readings). I am using
    > the FOR-NEXT code but am unable to program any loops that make
    > sense.The main body or the program works fine for my purposes.
    >
    > Any hints would be appreciated.
    >
    > Thanks Mike Z
    >
    >
    >
    >
    > '{$STAMP BS2}
    > '
    Define variables, set status of pins
    > reps VAR WORD
    > x VAR WORD
    > y VAR WORD
    > z VAR WORD
    > cnt VAR WORD
    > cmt VAR WORD
    > clt VAR WORD
    > S0 CON 0
    > S1 CON 1
    > S2 CON 2
    > S3 CON 3
    > LED1 CON 11
    > LED2 CON 12
    > LOW S0 'Sets status of TSL230 pins
    > HIGH S1
    > HIGH S2
    > LOW S3
    > LOW 4 'Toggle state to enable/disable chip – change this if there
    is
    > no
    > 'output from the TSL230
    > '
    Main program loop
    `
    >
    >
    > ROUTINE1
    > LOW LED1
    > PAUSE 2000
    > FOR x = 1 TO 2000
    > COUNT 5, 1, cnt 'Count on pin 5 for 1 ms, store value in cnt
    > DEBUG DEC cnt, CR 'Send value of cnt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > IF x=2000 THEN ROUTINE2
    >
    > ROUTINE2:
    > PAUSE 2000
    > FOR y = 1 TO 2000
    > COUNT 6, 1, cmt 'Count on pin 6 for 1 ms, store value in cmt
    > DEBUG DEC cmt, CR 'Send value of cmt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > IF y=2000 THEN ROUTINE3
    >
    >
    > ROUTINE3:
    > HIGH LED1
    > LOW LED2
    > PAUSE 2000
    > FOR z = 1 TO 2000
    > COUNT 7, 1, clt 'Count on pin 6 for 1 ms, store value in clt
    > DEBUG DEC clt, CR 'Send value of clt, as a decimal to the computer
    > NEXT 'Do it all over again, 2000 times
    > HIGH LED2
    > IF z = 2000 THEN ROUTINE4
    >
    >
    > ROUTINE4
    > FOR reps = 1 TO 3
    > GOTO ROUTINE1
    > NEXT
    > END
Sign In or Register to comment.