Shop OBEX P1 Docs P2 Docs Learn Events
Programming in PBASIC 2.5 question — Parallax Forums

Programming in PBASIC 2.5 question

rusty009rusty009 Posts: 21
edited 2009-01-29 11:54 in BASIC Stamp
Hey, I am trying to write some code that will loop UNTIL a certain criteria is met. so I have my variable x, and I want to loop until x is between values 20 - 40. How would I go about doing this ? Thanks in advance.

Comments

  • CoffeeAddictCoffeeAddict Posts: 3
    edited 2009-01-28 21:10
    use a do while loop. It's general syntax looks like this:

    DO { WHILE | UNTIL condition(s) }
    Statement(s)
    LOOP

    or if you need to post-test the variable use

    DO
    Statement(s)
    LOOP { WHILE | UNTIL condition(s) }

    So, your specific example would look something like

    x VAR Byte

    DO WHILE (x < 40 AND x > 20)

    whatever you need done goes here

    LOOP


    This is actual code I compiled and ran. Try it out and see if it works. It shows you how you can get out of the loop by picking the correct value for x.


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    x VAR Byte

    DEBUG "Please enter a value for x: "
    DEBUGIN DEC x

    DO WHILE ( x > 40 OR x < 20 )

    DEBUG "Inside Loop", CR
    DEBUG "Please enter a value for x: "
    DEBUGIN DEC x

    LOOP

    DEBUG "Out of Loop"

    END
  • rusty009rusty009 Posts: 21
    edited 2009-01-29 08:48
    Thanks a lot!
  • rusty009rusty009 Posts: 21
    edited 2009-01-29 09:19
    Ok another question, heres my code,

    controlv VAR Byte

    FOR controlv =1 TO 50

    PULSOUT 13, 850
    PULSOUT 12, 650


    NEXT

    I want to repeat the code in my loo 51 times, but it doesnt seem to be working, can anyone see whats wrong with it? Thanks in advance,

    Paul.
  • Carl HayesCarl Hayes Posts: 841
    edited 2009-01-29 11:34
    Since you don't tell us in what way it "doesn't seem to be working", one must assume that you're bothered because it executes the code in the loop (the two PULSOUTs) only 50 times (which is what you specified) rather than 51 times (which you say you want). Or is there some other problem?

    Would you take your car to a repair shop and, when asked what you'd like repaired, say only that the car "doesn't seem to be working"?· Wouldn't you say a little more than that, so the mechanic would have some idea what the problem was?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    · -- Carl, nn5i@arrl.net

    Post Edited (Carl Hayes) : 1/29/2009 11:39:27 AM GMT
  • rusty009rusty009 Posts: 21
    edited 2009-01-29 11:47
    Hey ok, the pulsouts arnt working, heres my code,

    DO WHILE (MyByte > 40 OR MyByte < 20 )
    'Move forward
    PULSOUT 13, 850
    PULSOUT 12, 650

    'begin sensing
    (sensing code is here)

    LOOP

    'begin second loop after the first white line has been detected
    FOR controlv = 0 TO 50
    debug "hello" 'this debug is only here to test if the loop has been executed
    PULSOUT 13, 850
    PULSOUT 12, 650


    NEXT

    first loops drives the motor forward until a signal between 20 and 40 is returned to the robot ( I have left the sensing code out because I believe it is not relevant) This loops executes fine. The problem is after the value between 20-40 is met the boe-bot just stops, and the pulsouts in the loop don't initiate, so the robot just stops after a value of 20-40 has been met. The loop is definately executed because hello is returned to the screen form the DEBUG inside the for loop but for some reason the pulsouts are not executing. Thanks in advance.
  • rusty009rusty009 Posts: 21
    edited 2009-01-29 11:54
    hey, nevermind, I changed the value from 50 to 100 and now it works fine.
Sign In or Register to comment.