For Next loop

Eric01234

Member
Join Date
Feb 2014
Location
Leicestershire
Posts
23
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 the "for next" command. I wrote 2 very simple loops each to start with the same input say X0 for a k of 5. I would have thought that the first loop needs to be run 5x before the second loop starts. But instead both loops are running at the same time.

Where do I go wrong?
 
0 ------------FOR K5
3 X0---------------Y0
5 ---------------NEXT
6 ------------FOR K5
9 X0---------------Y1
11 ---------------NEXT

My idea is that when I toggle X0 ten times, first Y0 will energise 5x and then Y1 5x.
I know that the plc executes all commands simultaneously, but how else can I use “For Next” ?

Thanks for any input.
 
I would have thought that the first loop needs to be run 5x before the second loop starts. But instead both loops are running at the same time.

Where do I go wrong?

0 ------------FOR K5
3 X0---------------Y0
5 ---------------NEXT
6 ------------FOR K5
9 X0---------------Y1
11 ---------------NEXT

My idea is that when I toggle X0 ten times, first Y0 will energise 5x and then Y1 5x.
I know that the plc executes all commands simultaneously, but how else can I use “For Next” ?
Looking at the Mitsusibishi Programming Manual, your For-Next loops will run 5 times. The problem is that you have no way to see that Y0 flickered ON 5 times. Most likely the scan was so fast that your eyes saw Y0 go ON 1 time, and Y1 go ON 1 time.

If you put a timer in each loop to turn on Y, then wait 2 seconds, then use the Timer bit to trigger the NEXT, then you should see it go on 5 times and stay on each time for 2 seconds.

If you are trying to find a way to flash lights, then there are better ways.
 
Last edited:
0 ------------FOR K5
3 X0---------------Y0
5 ---------------NEXT
6 ------------FOR K5
9 X0---------------Y1
11 ---------------NEXT

My idea is that when I toggle X0 ten times, first Y0 will energise 5x and then Y1 5x.
I know that the plc executes all commands simultaneously, but how else can I use “For Next” ?

Thanks for any input.

I believe the syntax is correct for a Mitsubishi PLC. It is probably running correct too although he's not knowing it.

It will loop through the first loop 5 times each scan and examine if X0 is on and turn on Y0 if it is.
Then it will loop through the second loop 5 times each scan and examine X0 again and turn on Y1 if appropriate.

I'm guessing when it's running the Y0 and Y1 outputs just follow the X0 input condition.
 
My idea is that when I toggle X0 ten times, first Y0 will energise 5x and then Y1 5x.
Since the physical I/Os are updates only between program scans, these loops make no sense.
The result of execution will be the same regardless of the number of passes, and will be reflected on the physical output after the scan end.

Most likely the scan was so fast that your eyes saw Y0 go ON 1 time, and Y1 go ON 1 time.
Not because of the scan time, as explained above.
Physical Y really could update only once per scan.

If you put a timer in each loop to turn on Y, then wait 2 seconds, then use the Timer bit to trigger the NEXT, then you should see it go on 5 times and stay on each time for 2 seconds.
This is impossible the way you suggest, but anyways so long loop will time out the watchdog timer, and the PLC will stop by scan overtime.
The result on the output will be the same though: update only once per scan.


I know that the plc executes all commands simultaneously...
Absolutely wrong. It executes instructions as it scans them, in order.
This is the cornerstone difference between PLC operation and hardware circuits.

... how else can I use “For Next” ?
For anything, that does not need to wait for the program scan end to update the result.
That is, not for physical outputs (unless they are refreshed immediately by a dedicated instruction).
 
Last edited:
Hi I would use counters, pseudo code below

Look to use a counter with a PLS INC to add to count1 each time X0 is turned on.

When count=10 set a bit (say M0)

When M0=1 use the timer second flag to operate the lamps on/off every second

M0 and timer second and count2 <=5 then Y0
and pls inc count2

M0 and timer second and count2 <=5 then Y1
and pls inc count2

If count2 >=10 then reset M0 and clear count1 and count2
 
As others have indicated, the For Loop does not work like the OP expects it to. The idea of a FOR loop in ladder is that the entire loop is performed in one scan.

I rarely use a For loop on a PLC. But when I do it is usually to perform a math operation or transfer structured but non-contiguous memory areas from one place to another. Say I have a 6 axis of motion and use the For loop to index the axis number and re-invoke a subroutine or function block to process that axis. Again, only if there is a need to process all in one scan. Otherwise I would just index the axis once per scan and expect a full refresh after an "axis number" of scans.
 
Thanks a lot guys. That was really helpful.

I sort of started to get an inkling of what was happening before I posted, but without you lot I would’ve always had my doubts.

My program was purely for educational purposes.

I’ll be back. Thanks again. 🍺
 
The idea of a FOR loop in ladder is that the entire loop is performed in one scan.
Sorry, but if you put in a timer that makes the NEXT wait for 2 seconds, only one loop will be performed in each scan, just like any other event that uses a timer. Seeing is believing, try it yourself.
 
Sorry, but if you put in a timer that makes the NEXT wait for 2 seconds, only one loop will be performed in each scan, just like any other event that uses a timer. Seeing is believing, try it yourself.

hmmmm Lancie I don't have a mitsi PLC handy to try it myself but I wound expect the for next loop to not care if the timer has elapsed

IMHO The for/next is independent of a timers status

Wait to be corrected :)
 
Sorry, but if you put in a timer that makes the NEXT wait for 2 seconds, only one loop will be performed in each scan, just like any other event that uses a timer. Seeing is believing, try it yourself.

Lancie, if you put something inside the For Loop that would cause it to "wait" for 2 seconds, you will likely cause it to trip the watchdog since you will have prevented it from finishing the scan.

If you have it sample a timer function within the For Loop the timer will be burnt up that much quicker, in the same fashion as if you had duplicate references to the same timer function per scan.

I think it also is makes sense to state which controller and software you believe exhibits the behavior you claim. I have never used LogixPro, or whatever it is you use, but I have not seen the behavior you claim on a real PLC.

Try it yourself.
 
Sorry, but if you put in a timer that makes the NEXT wait for 2 seconds, only one loop will be performed in each scan, just like any other event that uses a timer. Seeing is believing, try it yourself.

The NEXT statement normally doesn't allow any conditions on it's rung so you can't really pause the FOR/NEXT loops with a timer.
 
The NEXT statement normally doesn't allow any conditions on it's rung so you can't really pause the FOR/NEXT loops with a timer.

I'm not sure where he was coming from with the timer. Once the For Loop is invoked, whether the call to it was conditional or not will not stop it from completing the loop.

If he places a timer contact within the subroutine the loop calls (Assuming the AB version of the FOR loop) it will evaluate the logic in the same fashion it normally would.

If you place something within the loop that either prevents it from continuing for a time greater than the watchdog (easy to do in a structured text subroutine) then it will simply fault on the watchdog.

As an aside, that also reflects the "handle with care" nature of the LOOP instructions. If you pile them with tons of code, and put a large number of iterations on them you can cause some pretty severe scan times (or scan overtimes).
 
IMHO The for/next is independent of a timers status
If you use JMP to jump out of the For Next loop until the timer expires, then it will allow pausing as long as desired before completing the NEXT. Also CJ and SCJ jumps around the FOR-NEXT, and STOP and FEND will end the loop early if needed.
 

Similar Topics

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,682
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...
Replies
5
Views
6,200
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,586
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,438
Anyone know what the little green triangle on SCREEN 3 means ? See picture Thanks
Replies
2
Views
457
Back
Top Bottom