The fear of FOR TO loops

3 posts in this topic

Hi everybody,

For a while I’ve been wondering, in every plc forum where the use of loops come to discussion I find most people answering that it’s best to stay away from them and that there are better, easier and mostly less scan time demanding solutions out there. Why is that?

In my experience they’re the next best thing.

Why would referencing data from an array of 40 Dwords using a FOR X:= 0 TO 40 DO loop be any more scan time demanding then the PLC scanning through 40 rungs of statments?

Just curious if I’m missing something here. 


Share this post

Link to post
Share on other sites

The biggest objection I have to loops in the PLC is that they're harder to diagnose later. I would say if you're using loops, be meticulous and over-zealous in your documentation. Explain every detail of what you're doing and why so a technician on 3rd shift won't have any issues figuring it out 5 years from now. Without having to call an engineer.

From a programming standpoint, just be careful when designing your loops to properly limit check them so you don't run past the end of your array. Most production managers don't have much of a sense of humor when it comes to faulting processors. Writing the loop is often simpler to avoid typos and copy-paste errors than having a rung for each element. If you have 40 items, it may not be a huge deal to have a rung for each, but when your array is a couple hundred or a thousand items, you almost have to do a loop just to stay sane.

As an end user (not an OEM) who has to support the code in the long term, I will sacrifice memory/performance every time for ease of troubleshooting except in a few limited scenarios where the loop is doing something that's not critical to the machine's operation and that's unlikely to require diagnostics later. Like looping through alarm messages to display on an HMI.

War story: we had a contractor come in and create a very complex piece of code for us that has multiple nested loops that are almost impossible to untangle. It works great...until something goes wonky and we have to download an old copy of the program into the PLC to get it working again. Power-cycling the PLC doesn't fix it. There's some tag value somewhere that makes it not work. After several years of it happening every few months, we're no closer to identifying the pattern. I spent several days one time just going through the code and making a flowchart but wasn't able to reproduce the bug. Weirdly enough, we have the identical code running in 3 lines in 3 different processors. One of them (originally a ControlLogix -L55M12 at v16 and now an -L81E at v30) has never gone wonky, another one (ControlLogix -L71, v24) goes wonky every other month or so, and the third (CompactLogix -L33ER, v24) has only gone wonky twice in 1.5 years or so. I've run comparison after comparison between the systems and haven't been able to figure out why that would be.

In my opinion, loops definitely have their place, but PLEASE document them so I don't get a phone call at 3AM.

Share this post

Link to post
Share on other sites

There's a reason QuickBASIC wasn't ported to windows.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now