Statechart Module Tutorial Part 1: Modifying a Statechart
- Updated2023-02-21
- 14 minute(s) read
In Part 1 of this tutorial, you examine how LabVIEW executes a statechart. You also add a state to an existing statechart, create transitions to and from this state, create a trigger that causes the statechart to take a transition, and debug the statechart.
![]() |
You can complete these exercises in approximately 60 minutes. |
![]() |
Note Refer to the labview\examples\Statechart\Tutorial\Asynchronous\Getting Started 1.lvsc for a completed version of the statechart you create in this part of the tutorial. |
Running the Caller VI
The first step is opening and running the caller VI, which is the VI that executes the statechart. Complete the following steps to open and run this VI.
- Launch LabVIEW and display the Getting Started window.
- Select File»Open.
- Browse to the labview\examples\Statechart\Tutorial\Asynchronous directory and double-click Getting Started Caller VI.vi.
- Display the front panel of the VI and click the Run button
, located on the LabVIEW toolbar. - Notice the Current Activity indicator displays Menu.
- Click the Play front panel button. Notice the Current Activity indicator now displays Playing.
- Click the Menu front panel button and notice the Current Activity indicator displays Menu again.
- Click the Pause front panel button. Notice that nothing happens to the Current Activity indicator.
Nothing happens because the statechart this VI calls has only two states: Menu and Play. These states are configured to update the Current Activity indicator after you click the appropriate button. The statechart does not have a state that handles the Pause front panel button. You will add this state to the statechart. - Click the Stop front panel button. The Current Activity indicator displays Stopped, and the VI stops executing.

Note The statechart does not have a Stop state. The statechart is configured to update the Current Activity indicator before the statechart terminates.
Examining the Caller VI
The next step is examining the caller VI. Press the <Ctrl-E> keys to view the block diagram of this VI.
This caller VI has two loops because the statechart is asynchronous. Asynchronous statecharts react to triggers that another thread or VI sends. In this caller VI, one While Loop sends triggers to the statechart, and the other While Loop executes the statechart after receiving a trigger. These loops execute in parallel. You also can place each loop in a separate VI.
Launching the Statechart Editor Window
The next step is launching the Statechart Editor window. This window displays the statechart diagram that you executed in the Running the Caller VI section of this tutorial. The statechart diagram is where you place the statechart objects that comprise a statechart.
Complete the following steps to launch the Statechart Editor window.
- Locate the Run Statechart function
. - Right-click this function and select View»Statechart Diagram to launch the Statechart Editor window that contains the statechart diagram.
Examining the Statechart
The statechart diagram resembles the following figure:

When you ran the caller VI, the statechart located the Initial pseudostate. The transition (1) that exits this pseudostate specifies which state the statechart enters first. Therefore, the statechart executed the Menu state (2) first. The statechart then waited for the caller VI to send a trigger, which happened when you clicked the Play front panel button. When you clicked this button, the caller VI sent a trigger to the statechart. This trigger caused the statechart to take the transition (3) to the Play state and wait for another trigger, which you sent by clicking the Menu front panel button. Each state is configured to update the text of the Current Activity indicator.
![]() |
Note Because transitions go in only one direction, notice that the transition that enters the Play state is separate from the transition that exits the Play state. |
As the previous figure shows, there is no Pause state. Therefore, when you clicked the Pause front panel button, nothing happened. However, when you clicked the Stop front panel button, the statechart took the transition (4) to the Terminal pseudostate, which halted the execution of the statechart.
The arrow-shaped objects in the middle of each transition are the transition nodes. The following figure shows a transition node.

