Do Loop
dbrose
Posts: 7
For my project in school I am writing a program that requires loops, but I need to specify the number of repetitions, does anyone know how to make a 'Do-loop' with a specific number of repetitions?
Comments
Here's what someone else replied to a similar question. The program code is posted below their answer
You could use a DO.. WHILE LOOP and a variable to count the number of times the loop executes. I have attached a piece of sample code that will enter a DO...WHILE LOOP if the status pin equals zero. While in the DO...WHILE LOOP, the "TimeOut" Variable increments every time the loop is executed. The "IF" statement breaks the loop if the timeout variable accumulates a value of 20000. The reason I chose 20000 for the timeout is because the loop pauses for one millisecond, therefore, the loop will be exited after approximately 20 seconds.
Program code:
' {$STAMP BS2p}
' {$PBASIC 2.5}
Status PIN 0
TimeOut VAR Word
TimeOut = 0
DO WHILE Status = 0
IF TimeOut = 20000 THEN EXIT
PAUSE 1
TimeOut = TimeOut + 1
LOOP
DEBUG "Status Pin HIGH. Connection established", CR
END
Hope this helps,
Mark
ARLISS Team New Hampshire
:cool:
You could also you a FOR...NEXT loop to accomplish this task. You can read up on this and the DO...WHILE loop in the BASIC Stamp Syntax and Reference Manual.
Best of luck!
-- Jessica