State diagrams

From Flowcode Help
Revision as of 08:54, 7 October 2020 by BenR (talk | contribs)
Jump to navigationJump to search

About state diagrams

State Diagram Overview


State machines allow a programmer to construct an electronic system based on states and transition between states rather than in a conventional scripted or flowchart paradigm.

State machines are really useful in some circumstances where a very clear visual representation of an algorithm can be developed showing how a system behaves when it is in certain states and certain actions occur. State machines are particularly useful in communication systems where handshaking between systems is needed. A state machine can be a part of a program or can be the entire program.

Flowcode Embedded and Flowcode App Developer both have a facility to incorporate state machines in their programs using a state diagram macro.


To start a new state diagram

Select USER MACROS…NEW STATE DIAGRAM.

This creates a state diagram macro. A new tab will appear in the workspace next to the other macro tabs you have.


Blank State Diagram


At this point you will notice that the Command Icons have changed and you can drag 5 new icons onto the diagram. These icons are:


State Diagram Icons


States

States: there are only three types of state: an Entry state – all state diagrams have a single point of entry - a normal state and an Exit state. Not all state diagrams have an Exit state but a state diagram can have more than one Exit state.

States test each connected Transition in turn and go to the first one that has a 'true' condition. Normal States can be orphaned (have no connected Transitions), in which case they will never be entered or executed. Normal States are allowed to have no exiting Transitions, in which case program execution will stay held at that state (repeating its process functionality if it exists).


Transitions

Transitions: these can be straight or curved. The only difference is how they look. All other properties are the same. Transitions are the links between states.  Each “tick” (e.g. when pressing ‘step’ during debug), the current state’s functionality will be executed.  The next “ticks” will evaluate each exiting Transition’s condition from the current state.  When a Transition’s condition is true, that Transition’s functionality is executed, and then subsequent “tick” will see the functionality of the next state executed.


Comments

Comments: these have no function, they just allow you to comment your state diagram.


To use a state diagram in your program

To make use of the state machine engine in Flowcode you first design your state diagram macro and then you simply call it as you would call any other macro in Flowcode.


Calling State Diagram


Designing a basic state machine

States have optional functionality which is repeated each time the simulation is (or remains) on that state.  Transitions have a condition which needs to be true for them to be executed, and also have optional functionality which is executed once when the Transition’s condition is true.

Once you have dragged a state icon and a Transition line onto the Macro workspace you can use your mouse to connect a Transition to a state.


Connecting a State and Transition


Altering the functionality of a state icon

To alter the functionality of the state icon or transition you either click twice on it or you right click and select Properties. This allows you to define the function that takes place as the program enters the state. There are four properties you can enter:

Display Name

Display name: The name of the icon that is displayed in the icon graphic

Function

Function: The function that is executed on program entry. This is one of four functions:

  • Do nothing: no action.
  • Call Macro: call a Macro routine of your choice.
  • Calculation: execute a calculation.
  • Delay: execute a delay.
  • C code: Execute some C code (Flowcode Embedded only).


Set Transition Order

Set Transition order: allows the user to order the exiting transitions from a state – i.e. the order in which they are evaluated.  This order is shown whenever a state with multiple exiting transitions is selected (as numbers where the transitions exit the state).  The order is also changed whenever a transition is connected to exit the state.

Details

Details: Displays parameters for one of the above Functions.


Example

The first example is interesting from a how to perspective because it contains different approaches to perform the same functionality (i.e. flashing an LED while a switch is held down). The first two are state diagrams – one with states as dumb placeholders and one with states performing the functionality, and the third is a flowchart for completeness.

FC6 Icon.png State Diagram LED Flasher Example


Example program showing how to parse through a string a character at a time with a variable delay. Any numeric value in the string will be presented on the 7-segment display. Any none numeric value will be automatically skipped.

FC6 Icon.png State Diagram Number Ticker Example


Example program showing how to create an alarm system. The state diagram monitors the value of the analogue potentiometer and if the value goes above 150 then the alarm is triggered and the PortB LEDs will flash on and off. The Alarm is deactivated using an interrupt on change interupt which is triggered using PortA pin 4.

FC6 Icon.png State Diagram Warning System Example