The blue rectangles on these nodes indicate certain information about the transition. You will create and configure a transition later in this part of the tutorial.
Saving a Backup Copy of the Statechart
During this part of the tutorial, you modify the example statechart that installs with the LabVIEW Statechart Module. You can save a backup copy of this example in case you need to return to the original form of this example. This step is optional.
Complete the following steps to save a backup copy of this statechart.
- In the caller VI, right-click the Run Statechart function and select View»Statechart to launch the Project Explorer window that contains the .lvsc file.
- Right-click the Getting Started 1.lvsc file in the Project Explorer window and select Save»Save As to launch the Save As dialog box.
- Ensure the Copy option is selected.
- Enter a descriptive name in the Name for copy of library file text box.
- Click the Continue button. LabVIEW prompts you for a location to save the .lvsc file.
- Browse to a convenient directory and click the Current Folder button. LabVIEW creates a copy of the .lvsc file and saves this copy in the location you specified. The original statechart remains in memory.
Adding a State to the Statechart
The next step is creating the state that reacts to the Pause front panel button. Complete the following steps to add this state to the statechart.
- Display the Statechart Editor window.
- If you do not see the Functions palette, select View»Functions Palette to display this palette.
- On this palette, select Statechart»Statechart Development to display the Statechart Development palette.
- Click the State icon.
- Move the cursor over the statechart diagram. Click to place the top left corner of the state, drag the cursor diagonally to establish the size of the state, and click again to place the state on the statechart diagram.
- Notice the state is labeled State. Double-click this label to enable text editing and enter Pause as the new label.
- Move the Pause state to the right of the Play state as shown in the following figure:

- Save the statechart by selecting File»Save or by pressing the <Ctrl-S> keys.
Creating a Transition from the Play State to the Pause State
The next step is defining when the statechart can enter and exit the Pause state. You accomplish this step by creating transitions to and from this state. Complete the following steps to create a transition from the Play state to the Pause state.
- Move the cursor over the right border of the Play state until the inner border blinks and the cursor changes to the Wiring tool.

Note If you do not see the Wiring tool, you might have disabled automatic tool selection. Either enable this feature or press the <Tab> key until the Wiring tool appears. - Click the border to initiate a transition. Notice that when you move the cursor now, a dotted line follows the cursor to indicate the placement of the transition.
- Move the cursor over the left border of the Pause state. Notice a gray box appears under the cursor.
- Click the border. LabVIEW creates a transition between the two states.

Note If you move the cursor inside the state border before clicking it, LabVIEW attempts to create a transition into the state instead of to the state. In this situation, press the <Esc> key to remove the erroneous transition.
The statechart diagram now resembles the following figure:

Creating a Trigger and Configuring the Transition
The next step is specifying that the statechart takes the transition only after receiving a specific event, also called a trigger. By default, transitions are configured to react to the NULL trigger. However, the caller VI that executes this statechart does not send the NULL trigger. Therefore, you must configure this transition to react to a different trigger.
Notice the transition node. You use this node to define the trigger(s) that cause the statechart to take a transition. Complete the following steps to configure this transition.
- Double-click the transition node between the Play and Pause states to launch the Configure Transition dialog box. You use this dialog box to configure transitions.

Note When you configure a transition, a wrench icon appears on the transition (
) in the Statechart Editor window. The transition also changes color. You can customize the configuration color for transitions. The default configuration color is yellow.
The first tab in this dialog box is the Triggers/Groups tab. This tab contains a list that shows the Play, Menu, and Stop triggers. These triggers are the valid triggers that the caller VI can send to the statechart. Also notice the NULL trigger. The LabVIEW Statechart Module provides this default trigger for every statechart. - Click the Edit Triggers and Groups button to launch the Edit Triggers and Groups dialog box. You use this dialog box to create and modify triggers.
- Click the Create Trigger button to add a new trigger, which appears in the Triggers and Groups list.
- Enter Pause as the new trigger name.
- Click the OK button to save changes and return to the Configure Transition dialog box. Notice the new trigger appears in the Triggers/Groups list.
- In the Triggers/Groups list, place a checkmark in the Pause checkbox to specify that this trigger causes the statechart to take the transition.
- Remove the checkmark from the NULL checkbox.
- Click the OK button to save changes and return to the statechart diagram.
Notice the blue rectangle that appears on the transition node. This rectangle indicates that the transition has a specified trigger that is not NULL.
Recall that transitions are unidirectional. As the previous figure shows, the statechart cannot move from the Pause state to the Play state. Complete the following steps to address this limitation.
- Create a transition from the Pause state to the Play state.
- Configure this transition to react to the existing Play trigger. Remember to deselect the NULL trigger.
- Save the statechart.
The statechart diagram now resembles the following figure:

