Exercise: State Machines
Overview
This exercise will show you how you are able to create a State Machine in LabVIEW. You will go through the general steps needed to create and then manipulate the State Machine.
Goal
Understand the basic mechanics of state machines in LabVIEW.
Description
This exercise consists of a series of tasks in which will walk you through how state machines are created and how to manipulate and control them.
Implementation
1. Launch LabVIEW and open a blank VI.
❑ Select File»New VI
2. Place a while loop on the block diagram.
❑ On the block diagram, under the View menu select Functions Palette
❑ Within the programming folder, select Structures » While Loop

3. Place a case structure in the while loop.
❑ Within the functions palette, select Structures » Case Structure

4. Create a shift register on the while loop.
❑ Right click the while loop
❑ Select Add Shift Register

5. Create an Enum and wire it to the shift register and case structure.
❑ Within the functions palette, select Numeric » Enum Constant
❑ Wire the Enum to the shift register input
❑ Wire the shift register output to the case structure conditional terminal

The Enum acts as a variable to contain the current state information. The case structure will contain the code that is to execute during the current state and to determine the next state.
6. Create a list of states contained in the Enum.
❑ Right click the Enum
❑ Select Edit Items
❑ Click Insert
❑ Type "First State"
❑ Press the <Enter> key to access the next position in the list
❑ Type "Second State"
❑ Click Ok

7. Create a button on the front panel to control the states.
❑ On the front panel, open the control palette by selecting View » Controls Palette
❑ Select Buttons » Rocker and place it on the front panel

When the button is in the false state, the state machine will be in the first state and when the button is in the true state, the state machine will be in the second state.
8. Create a LED on the front panel to indicate the current state.
❑ On the front panel, open the control palette by selecting View » Controls Palette
❑ Select LEDs » Round LED and place it on the front panel

9. Wire the button into the case structure to control the state transitions from the second state.
❑ On the block diagram, move the button control to the left side of the case structure within the while loop
❑ Select Second State from the case pull down menu at the top of the case structure
❑ Open the functions palette by selecting View » Functions Palette
❑ Select Programming » Comparison » Select and place it in the case structure
❑ Wire the button output to the S input of the select function

10. Specify which state to go to based on the buttons state for the second state.
❑ Right click on the Enum and select Create » Constant
❑ Move the constant into the case structure and wire it to the T input of the select function
❑ Click the down arrow on the Enum constant
❑ Select Second State
❑ Right click on the Enum and select Create » Constant
❑ Move the constant into the case structure and wire it to the F input of the select function
❑ Click the down arrow on the Enum constant
❑ Select First State
❑ Wire the select function output to right shift register

11. Use the LED indicator to show which state is the current state.
❑ Move the LED indicator to the right of the case structure within the while loop
❑ Connect the wire input into the S input terminal of the select function and the LED indicator

12. Configure the case structure to control the state transitions from the first state.
❑ Select First State from the case pull down menu at the top of the case structure
❑ from the functions palette select Programming » Comparison » Select and place it in the case structure
❑ Wire the button output to the S input of the select function
❑ Right click on the Enum and select Create » Constant
❑ Move the constant into the case structure and wire it to the T input of the select function
❑ Click the down arrow on the Enum constant and select Second State
❑ Right click on the Enum and select Create » Constant
❑ Move the constant into the case structure and wire it to the F input of the select function
❑ Click the down arrow on the Enum constant and select First State
❑ Wire the select function output to right shift register
❑ Wire the input into the S input terminal of the select function and the LED indicator

12. Create a delay in the while loop.
❑ From the functions palette select Programming » Timing » Wait
❑ Place a numeric constant from Programming » Numeric » Numeric Constant
❑ Type 100 into the numeric constant
❑ Wire the constant to the input of the wait function

12. Create a control to stop the while loop.
❑ Right click the input to the conditional terminal of the while loop
❑ select Create Control

When the VI is running the LED indicator will be lit when the state machine is in the second state and not lit when the state machine is in the first state. This is a basic example that can be expanded upon by inserting code in the case structure that will only execute when the respective state is the current state. Additional states can also be added by adding items to the Enum and corresponding cases to the case structure.
Tutorial Video State Machine Modules Home FIRST Community
Reader Comments | Submit a comment »
Pictures are not legible
I'd like to do the exercises, but the
resolution on these pictures is too low. I
can't read the text in the pictures and it
doesn't seem as though I can click
through to a larger version. Can you
update these pictures to make them
more legible?
- Robert Blinn, Portent Technologies. robertblinn@gmail.com - Jul 10, 2008
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
