A state machine is a design pattern in computer programs or digital logic. This concept is used to simplify the problems in a manageable way. There are two types of state machines: finite and infinite state machines. The former is comprised of a finite number of states, transitions, and actions that can be modeled with flow graphs, where the path of logic can be detected when conditions are met.
We shall describe the design of the state machine by using our fictitious home automation system. When the owner of the house enters the house area then there is a gate in the yard. He needs to open it either showing a tag or by using a remote control. We shall build a model of the main gate of the yard. The gate can be in different states. These can be “CLOSED”, “OPENED”, “CLOSING”, “OPENING” and “UNKNOWN” states. The default state is the “CLOSED” state.
In most cases, we shall have a functional specification that describes how the system should work. The “OPENED/CLOSED” state is identified by using proximity sensors. When power is applied to the system, the gate can be in a partial open situation. We don’t consider it as a state since we have “OPENING/CLOSING” states. If the gate is in a partial open situation, we should close the gate. The system may not be able to close the gate due to obstacles that prevent to close it. We have a through-beam sensor that checks the line of sight. If the sensor can’t see the other side it indicates there is an obstacle in the system. One important point to note is that system can go to “OPENING” state directly from “CLOSING” state. This can happen when the through beam sensor can’t see the other side, in this case it will “OPENING” state.
We shall develop a state machine in PLC program in Structure text programming language. This can be executed in any structure text compatible PLC.