Viewing and Changing the Transition Label
Each transition node has a built-in label. To see this label, right-click the second transition node you created and select Visible Items»Label from the shortcut menu. LabVIEW displays Transition as the label for this transition. You can double-click this label to change the label. You also can use the Label text box in the Configure Transition dialog box.
Duplicating a Transition Node
Notice that the statechart cannot move from the Pause state to the Terminal pseudostate. You can create this transition by duplicating the transition node that connects the Menu state to the Terminal pseudostate. Complete the following steps to duplicate this transition.
- Press the <Ctrl> key and click and drag this transition node.
- Move this node under the Pause state.
- Click the border of the Pause 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.
- Save the statechart.
The statechart now resembles the following figure:

Configuring the Pause State
The next step is configuring the Pause state to update the Current Activity front panel indicator appropriately. You accomplish this task by defining an entry action for this state. An entry action is LabVIEW code that the statechart executes immediately after entering a state.
Complete the following steps to configure the Pause state to update this indicator.
- Double-click the border of the Pause 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 from the shortcut menu. This element now displays Outputs.Current Activity. You now can use this element to write to the value of this indicator.
- 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. - Double-click inside this constant and enter Paused as the value of the constant. The block diagram now resembles the following figure:

This code means that, every time the statechart enters the Pause state, the statechart changes the Current Activity indicator to read Paused. - Click the OK button to save changes and return to the statechart diagram.
- Save the statechart.
Generating Code for the Statechart
The next step is generating code for the statechart. You then call this code by executing the Run Statechart function, which 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.
![]() |
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 to Communicate with the Pause State
The next step is configuring the caller VI to send the Pause trigger to the statechart when you click the Pause front panel button. Complete the following steps to configure this behavior.
- Display the block diagram of the caller VI.
- Select the Pause event case of the Event structure. Notice the blue output tunnel on the right side of this structure. A wire connects this tunnel to the Trigger input of the Send External Trigger function.
- Right-click this output tunnel and select Create»Constant from the shortcut menu. LabVIEW creates an enumerated type constant that contains the triggers you can send to this statechart.
- Select Pause as the value of this constant.
- Select File»Save As to launch the Save As dialog box.
- Ensure the Substitute copy for original option is selected and click the Continue button.
- Enter My Getting Started Caller VI.vi as the new name and click the OK button. LabVIEW saves a copy of this caller VI and opens this copy in memory.
This case of the Event structure now resembles the following figure:

In the previous figure, clicking the Pause front panel button causes the Event structure to send the Pause trigger, which you created, to the Send External Trigger function. This function sends the Pause trigger to the external trigger queue of the statechart, which then reacts to the trigger by moving to the appropriate state.
![]() |
Note Notice that you did not have to replace the Run Statechart or Send External Trigger functions on the block diagram. After you generate code for the statechart, LabVIEW updates this function automatically. LabVIEW also updates the Trigger enumerated type constant with the Pause trigger you created. |
Running the Caller VI Again
The next step is running the caller VI again and seeing the behavior you defined for the Pause front panel button. Complete the following steps to run this VI and test the new state.
- Display the front panel of the caller VI.
- Press the <Ctrl-R> keys to run the VI.
- Click the Play front panel button to move to the Play state.
- Click the Pause front panel button. Notice Current Activity indicator displays Paused. This behavior is what you configured during this part of the tutorial.
- Click the Menu front panel button. Notice the statechart does nothing. You have not defined a transition from the Pause state to the Menu state. You can move to the Menu state from only the Play state.
Highlighting Statechart Execution
The next step is highlighting the execution of the statechart. Complete the following steps to highlight the execution of the statechart.
- While the caller VI is running, display the block diagram of the caller VI.
- Right-click the Run Statechart function and select Debug Statechart to display the statechart in the Statechart Debugging window.
- Click the Highlight Execution button
, located on the toolbar of the Statechart Debugging window, to enable statechart execution highlighting. - Display the front panel of the caller VI and click the Play front panel button. Watch LabVIEW highlight the transition from the Pause state to the Play state.
- Click the Menu front panel button and watch the statechart move to the Menu state.
- Click the Stop front panel button. Watch the statechart move to the Terminal pseudostate and stop the execution of the statechart.
- Close the Statechart Debugging window and the caller VI.
Summary
In this tutorial, you learned about the following tasks:
- Defining a state.
- Creating transitions.
- Creating and specifying triggers.
- Defining the entry action of a state.
- Generating code for a statechart.
- Calling a statechart from a VI.
- Highlighting the execution of a statechart.
Where to Go from Here
In Part 2 of this tutorial, you create and configure a new statechart.
