User`s manual

6.0 - PLC Programs
Page - 59
6.4 - Conditional Statements
Most action in a PLC program is conditional, dependent on the state of PMAC variables, such as inputs, outputs, positions,
counters, etc. You may want your action to be level-triggered or edge-triggered; both can be done, but the techniques are
different.
6.4.1 - Level-Triggered Conditions:
A branch controlled by a level- triggered condition is easier to implement. Taking our incrementing variable example and
making the counting dependent on an input assigned to variable M11, we have:
IF (M11=1)
P1=P1+1
ENDIF
As long as the input is true, P1 will increment several hundred times per second. When the input goes false, P1 will stop
incrementing.
6.4.2 - Edge-Triggered Conditions:
Suppose instead that you only want to increment P1 once for each time M11 goes true (triggering on the rising edge of M11
sometimes called a "one-shot" or "latched"). To do this, we must get a little more sophisticated. We need a compound
condition to trigger the action, then as part of the action, we set one of the conditions false, so the action will not occur on
the next PLC scan. The easiest way to do this is through the use of a "shadow variable", which will follow the input
variable value. Action is only taken when the shadow variable does not match the input variable. Our code could become:
IF (M11=1)
IF (P11=0)
P1=P1+1
P11=1
ENDIF
ELSE
P11=0
ENDIF
Notice that we had to make sure that P11 could follow M11 both up and down. We set P11 to 0 in a level-triggered mode;
we could have done this edge-triggered as well, but it does not matter as far as the final outcome of the routine is concerned,
it is about even in calculation time, and it saves program lines.
6.5 - WHILE Loops
Normally a PLC program executes all the way from beginning to end within a single scan. The exception to this rule occurs
if the program encounters a true WHILE condition. In this case, the program will execute down to the ENDWHILE
statement and exit this PLC. After cycling through all of the other PLCs, it will re-enter this PLC at the WHILE condition
statement, not at the beginning. This process will repeat as long as the condition is true. When the WHILE condition goes
false, the PLC program will skip past the ENDWHILE statement and proceed to execute the rest of the PLC program.
If we want to increment our counter as long as the input is true, and prevent execution of the rest of the PLC program, we
could program:
WHILE (M11=1)
P1=P1+1
ENDWHILE
This structure makes it easier to "hold up" PLC operation in one section of the program, so other branches in the same
program do not have to have extra conditions so they do not execute when this condition is true. Contrast this to using an IF
condition (see above).