Introduction
A state machine helps control machines step-by-step by remembering what happened before. It makes sure the machine does tasks in the right order.
Use ladder logic rungs with contacts and coils to represent states. Use internal bits or memory bits to store the current state. Use conditions to move from one state to another by setting and resetting bits.
State1: --[ ]--( )-- // If condition true, turn ON State1 coil State2: --[ ]--( )-- // If condition true, turn ON State2 coil
Rung1: --[ State1 ]--[ Condition ]--( State2 )--
--[ State1 ]------------------( /State1 )--Rung1: --[ State2 ]--[ Condition2 ]--( State3 )--
--[ State2 ]------------------( /State2 )--(* Simple 3-state machine example in ladder logic style pseudocode *) (* Memory bits: State1, State2, State3 *) (* Inputs: Start, Next *) // Initial state: State1 ON State1 := TRUE; State2 := FALSE; State3 := FALSE; // Transition from State1 to State2 when Start is pressed IF State1 AND Start THEN State1 := FALSE; State2 := TRUE; END_IF; // Transition from State2 to State3 when Next is pressed IF State2 AND Next THEN State2 := FALSE; State3 := TRUE; END_IF; // Transition from State3 back to State1 when Next is pressed IF State3 AND Next THEN State3 := FALSE; State1 := TRUE; END_IF; // Output current state IF State1 THEN OUTPUT := 'State1 Active'; ELSIF State2 THEN OUTPUT := 'State2 Active'; ELSIF State3 THEN OUTPUT := 'State3 Active'; END_IF; // Simulate pressing Start and Next inputs step by step Start := TRUE; Next := FALSE; (* Run transitions *) IF State1 AND Start THEN State1 := FALSE; State2 := TRUE; END_IF; Start := FALSE; Next := TRUE; IF State2 AND Next THEN State2 := FALSE; State3 := TRUE; END_IF; IF State3 AND Next THEN State3 := FALSE; State1 := TRUE; END_IF; // Final output IF State1 THEN OUTPUT_FINAL := 'State1 Active'; ELSIF State2 THEN OUTPUT_FINAL := 'State2 Active'; ELSIF State3 THEN OUTPUT_FINAL := 'State3 Active'; END_IF; // Print final output PRINT(OUTPUT_FINAL);
--[ State1 ]--+--[ Condition ]--( State2 )
|
+--( Reset State1 )Condition is TRUE?--[ State1 ]--+--[ Condition ]--( State2 )
+--( State1 )Pass the test and this pattern moves unknown