Electronics Projects: How to Use FOR Loops in PBASIC Code

The PBASIC FOR loop will make the BASIC Stamp in your electronics project execute a loop a certain number of times. It is also a great programming tool for performing an action on multiple I/O pins. Thus, a FOR loop is the ideal way to implement an LED Flasher program.


The basic structure of a FOR loop looks like this:


FOR counter = start-value TO end-value
Statements...
NEXT

Here’s an example that flashes the LED on pin 0 ten times:


X VAR BYTE
FOR X = 1 TO 10
HIGH 0
PAUSE 500
LOW 0
PAUSE 500
NEXT

In this example, the loop is executed ten times. The value of the variable X is increased by 1 each time through the loop.


In the preceding example, the program didn’t actually use the counter variable. That’s common in FOR loops; sometimes the only purpose for the counter variable is to control how many times the loop is executed. But you can use the counter variable within the loop. For example, here’s a loop that makes every I/O pin on the Stamp HIGH for one-tenth of a second:


IO_Pin VAR BYTE
FOR IO_Pin = 0 TO 15
HIGH IO_Pin
PAUSE 100
LOW IO_Pin
NEXT

Normally, the counter variable is increased by one on each pass through the loop. You can use the STEP keyword to specify a different step value if you want. When you use the STEP keyword, the basic structure of the FOR statement looks like this:


FOR counter = start-value TO end-value STEP step-value
Statements...
NEXT

For example, you could flash LEDs on just the even-numbered pins like this:


Led VAR Byte
FOR Led = 0 TO 10 STEP 2
HIGH Led
PAUSE 100
LOW Led
NEXT

Another interesting feature of FOR loops is that they can count backward. All you have to do is specify a start value that’s larger than the end value, like this:


Led VAR Byte
FOR Led = 10 TO 0 STEP 2
HIGH Led
PAUSE 100
LOW Led
NEXT

This version of the LED Flasher program uses a pair of FOR loops to flash the LEDs first in one direction, and then in the opposite direction. The first FOR loop flashes the LEDs on pins 0, 2, 4, 6, and 8. Then, the second FOR loop flashes the LEDs on pins 10, 8, 6, 4, and 2. Both FOR loops are contained within a DO loop that keeps the LEDs bouncing back and forth indefinitely.


' LED Flasher Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' back and forth, like Cylon eyes.
'
' This version of the program uses FOR loops.
' {$STAMP BS2}
' {$PBASIC 2.5}
Led VAR Byte
Main:
FOR Led = 0 TO 8 STEP 2
HIGH Led
PAUSE 100
LOW Led
NEXT
FOR Led = 10 TO 2 STEP 2
HIGH Led
PAUSE 100
LOW Led
NEXT
GOTO Main

When FOR loops are nested, the innermost loop(s) complete their entire cycle each time through the outer loop. If a FOR loop that repeats ten times is placed within an outer loop that repeats ten times, the statements within the innermost loop will execute a total of 100 times — ten times for each of the ten repetitions of the outer loop.


This one uses an outer FOR loop that varies the delay time for the PAUSE statements. The result is that the LEDs sweep very fast at first, but slow by 10 ms on each repetition of the outer loop until the delay reaches one second per LED.


' LED Flasher Program
' Doug Lowe
' July 10, 2011
'
' This program flashes LEDs connected to pins 0, 2, 4, 6, 8, and 10
' back and forth, like Cylon eyes.
'
' This version of the program uses nested FOR-NEXT loops to slow the
' sweeping motion of the LEDs.
' {$STAMP BS2}
' {$PBASIC 2.5}
Led VAR Byte
Speed VAR Word
FOR Speed = 10 TO 1000 STEP 10
FOR Led = 0 TO 8 STEP 2
HIGH Led
PAUSE Speed
LOW Led
NEXT
FOR Led = 10 TO 2 STEP 2
HIGH Led
PAUSE Speed
LOW Led
NEXT
NEXT










dummies

Source:http://www.dummies.com/how-to/content/electronics-projects-how-to-use-for-loops-in-pbasi.html

No comments:

Post a Comment