ControlLogix For / Do Statement


Introduction to ControlLogix For / Do Statement

The ControlLogix For / Do Statement is similar to what you might think of as a FOR / NEXT loop. Basically, it is a construct that executes the same code a number of times. It does this by incrementing a tag.

For example, we might have an array of 32 tanks. We might use the For / Do construct to execute the same logic 32 times. Each time the code executes, it will increment the value of a tag. We can use this tag for an indirect address. In other words, the first time our code executes, we perform all the math on tank[0]. The next time our code executes we perform on logic on tank[1]. We would do this with indirect addressing. Instead of specifying which tank, we simply insert a tag as an array element. In this case, all of our logic might execute using tank[MyIndex]. MyIndex will increment by 1 each time our loop executes.

We must use extreme care when using this loop. The For / Do construct could greatly increase the scan time of your processor. If this happens, you could fault the processor. You would trip the watchdog timer.

Create Your Tags

We want to keep our example simple. For this reason, we will write some logic with no practical purpose. The only purpose of this logic is to prove how the construct works.

Let’s create a couple tags. “Tank” will have the data type of DINT[32]. Likewise, create “MyIndex” as a simple DINT.

Write the logic for the ControlLogix For / Do Statement

It’s important to realize, we need a structured text routine. I will create the structured text routine. After that, I will add a JSR statement into the main routine. This will cause the subroutine to execute.

Within the structured text routine, I’ll add the following logic. As I said before, this is just to prove how the logic works. Our goal will be to place the value of 1 into Tank[0]. Likewise, we’ll place the value of 1 into Tank[1], and so on.

In reality, you would have different logic that performs various calculations for each tank. Examples of this might be a tank level or volume.

As you can see, MyIndex will execute 32 times. Once the loop begins, MyIndex will start at 0. Therefore, Tank[MyIndex] refers to Tank[0]. We set this to MyIndex, which is zero.

The next time the loop executes, MyIndex will equal 1. Therefore, we refer to Tank[1] this time. Once gain, we set this element to MyIndex, which also has the value of 1.

The loop repeats 32 times PER LOGIC SCAN.

For more information on Indirect Addressing in ControlLogix, refer to this post.

Test your Work!

At this point, we’ll finalize all edits. After that, let’s go back to our tags. Expand your “Tank” Array, and let’s see if the loop worked as expected.

Obviously, our logic is working properly. One thing to consider is our scan time. Let’s go to the properties of the task. After that, we’ll click on “Monitor”.

It looks like our maximum scan time is about 23ms. Let’s go to “Configuration” to make sure we aren’t running close to a watchdog fault.

As you can see, our watchdog is set to 500ms. This is half of 1 second. We are probably not in danger of faulting the processor at this time.

Summary

In short, we use the FOR / DO statement to execute the same code a number of times. Usually, we’ll use this with arrays. We can use the index of the For / Do loop to specify which element to operate on each time the loop executes. Although we incremented the index by 1 each time, we can choose how much to increment the index for each execution. We must be careful to monitor our scan time of the task. If we execute the loop too many times, we could be in danger of faulting the processor. At the same time, we could slow down the processor from executing other important logic.

For more information, visit the ControlLogix Category Page!

— Ricky Bryce

Leave a comment

Your email address will not be published. Required fields are marked *