For-Next Loop Question

Heterodoxy

Member
Join Date
Mar 2003
Location
Northwestern Tenn.
Posts
30
Hello All,
I read so many posts where individuals utilize the practice of
for-next loops. And I was hoping to find some example code, so that I too could learn this technique.

Secondly, what are the advantages of writing one’s code at the word level? Is it main advantage for saving memory space? I try to use the K.I.S.S. principles in my writing. (I guess it’s my previous maintenance experience showing thru!)

I almost forgot to mention, but most of you know by now. I use AB products exclusively.

Sincerely
Heterodoxy
 
Last edited:
A FOR/NEXT loop construct is a basic feature of procedural programming languages. It allows you to repeat a section of code based on an index value which increments upon each exicution. It comes in handy especially when performing functions on arrays. An example to add two arrays, A & B together and store the result in another array C might read:


FOR N = 1 TO 10
C[N] = A[N] + B[N]
NEXT N



The SLC line does not have a FOR/NEXT instruction and you would have to use jumps (JMP) instructions to perform the same functionality as a FOR/NEXT loop.

BUT

This can get you into trouble if you mess up the end condition and you spend too much time jumping back in your code causing a watchdog timer fault. A much better way to achieve this in a SLC is to use a counter that increments each scan and performs the function one scan at a time (sort of like this)...


----------------(CTU C5:0 0 9)

C5:0/DN
--]/[--------------(MOV C5:0.ACC N7:0)

C5:0/DN
--]/[--------------(ADD N20:[N7:0] N21:[N7:0] N22:[N7:0])



In a procedural language such as Basic or C. The flow of program control is entirely driven by the code. If you want to stay in a loop for 5 minutes or 5 hours or 5 years waiting for a key press that is just fine.

In a PLC, if the flow doesn't reach the end of the program in a pre-determined amount of time (the watchdog timer preset time) then you fault the processor and everything stops. In a PLC, I always try to think of the control as falling through my code. Don't know if this analogy helps anyone else but it seems to make sense to me.

Not sure about CLX or PLC5 since I haven't ever used them...
 
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.

fornext.jpg
 
Thanks Norm & Ron

I’m using a SLC500 in my current project, so Ron your link and example where extremely informational. I have about 45 alarms that I’m monitoring. Would like to try incorporating Allen’s technique for handling them into the program. From this previous thread:

Alarm Code

I too had to print it out and study it for awhile (filed that one away for safekeeping) as many others have already said – It’s an excellent piece of work!

That post also is what inspired the second part of my question. About writing at word level – advantages?


Once Again Thanks Norm & Ron
 

Similar Topics

Hi all. (this is no homework) For a couple of day I've been practicing on a Mitshi plc. All went fine including data registers etc until I tried...
Replies
24
Views
6,061
Hello All, HMI application I'm using is GE's HMI/SCada Cimplicity. Im writing a basic program in the script to do as follows. When the operator...
Replies
33
Views
5,655
N
In a processor like the MicroLogix 1500 which does not have a built-in FOR..NEXT loop instruction (I've seen some web sites that indicate that...
Replies
26
Views
28,252
New2PLCs
N
I've got this start screen where the user has to enter their username and password. The username and password feature works fine. However, I want...
Replies
7
Views
1,365
Anyone know what the little green triangle on SCREEN 3 means ? See picture Thanks
Replies
2
Views
433
Back
Top Bottom