Shop OBEX P1 Docs P2 Docs Learn Events
How to interrupt from a infinite loop ?? — Parallax Forums

How to interrupt from a infinite loop ??

kevinspacekevinspace Posts: 56
edited 2011-04-21 22:57 in Propeller 1
Hello everybody~

How to interrupt from a infinite loop(repeat) by push button??

And another question is how to switch between two infinite loops??

I tried to set a condition of interrupt in the repeat loop but it seems like didn't.

For example : I want to blink the LED always, and if it encounter a condition of interrupt, the blinking will be stop.

Another case is that I want to select any oneinfinite loops, how to write the code to switch it??

Are there any simple example??

Thanks a lot.

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2011-04-21 11:29
    HI Kevin,

    post your code or a more derialed description of what you want to do in the end

    there are a lot of ways to do something like if condition stop blinking

    for too infinite loops

    code ONE loop and some if-conditions

    if condtion 1 true
    "do things1"

    if condtion 2 true
    "do things2"

    really tell us what you want to do in the end
    then good solutions can be found

    for stopping a infinite loop see command quit PropManual page 186

    best regards

    Stefan
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-21 11:59
    Strictly speaking, you can't force a Prop loop to finish or switch to some other loop. The only thing one cog can do to another is to reset it completely, forcing it to stop running. All the registers in the cog are cleared and the cog's memory can't be accessed. If you start up the cog, the entire contents of the cog's memory gets replaced.

    That said, as Stefan mentioned, you can write your loops so they'll stop when any of several conditions are met. There are also ways to use the ABORT statement and the "catch" operator ("\") in some circumstances for a subroutine to force a loop in its caller to exit to a higher call level.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-04-21 13:04
    It would look something like this
    con
      LED = 0 ' LED pin number
      BUTTON = 1 ' Button pin number
    
    dat
      prev long 0
    
    pub main
      repeat
        loop1
        loop2
    
    pub loop1
      repeat
        waitcnt(clkfreq/10+cnt)
        outa[LED] ^= 1
        if ButtonPush
          quit
    
    pub loop2
      repeat
       waitcnt(clkfreq/10+cnt)
        if ButtonPush
          quit
    
    pub ButtonPush | val
      val := ina[BUTTON]
      if val and not prev
        result := 1
      prev := val
    
  • Ed TEd T Posts: 50
    edited 2011-04-21 18:24
    You could also incorporate terminating the loop in the repeat with:
    pub loop1
        repeat while ButtonPush==0
            waitcnt(clkfreq/10+cnt)
            outa[LED] ^= 1
    

    Don't forget a Dira[LED]~~ also

    - Ed
  • kevinspacekevinspace Posts: 56
    edited 2011-04-21 22:57
    Thanks for everybody~~

    I have tried Dave's code and added the "dira[LED]~~" That to be successful.
Sign In or Register to comment.