(* Rising Edge / One-Shot: P and N contacts — Control Panel scene X1 = Start (N.O.) X2 = Stop (N.C., TRUE when not pressed) X3 = Jog button, used here as the Count button X4 = Reset Counts (click it on the I/O tab) Y1 = Motor run light IEC 61131-3 has no P or N contact in ST. The R_TRIG and F_TRIG function blocks do the same job: Q is TRUE for one scan on the edge. *) VAR CountRise : R_TRIG; // rising edge of the Count button CountFall : F_TRIG; // falling edge of the Count button StartRise : R_TRIG; // rising edge of Start StopPress : F_TRIG; // Stop is N.C., so a press is a FALLING edge LevelCount : INT; // the bug: adds 1 every scan X3 is held PressCount : INT; // the fix: one count per press ReleaseCount : INT; // one count per release StopCount : INT; // Stop presses MotorOn : BOOL; // push on / push off state END_VAR (* THE BUG — a plain IF runs every scan the button is held. Hold it for 4 s at a 100 ms scan and LevelCount jumps by 40 *) IF X3 THEN LevelCount := LevelCount + 1; END_IF; (* THE FIX — R_TRIG.Q is TRUE for one scan when X3 goes FALSE to TRUE *) CountRise(CLK := X3); IF CountRise.Q THEN PressCount := PressCount + 1; END_IF; (* F_TRIG.Q is TRUE for one scan when X3 goes TRUE to FALSE *) CountFall(CLK := X3); IF CountFall.Q THEN ReleaseCount := ReleaseCount + 1; END_IF; (* PUSH ON / PUSH OFF — each Start press flips the motor. Stop (N.C.) always turns it off *) StartRise(CLK := X1); IF StartRise.Q THEN MotorOn := NOT MotorOn; END_IF; IF NOT X2 THEN MotorOn := FALSE; END_IF; Y1 := MotorOn; (* STOP PRESSES — pressing an N.C. Stop turns X2 OFF: a falling edge *) StopPress(CLK := X2); IF StopPress.Q THEN StopCount := StopCount + 1; END_IF; (* RESET COUNTS *) IF X4 THEN LevelCount := 0; PressCount := 0; ReleaseCount := 0; StopCount := 0; END_IF;