Shop OBEX P1 Docs P2 Docs Learn Events
Do Loop in spin? — Parallax Forums

Do Loop in spin?

10gigbill10gigbill Posts: 79
edited 2010-02-11 16:19 in Propeller 1
I have a 9600 baud stream party-lined to several (5) props
Each prop has a one character “address” (unique char only used for addressing) that will have a 5 character command following for that address. I want to have a timer in the prop so that once it sees it’s address and starts to collect it’s 5 characters it will not hang if the party-line is interrupted. That is it should receive the characters in say .1 seconds. If not it should go back to looking for a new command string.
As soon as I see my address I start the timer and collect the next
5 characters while timer is not timed out. Do while….
BUT AAAARRRRGGGGGG…. There is no Do loop …..?

Sorry I’m new at this spin thing

Thank you for your support.

Bill

Comments

  • RossHRossH Posts: 5,519
    edited 2010-02-11 05:28
    Bill,

    In SPIN, the REPEAT loop does the job of all the usual DO/WHILE/UNTIL/FOR loop constructs. Check page 293 of the Propeller manual (v1.01).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • 10gigbill10gigbill Posts: 79
    edited 2010-02-11 05:47
    OK thanks... It's page 188 in prop manual (v1.1)
    I'll give this a try and maybe by tomorrow I'll get a little farther down the line on my project.

    Repeat (while...) hummmm......


    thanks
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-02-11 07:21
    What's the difference between a while loop and a do loop?

    The condition is checked at the beginning in one case and it's checked in the end in the other case.

    So, to turn a while loop into a do loop you'd simply choose a condition which is true in the first iteration.

    cond:=-1
    repeat while cond
      ' some code
      cond:= 'your condition comes here and must be 0 to exit the "do"-loop
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-02-11 16:19
    I have used that method to implement do loops as well.· It can get a little complicated if your loop contains "next" statements (same as C continue).· You would have to compute the new value of "cond" before each next statement.

    A slightly modified method is to OR a first-time flag with the normal "do" condition as follows:

    first := true
    repeat while first OR <condition statement>
    · first := false
    · ...
    · next
    · ...
    · next
    · ...

    Another thing is that "repeat from" loops always execute at least once.· Also the "step" value is not the actual increment used, but it is a multiplier of the computed step value.· The following examples yields unexpected results unless you understand how the step value is used.

    for i from 0 to -10 step -2

    This will execute once with the final value of i being 2.· If I wanted to step through values of 0, -2, -4, -6, -8 and -10 I would need to use the following statement.

    for i from 0 to -10 step 2

    Also, a C statement of "for (i = 0; i <= num; i++)" does not translate directly to "repeat i from 0 to num".· If num is negative Spin will decrement "i" instead of incrementing it.· The C statement would need to be written in Spin as follows

    if (num => 0)
    · repeat i from 0 to num
    ·
Sign In or Register to comment.