For/Next construction ...
the basic idea of a “For/Next” loop is to force the processor to go through a series of rungs over and over again for a certain number of times ... here is a very simple example of a program with a For/Next loop ...
[attachment]
Rung 0000 is outside the loop. Each time the processor scans ladder #2, the value of F8:0 will increase by one.
Rung 0001 is the first rung of the For/Next loop. The FOR acts as a target for the NXT. N7:0 is used as an "Index" to count how many times the loop has been executed. In this example, the value of N7:0 will increase by one (the Step Size) each time the FOR is executed. Programmers often refer to this operation as “incrementing the index”. Notice that we could have specified a different value for the Step Size. This ability comes in very handy when we want to do something such as (for example) “count by five” in a data array. The Initial Value and the Terminal Value determine the starting point and ending point for the Index. The best way to get a feel for these is to experiment until you see how they work.
Rung 0002 is inside the For/Next loop. Because of the For/Next action, each time the processor scans ladder #2, the value of F8:1 will increase by four. In this simple example, we only have one rung inside the loop. In actual practice the loop might include many rungs. We could also clean this up by putting a single JSR rung here – and then programming as many rungs as necessary in a separate subroutine file. This would NOT make the program any more efficient – but it might make it easier for our maintenance technicians to recognize and understand the “For/Next looping” action. Constructions like this can be very confusing – especially when we cannot see the FOR and the NXT on the screen at the same time.
Rung 0003 is the last rung of the For/Next loop. When the processor executes this rung, the scan is sent backwards to the FOR in Rung 0001 – the FOR then compares the current value of the Index to the Terminal Value to see if the loop has finished. If the loop is not finished, then the FOR will send the scan back through the loop again. If the loop has finished, then the FOR will send the scan to the next rung in the ladder PAST the NXT. The Index will automatically reset back to the Initial Value to get ready for the next scan.
When we run this sample program:
The value in F8:0 will constantly increase. Basically it is just counting the total number of scans which the processor has made.
The value of F8:1 will also constantly increase – but it will increase four times as fast as the value in F8:0. That is because of the “For/Next” action. It forces the processor to go back and execute Rung 0002 four times during each processor scan. (Note that in the picture 6730 * 4 = 26920).
The Allen-Bradley PLC-5 processors have this For/Next construction available in their instruction set. Smaller processors (such as the SLC-500 family) do not support this construction. To “roll your own” for those types of systems, you might want to take a look at:
a recent thread on “recursive” loops
In any case, just make sure that you don’t stay inside any loop too long! Most processors have a “watchdog” timer which monitors the length of time it takes to complete each scan. If the timer runs out then the processor will fault.