Shop OBEX P1 Docs P2 Docs Learn Events
Alternating loop — Parallax Forums

Alternating loop

ArchiverArchiver Posts: 46,084
edited 2001-04-09 05:12 in General Discussion
Hi all!

I have a need to make my stamp program alternate between two
different sets of code every pass through the program.

I am running two sonar modules and I think they are setting each
other off because they are pinging one right after the other. If I
could make the program alternate it would ping one then the next time
through the program it would ping the other.

TIA

Doug

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2001-04-09 03:55
    Doug,


    You can do that with a State Machine. i.e.,

    laststate var bit
    laststate=0

    Main:
    if laststate =1 then Check_sonar2

    Check_sonar1:
    "get data from Sonar1"
    Laststate=1
    goto main

    Check_sonar2:
    "get data from sonar2"
    laststate=0
    goto main

    I'm sure there is a more elegant solution but this might work.

    Jeff
  • ArchiverArchiver Posts: 46,084
    edited 2001-04-09 05:12
    So you want to fire one module during one pass through the loop, then the
    other on the next pass? Just use a bit variable -- when it is true, fire one
    module, false, fire the other. Just toggle the value each pass.

    Original Message

    > I have a need to make my stamp program alternate between two
    > different sets of code every pass through the program.
    >
    > I am running two sonar modules and I think they are setting each
    > other off because they are pinging one right after the other. If I
    > could make the program alternate it would ping one then the next time
    > through the program it would ping the other.
Sign In or Register to comment.