Labview Day 4: The Case Structure and Rings: Vern Lindberg
Labview Day 4: The Case Structure and Rings: Vern Lindberg
Vern Lindberg
The case structure allows selective execution of code depending on an input. The simplest
example of a Case is an If (condition) ...Then...Else.
Let’s program a vi to convert between Fahrenheit and Celsius. There will be an input
temperature, an output temperature, and a Boolean Control that selects which conversion
to do.
(a) Place a Structures→Case Structure on a block diagram. You will see two parts
to the Case, a Case Selector with a ? on the left, and a Selector Label at the top.
By default the Case Selector is green, or Boolean, and the Selector label is either True
or False.
(b) On the front panel place a Modern→Numeric Control for the input temperature, a
Modern→Numeric Indicator for the output temperature, and a Modern→Boolean→Vertical
Toggle Switch.
(c) Switch to the block diagram. Place the Numeric Control and Vertical Toggle Switch
icons to the left of the Case Structure, and the Numeric Indicator to the right of the
Case Structure.
Connect the switch output to the ? on the case structure. Connect the numeric control
to a tunnel (non-indexed) on the Case.
(d) Define the switch-up position as a Celsius input. Go to the Properties of the switch,
uncheck Label Visible, check Show Boolean Text, Enter the On and Off texts (Celsius
and Fahrenheit).
On the front panel drag the Boolean Label to the left of the switch.
1
2 Increasing the number of cases 2
(e) Two sets of code will need to be written for the two cases. For the True case, use icons
from Numeric or use a Structures→Formula Node to do the conversion. Wire the
converted temperature to a tunnel and then to the Indicator.
Switch to the False case and repeat the process, this time converting from Fahrenheit
to Celsius. Run the program to see it it works. (You may want to use the Run
Continuously, or to wrap the entire code in a While Loop so you can change values
and switch positions while the program runs.)
(a) Go to the properties of the switch, and choose different colors for the two states of the
switch (e.g. cyan for Celsius, fuchsia for Fahrenheit). Once you are used to the colors,
you can glance at the screen and without reading see the state.
(b) Add a Modern→Boolean→Round LED to the front panel and place near the numeric
indicator. Go to its properties, uncheck Label Visible, check Show Boolean Text, enter
the On and Off texts (Fahrenheit and Celsius), reversing the order relative to the
switch. Make the ON and OFF colors appropriate. Wire the switch to the LED.
(c) Finally right click on the Numeric Control and the Numeric Indicator, and adjust the
Display Format to have two digits after the decimal point and to show trailing zeros.
Often 0. 0 . will have to deal with more than two cases. We will add cases to the Case
Structure, and select them by using a Ring Control. Let’s build a vi that will input an
angle (in degrees) and compute a trig function, sine, cosine, or tangent.
(a) Place a case structure on a new vi. Switch to the front panel and place a Modern→Ring
& Enum→Text Ring and a Numeric Control.
On the block diagram you will see that the ring is blue, or integer. Its output will be
an integer chosen by words in the ring. Let’s prepare the ring first. Click on a blank
place on the block diagram and add a comment, “0. Sine 1. Cosine 2. Tangent.”
Open Ring Properties→Edit Items. Type “Tangent” for the item, then Insert to get
a new ring item, “Sine”, then insert the third item, “Cosine”. Move items up or down
so that they match the numbers in your comment box.
(b) Wire the ring to the ? input of the Case Structure. It may be hard to see, but the
color will switch from the Boolean green to the Integer blue.
3 State Machines—Briefly 3
Your ring has 3 values, but your Case has only two. with the 1. case displayed,
right click on the case boundary and Add case after so that you will have three cases
numbered 0, 1 and 2.
(c) Trig functions like radians, so convert the angle into radians. Hint: on the numeric
palette you can find constants like π.
Trig functions are found on Mathematics→ Elementary and Special Functions→
Trig Functions. Place the appropriate one for each of the three cases. Wire the
output of a trig function to the boundary, then Create an indicator. You will need to
wire the tunnels for each case. Run the vi and check it.
(d) To increase the usability, let’s add a string that tells which trig function is being done1 .
In each case create a String Constant with the appropriate name. Connect to a tunnel,
and Create an Indicator. Arrange the front panel so that the ring appears, along with
the Stop, as is shown in Figure 1. (You don’t need to have a while loop if you use Run
Continuously.)
Fig. 1: Built with a Case structure controlled by a 3 value ring. The last row has a string
indicator (tangent), the numeric control, a comment on the front panel (=), and
another numeric indicator.
Variations on the Ring that you have just used include Menu Rings and Pict Rings.
3 State Machines—Briefly
An important control structure is a state machine. Based on the values of several inputs
(coolant level, temperature, etc.) the controlling vi will go to one of several states (add
coolant, increase flow, etc.) In LabVIEW a state machine is created by placing a Case
Structure inside a While Loop. More on this another day.
1
This could be done just with a Ring also. Make another Ring Control, change it from a control to an
indicator and using the first ring as input.