You can convert a cluster to an array if all cluster components have the same data type (for example, all are Boolean data types or all are numeric). With this conversion, you can use array functions to process components within the cluster.
The Cluster to Array function Functions»Cluster and Functions»Array palettes converts a cluster of identically typed components to a 1D array of the same data type.
The following example shows a four-component Boolean cluster converted to a four-element Boolean array. The index of each element in the array corresponds to the logical order of the component in the cluster. For example, Button 1 (component 0) corresponds to the first element (index 0) in the array, Button 2 (component 1) to the second element (index 1), and so on.
The Array to Cluster function (Functions»Cluster and Functions»Array palettes) converts a 1D array to a cluster in which each component in the cluster is the same type as the array element.
Note You must right-click the function icon to set the number of components in the cluster. The default number of components in the output cluster is nine.
You can combine the concept of a state machine with a Boolean menu cluster to provide a powerful system for menus. For example, perhaps you need to provide the following application, which can be divided into a series of states.
1 Front Panel 2 Cluster Panel 3 Block Diagram
State Value State Name Description Next State –1, Default No Event Monitor Boolean
menu to determine
2 Acquire Acquire data No Event (0)
1 2
3
The following is an example of a state machine for this application.
The front panel consists of a Boolean button cluster, with each button triggering a state in the state machine. In state –1 (the No Event state), the Boolean button cluster is checked to see if a button has been pressed. The Search 1D Array function returns the index of the button pressed (or –1 if no button is pressed) to determine the next state to execute. That state value is loaded into the shift register, so that on the next iteration of the while loop the selected state will execute.
In each of the other states, the shift register is loaded with the next state to execute using a numeric constant. Normally this is state –1, so that the Boolean menu will be checked again, but in state 1 (Configure) the subsequent state is state 2 (Acquire).
Exercise 2-3 Menu.vi
Objective: To build the menu system for the sample application.
A set of dependencies exist between the different operations of the application to be built in this course. Under most circumstances, after performing a certain action, the application should return to a “No Event”
state, in which the application should monitor a menu to see which button should be pressed.
The dependencies of the application can be described as a simple state machine, where each numeric state leads to another subsequent state. The following table summarizes this series of dependencies.
In this exercise, you will build the state machine to be used in this application and observe its operation.
Note You will use this VI in Lesson 5.
State Value State Name Description Next State –1, Default No Event Monitor Boolean
menu to determine
0 Login Log in user No Event
1 Acquire Acquire data No Event
2 Analyze Analyze data,
possibly save to file
No Event
3 View View saved data
files
No Event
4 Stop Stop VI No Event
Front Panel
1. Open a new VI.
2. Build the front panel according to the previous example. Each button in the cluster will trigger an appropriate state when it is pressed. When building the front panel, make sure that the Login button is at cluster order 0. Acquire Data is cluster order 1, Analyze & Present Data is cluster order 2, View Analysis File is cluster order 3, and Stop is cluster order 4. The cluster order of the menu cluster will determine the numeric state which will be executed.
Hints:
• Create the button with the largest label first.
• Set the mechanical action to Latch When Released.
• Use Copy and Paste to create the other buttons.
• Use Align and Distribute to arrange the buttons.
Block Diagram
1. Build the block diagram as shown previously.
a. Place a While Loop located on the Functions»Structures palette on the block diagram. This structures the VI to continue to generate and analyze data until the user presses the Stop button. Create the shift register by right-clicking the left or right side border of the loop and
b. Place a Case structure located on the Functions»Structures palette on the block diagram. This structure makes the states for the state machine. Add cases by right-clicking the border of the Case structure. Be sure to define the –1 case as being the default.
c. Place the Wait Until Next ms Multiple function located on the Functions»Time & Dialog palette on the block diagram. This function causes the While Loop to execute ten times a second.
Create the constant by right-clicking the input terminal and selecting Create»Constant.
d. .Place the Cluster To Array function located on the
Functions»Cluster or Functions»Array palette on the block diagram. In this exercise, this function converts the cluster of Boolean buttons into an array of Boolean data types. The Boolean object at cluster order 0 becomes the Boolean element at array index 0, cluster order 1 becomes array index 1, and so on.
e. Place the Search 1D Array function located on the
Functions»Array palette on the block diagram. In this exercise, this function searches the Boolean array that Cluster to Array returns for a TRUE value. A TRUE value for any element indicates that you clicked on the corresponding button. The function returns a value of –1 if you did not click a button.
f. Place the One Button Dialog function located on the
Functions»Time & Dialog palette on the block diagram. You will use four of these functions to indicate which state has been selected and loaded into the shift register.
2. Save the VI asMenu.vi.
3. Run the VI. When you click the Login, Acquire Data, Analyze &
Present Data, or Data File View buttons, a dialog box appears to indicate that you are in the associated state.
4. Using the Single-Step and Execution Highlighting features, observe how the VI executes. Notice that until you click a button, the Search 1D Array function returns a value of –1, which causes the While Loop to continuously execute state –1. Once a button is pressed, however, the index of the Boolean data types is used to determine the next state to execute. Notice how the states of the VI correspond to the states in the table at the top of the exercise.
In later exercises, you will substitute VIs that you create for the One Button Dialog functions to build the application.
5. Click the Stop button on the VI’s front panel to halt execution.
6. Close the VI when you are finished.
Exercise 2-4 Cluster Conversion Example VI (Optional)
Objective: To examine a VI that uses clusters to process data.
You will examine a VI that uses clusters to process data. The VI features a cluster containing four labeled buttons. The VI keeps track of the number of times you click each button.
Front Panel
1. Open the Cluster Conversion Example VI in thec:\exercises\LV Basics 2 directory.
Block Diagram
False case is empty except for passing the cluster from the left shift register to the right shift register.
1. Open and examine the block diagram.
2. Run the VI. Click a button. The corresponding digital indicator should increment each time you click a button.
3. Close the VI. Do not save any changes.