ControlLogix While / Do Statement


Introduction to the ControlLogix While / Do Statement

The ControlLogix While / Do Statement allows you to execute a block of code while a specific condition is true. This statement is available in Structured text. You can use this statement to perform large calculations on arrays. Some vendors might use structured text for complex algorithms. Another reason they use structured text is for developers to write code without the need of RSLogix or Studio 5000. Because of licensing, vendors might not be able to purchase a license for each developer. The developer can simply write the code in a simple text editor. After that, the person assembling all of the logic can paste the code into a structured text routine. Always remember that when you are writing code, someone else will need to troubleshoot a system. Always write your code in a way that is easy to read.

I would encourage you, however, to use the For / Do structure instead when possible. It’s important to realize the danger associated with this instruction. You can easily get the logic stuck in an infinite loop. You must take precautions against this. Additionally, someone else could modify your code at a later date. Their modifications could inadvertently cause an infinite loop. This would likely fault the processor.

The watchdog timer looks over your code. By default, if a task takes longer than 500ms to execute the processor will fault. You can configure the watchdog timer in the properties of a task.

Create the Structured Text Routine

In this case, I’ll simply add a structured text routine named “DoWhile” to the MainProgram.

Keep in mind that this is a subroutine. We must add a JSR Instruction to the MainRoutine so this subroutine will execute.

At this point, we are ready to write the code.

Create Your Tags

To demonstrate, I’ll simply create a task that counts to 11. In Controller tags, I’ll create a tag called TotalCounts. The Data Type of this tag is DINT.

Write your Logic

The logic for this loop will be very simple. The purpose of this logic is to show the structure of the Do / While loop. Keep in mind, the only purpose here is to count to 10.

Test your Code

At last, we’re ready to test your code. Let’s finalize all edits.

Let’s check our Controller Tag.

As you can see, our counts went to 11. Remember that our statement told the controller to execute the logic while the total counts are less than 11. Each time the loop executes, the TotalCounts increment. This loop does happens during the same processor scan. This is why it’s important to ensure we do not get the processor in an infinite loop. If you fail to break out of the loop, undoubtedly, the processor will fault.

Optionally, you can break out of the loop at anytime using the EXIT; statement.

Clearing a Processor Fault

As I have said before, if you fail to break out of the loop our system will shut down. Let’s consider the following code:

In this case, we will loop too many times, and the processor will fault.

Hit your pull down tab in the Online Toolbar where it displays the faulted status. Go to faults.

The Recent Faults message will give us an idea where the watchdog fault might have occurred.

Let’s look at our total counts tag:

As you can see, we didn’t even get near the value we would expect.

When this happens, clear the fault, fix your code, and go back to run mode.

Summary

To Summarize, the Do / While statement will execute a certain block of code while a condition is true. You must use extreme care when using this instruction. As I said before, it’s much better to use the For / Do block instead when possible.

For more information on ControlLogix, visit the category page!

— Ricky Bryce

Leave a comment

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