Programming in PBASIC 2.5 question
rusty009
Posts: 21
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
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
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.
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
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.