Statechart Module Tutorial Part 3: Defining and Modifying Statechart Data Types
- Updated2023-02-21
- 8 minute(s) read
In Part 2 of this tutorial, you created the beginnings of a statechart and learned how to find errors on the statechart diagram. In Part 3 of this tutorial, you define types of data upon which the statechart acts. You also configure the statechart to modify some of this data.
![]() |
You can complete these exercises in approximately 45 minutes. |
![]() |
Note Refer to the labview\examples\Statechart\Tutorial\Asynchronous\Getting Started 3.lvsc for a completed version of the statechart you create in this part of the tutorial. |
Defining the Type of Output Data
If you want the statechart to modify data, you first must define the type of data. This process involves creating the controls and/or indicators that represent the statechart data.
Complete the following steps to define the statechart output data. Later in this part of the tutorial, you configure the statechart to modify this data.
- Open the My Getting Started Statechart.lvsc statechart that you constructed in Part 2 of this tutorial. You also can navigate to the labview\examples\Statechart\Tutorial\Asynchronous directory and open Getting Started 2.lvsc for the completed statechart from Part 2 of this tutorial.

Note Recall that the Getting Started 2.lvsc statechart appears in a Project Explorer window containing the necessary support files for the statechart. - Double-click the Outputs.ctl item. LabVIEW displays the Control Editor window for this type definition.
- Notice the numeric control Numeric inside the Outputs cluster. Right-click this control and select Change to Indicator to change this control to a numeric indicator.
- Right-click the Numeric indicator and select Replace»Express»Text Indicators»String Indicator from the shortcut menu to replace the numeric indicator with a string indicator.
- Double-click the String label and enter Current Activity as the new label.
- Press the <Ctrl-S> keys to save this type definition.
- Close the Control Editor window to return to the Project Explorer window.
Defining the Type of State Data
State data is different from output data. State data is accessible from only the statechart, whereas output data also is accessible from a VI that calls the statechart. Use state data to define data types that other VIs do not need to access.
Complete the following steps to define the statechart state data. You modify this data in Part 4 of this tutorial.
- In the Project Explorer window, double-click the StateData.ctl item.
- Right-click the Data control and select Change to Indicator to replace the numeric control with a numeric indicator.
- Double-click the Data label and enter Counter as the new label.
- Press the <Ctrl-S> keys to save this type definition.
- Close the Control Editor window to return to the Project Explorer window.
Defining a State Entry Action
The next step is configuring each state to modify the Current Activity indicator that you defined earlier. You accomplish this task by defining an entry action for a state. An entry action is LabVIEW block diagram code that the statechart executes immediately after entering a state.
Complete the following steps to configure the Menu state to update this indicator.
- Double-click the Diagram.vi item to display the Statechart Editor window.
- Double-click the border of the Menu state to launch the Configure State dialog box.

Note When you configure a state, a wrench icon appears on the state (
) in the Statechart Editor window. The state also changes color. You can customize the configuration color for states. The default configuration color is yellow.
The left side of this dialog box contains the Reactions list. The right side of this dialog box displays several tabs that correspond to the item you select from the Reactions list. By default, the dialog box displays the Entry Action item in the Reactions list. This selection specifies that you want to define the entry action of the state.
The tabs on the right correspond to the selected item in the Reactions list. By default, the Action tab is selected. LabVIEW displays the block diagram you use to define the entry action. - Notice the Outputs cluster element on the right side of this block diagram. Click this cluster element and select Outputs»Current Activity. This element now displays Outputs.Current Activity, which indicates you can write to the value of the type definition you defined.
- Move the cursor over the input of this cluster element until the cursor changes to the Wiring tool.
- Right-click the input and select Create»Constant from the shortcut menu. LabVIEW places a string constant on the block diagram.

Note You cannot access the front panel window for this block diagram. Therefore, National Instruments recommends using block diagram constants instead of front panel controls. - Enter Menu as the value of the constant. The block diagram now resembles the following figure:

This entry action code means that, immediately after entering the Menu state, the statechart changes the Current Activity indicator to read Menu. Later in this tutorial, you will display the value of this indicator in the VI that calls the statechart. - Click the OK button to save changes and return to the statechart diagram.
- The next step is defining the Play and Pause states to update the Current Activity indicator appropriately. Define the Entry Action for each state to update this indicator to Playing or Paused, respectively.
- Save the statechart.
Defining the Action of a Transition
The transition from the Menu state to the Terminal pseudostate also causes a change in the activity of the statechart. Complete the following steps to configure this transition.
- Double-click the transition node in between the Menu state and Terminal pseudostate to launch the Configure Transition dialog box. Notice the tabs in this dialog box are identical to those in the Configure State dialog box. The only difference is that the Configure Transition dialog box does not have a list of items on the left side of the dialog box. These tabs apply to the whole transition.
- Click the Action tab.
- Write the block diagram code that changes the value of the Current Activity indicator to Stopped.
- Click the OK button to save changes and return to the statechart diagram.
Notice the transition node you just configured now displays two blue rectangles. The second blue rectangle indicates the transition has a specified action.
Duplicating the Transition Node
The next step is creating transitions from the Play and Pause states to the Terminal pseudostate. Instead of configuring additional transition nodes, you can duplicate the transition node you configured already. Complete the following steps to duplicate this transition node.
- Press the <Ctrl> key and click and drag the transition node you just configured.
- Move this node under the Play state.
- Click the bottom border of the Play state to initiate a transition segment.
- Move the cursor over the incoming tunnel of the transition node until LabVIEW displays a tip strip that reads Segment In.
- Click again to complete the transition segment.
- Create a transition segment from the other side of this node to the Terminal pseudostate.
- (Optional) Right-click the transition node and select either Rotate or Reverse to ensure the node faces the proper direction.
- Repeat steps 1–7 to create an additional transition node from the Pause state to the Terminal pseudostate. All three states now have a transition to the Terminal pseudostate.
- Save the statechart.
Generating Code for the Statechart
The next step is generating code for the statechart. This code is contained in a single Run Statechart function that you place on the block diagram of the caller VI. Complete the following steps to generate code for this statechart.
- Display the Statechart Editor window.
- Click the Generate Code button
, which is located on the toolbar of this window. - Close the Statechart Editor window.
![]() |
Note You also can generate code by right-clicking the .lvsc file in the Project Explorer window and selecting Generate Code from the shortcut menu. |
Configuring the Caller VI
The next step is configuring a caller VI to execute and send triggers to the statechart. This VI contains two functions, Run Statechart and Send External Trigger, that you must link to the statechart. Complete the following steps to link these functions to the statechart.
- In the Project Explorer window, select File»Open.
- Browse to the labview\examples\Statechart\Tutorial\Asynchronous directory and double-click Getting Started Caller VI (Unlinked).vi.
Open example
- Display the block diagram of this VI. All the wires are broken because neither the Run Statechart nor the Send External Trigger function is linked to a statechart.
- Locate the Run Statechart function
. This function executes the statechart when you run the caller VI. - Right-click this function and select Link to Statechart from the shortcut menu. LabVIEW prompts for a .lvsc file to which the function can link.
- Click the Browse button and navigate to the directory that contains the My Getting Started Statechart.lvsc file.
- Click the OK button. Notice the wires to and from this function now are intact.
- Repeat steps 4–7 for the Send External Trigger function.
- Select File»Save As and save the caller VI as Getting Started Caller VI Linked.vi.
- Press the <Ctrl-R> keys to run the caller VI. Click the front panel buttons and notice how the Current Activity indicators changes according to the state.
- Stop the caller VI.
- Save and close the caller VI.
![]() |
Note The inputs and triggers wired to the Run Statechart function and the Send External Trigger function are not linked to the My Getting Started Statechart.lvsc file. The Getting Started Caller VI (Unlinked).vi does not reflect any changes you make to the inputs or triggers in the My Getting Started Statechart.lvsc code. |
Summary
In this tutorial, you learned about the following tasks:
- Defining types of data that the statechart can modify.
- Defining the entry action of a state.
- Defining the action of a transition.
- Generating code for the statechart.
- Linking the Run Statechart and Send External Trigger functions to a statechart.
Where to Go from Here
In Part 4 of this tutorial, you create and configure regions and substates.
