For/Next Appropriate Usage

cvanb

Member
Join Date
Mar 2018
Location
PA
Posts
2
Hello, I trying to determine if the use of a For/Next Loop is appropriate for my application.

1.) The code in question will check the value of C517.
2.) If this value is 1 the code will check the value of controls C4 - C43.
3.) If the value is found to be 1, the PLC will set C(i) + 1 and C(i) - 1 equal to 1 as well. (i.e. If C5 = 1, set C4 and C6 =1).

Due to the nature of this, I will be using two for/next loops; one for both directions.

From an efficiency stand point, would this be better than having a rung for each command (78 total)..

Thanks for the help!
 
Does whatever you're doing have to be completed every program scan? If not just increment your loop index once per scan and let it run that way. PLC scan times are generally fast enough that you won't notice the time it takes for a full trip through the loop.
 
Hello, I trying to determine if the use of a For/Next Loop is appropriate for my application.

1.) The code in question will check the value of C517.
2.) If this value is 1 the code will check the value of controls C4 - C43.
3.) If the value is found to be 1, the PLC will set C(i) + 1 and C(i) - 1 equal to 1 as well. (i.e. If C5 = 1, set C4 and C6 =1).

Due to the nature of this, I will be using two for/next loops; one for both directions.

From an efficiency stand point, would this be better than having a rung for each command (78 total)..

Thanks for the help!

What PLC? Some PLCs do not allow iterating through bits.
 
Doing all the rungs individually will give you the fastest code execution. In fact, in some PC programs, the compilers sometimes "unroll" a loop that the programmer has written and turn it into individual statements, for precisely this reason. However, the unrolled version usually takes up more memory. Also, the speed increase is often negligible.

Doing the For/Next loop will likely be the fastest to program. This is definitely true if you need to make changes later (ie debug). You make the change in one place, not 78. The extra thing you have to test is that the bounds of the for/next loop don't cause you to go to high/low on your array.

Steve makes a good point that for PLCs, it is often better to run the FOR loop over multiple scans. That way it doesn't spike your scan time when you do the whole thing. This loop may be small enough that either way might be OK.
 
Does whatever you're doing have to be completed every program scan? If not just increment your loop index once per scan and let it run that way. PLC scan times are generally fast enough that you won't notice the time it takes for a full trip through the loop.

That's a good idea. However, this loop will only be executed when a fault occurs. The machine will not continue to run after the fault and the PLC will have some "extra time" to complete the scan. Running a for loop in one scan may also allow for easier readability. I am no means an expert at ladder logic, so please correct me if I am wrong.

What PLC? Some PLCs do not allow iterating through bits.

DL06 Micro PLC programmed with DirectSoft 5

Doing all the rungs individually will give you the fastest code execution. In fact, in some PC programs, the compilers sometimes "unroll" a loop that the programmer has written and turn it into individual statements, for precisely this reason. However, the unrolled version usually takes up more memory. Also, the speed increase is often negligible.

Doing the For/Next loop will likely be the fastest to program. This is definitely true if you need to make changes later (ie debug). You make the change in one place, not 78. The extra thing you have to test is that the bounds of the for/next loop don't cause you to go to high/low on your array.

Steve makes a good point that for PLCs, it is often better to run the FOR loop over multiple scans. That way it doesn't spike your scan time when you do the whole thing. This loop may be small enough that either way might be OK.

You've brought up some good points. Perhaps it may be helpful to program using a for/next loop and unroll the code once debugged (if found to be necessary).

The PLC I'm using has 7.5k of memory. I'm new to the game, is saving memory something I should pay close attention to?
 
You've brought up some good points. Perhaps it may be helpful to program using a for/next loop and unroll the code once debugged (if found to be necessary).

One of the nice things about loops is that it saves a lot of possibility of typos. If you're manually typing the whole thing, you would need to test every line.

I'd recommend doing one or the other (loop or all logic separate), don't plan to re-code it down the line.

The PLC I'm using has 7.5k of memory. I'm new to the game, is saving memory something I should pay close attention to?

Most of the time, you spend more time optimizing a system than it's worth. It is usually cheaper to upgrade to a bigger/faster CPU than the cost of the time it takes to reprogram something you've already done to be slightly better. That said, it isn't a bad practice to try to understand what is important before you start coding (cycle time vs memory usage vs programming effort).

If know you're right on the edge for memory, then it might be worth conserving. If this is the last thing you need to add or you have plenty to go, then it might not matter one way or the other. I've seen programmers spend a week trying to optimize and use the absolute minimum memory for something ("yay, we cut the memory in half!"), only to find out at the end of the project they only used 10% of the total. If they hadn't done the extra work, they'd have still been at 20% and have been just fine.

Personally, I don't know how much memory the code you described would take up on that platform. I've worked on PLCs from other brands that range from 25KB to 20MB, and different platforms compile to different amounts of memory. The best way to estimate is this: if you had to code 78 rungs of LAD, what % of the program do you think that would be?
 
Don't know what you are trying to do but a loop is better than 78 lines of code just about...every time :)
 
Running a for loop in one scan may also allow for easier readability. I am no means an expert at ladder logic, so please correct me if I am wrong.

One other thing to keep in mind is that you won't be able to monitor each individual pass through the loop (index = 1, index = 17, index =40, etc) to see exactly what is happening. You can usually only see either the 1st or the last loop, to check if the logic works at all.

Often times plant floor guys hate this. Easier for the programmer, harder for the maintenance guy. However, in most situations, the problem isn't the logic in the loop, but they want to watch the logic in the loop to see where the problem is. It's important to make sure they have another way to access that data either in the memory or on an HMI.

This might not be an issue for your specific application, but it definitely comes into play with For loops in general.
 
I'm not sure you can do what you want with the DL06 For/Next instruction. I believe it simply executes the loop the specified number of times, but I don't know if there is a way to link the loop index to the C address. Even if there is, DL06 addressing is octal while the loop index will probably be decimal making the conversion from the index to the address tricky. It might be better to use brute force and program the individual rungs.
As far as memory conservation is concerned, my experience has been that the PLC designers do a good job of matching the amount of program memory to the amount of I/O supported by the platform. When PLCs were new and I still had my hair memory was more of an issue than now.
 
Doing all the rungs individually will give you the fastest code execution. In fact, in some PC programs, the compilers sometimes "unroll" a loop that the programmer has written and turn it into individual statements, for precisely this reason.

If you have the time watch Prof. Brailsford Unrolling the Loops https://www.youtube.com/watch?v=guXgBe2wvEA
 

Similar Topics

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
What do you guys think of this representation for on/off contacts? C003 is on, then others are off. I have never seen the logic represented in...
Replies
5
Views
1,774
Is it possible to get a report about used digital inputs on GE Proficy? I want to set what inputs are available.
Replies
3
Views
1,050
Hello PLCS.net! Link here: https://www.rockwellautomation.com/en-us/products/software/factorytalk/whats-new.html Seems like Rockwell is actually...
Replies
33
Views
22,404
Back
Top Bottom