LabVIEW Core 1 Exercises
LabVIEW Core 1 Exercises
LabVIEW Core 1
Exercises
Trademarks
LabVIEW, National Instruments, NI, ni.com, the National Instruments corporate logo, and the Eagle logo are trademarks of National Instruments Corporation. Refer to the Trademark Information at
ni.com/trademarks for other National Instruments trademarks.
Other product and company names mentioned herein are trademarks or trade names of their respective companies.
Patents
For patents covering National Instruments products/technology, refer to the appropriate location: HelpPatents in your software, the patents.txt file on your media, or the National Instruments
Patent Notice at ni.com/patents.
Implementing a VI
Complete the exercises in this lesson to reinforce the concepts you learn in Lesson 3 of LabVIEW Core 1.
Exercises
Exercise 3-1
Temperature Warnings VI
Exercise 3-2
Auto Match VI
Exercise 3-3
Exercise 3-4
Exercise 3-5
Exercise 3-6
National Instruments
| 3-1
Lesson 3
Implementing a VI
Exercise 3-1
ni.com
3-2
Temperature Warnings VI
Goal
Create and document a simple VI.
Scenario
You must create a portion of a larger project. The lead developer gives you the inputs of the VI, the algorithm, and the expected outputs. Your task is to create and
document a VI based on the given design.
Design
Use the following flowchart and input/output list to create a VI that displays temperature warnings. This VI is part of the temperature weather station project studied
throughout the course.
Flowchart
The flowchart in Figure 3-1 illustrates the data flow for this design.
Figure 3-1. Temperature Warnings VI Flowchart
Current Temp
>= Max Temp?
No
Yes
Warning Text =
Heatstroke
Warning
Warning Text =
No Warning
Current Temp
<= Min Temp?
No
Yes
Warning Text =
Freeze Warning
Warning
Text = No
Warning?
No
Yes
Warning ? = False
Warning ? = True
National Instruments
3-3
Lesson 3
Implementing a VI
ni.com
3-4
Name
Properties
Numeric control
Current Temperature
Double-precision, floating-point
Numeric control
Max Temperature
Double-precision, floating-point
Numeric control
Min Temperature
Double-precision, floating-point
String indicator
Warning Text
Warning?
Implementation
Complete the following steps to create a front panel similar to Figure 3-2. The user enters the current temperature, maximum temperature, and minimum temperature.
Then, the front panel displays the warning string and the warning Boolean LED.
1. Create a new LabVIEW Project and add a new VI to the project.
Select FileNew from the LabVIEW menu and then select ProjectEmpty Project from the Create New tree.
Save the new project as <Exercises>\LabVIEW Core 1\Weather Warnings\Weather Warnings.lvproj.
Right-click My Computer in the Project Explorer window and select NewVI to open a blank VI.
Save the VI as Temperature Warnings.vi in the <Exercises>\LabVIEW Core 1\Weather Warnings directory.
2. Create the front panel shown in Figure 3-2.
Note
1
4
Numeric ControlRight-click and select RepresentationDBL to confirm that this numeric control is double precision.
Create copies of the Current Temperature numeric control by holding down the <Ctrl> key and dragging the Current Temperature control.
Round LED
Tip You can use this VI for Fahrenheit, Kelvin, or any other temperature scale, as long as all inputs to the VI use the same scale. Therefore, it is not necessary
to add scale units to the labels.
National Instruments
3-5
Lesson 3
Implementing a VI
ni.com
3-6
Greater or Equal?Compares input values and returns a TRUE value if Current Temperature is greater than or equal to Max Temperature.
Less or Equal?Compares input values and returns a TRUE value if Current Temperature is less than or equal to Min Temperature.
String ConstantCreate string constants for Heatstroke Warning, Freeze Warning, and No Warning.
SelectReturns the Heatstroke Warning string if Current Temperature is greater than or equal to Max Temperature; otherwise returns the No Warning
string.
SelectReturns the Freeze Warning string if Current Temperature is less than or equal to Min Temperature; otherwise returns the No Warning string.
Not Equal?Returns a TRUE value to the Warning? indicator if the value Warning Text is something other than No Warning.
4. Modify the description and tip associated with the Warning Text indicator to change the content shown in the Context Help window.
Right-click the Warning Text indicator and select Description and Tip.
Enter Displays text string indicating Heatstroke Warning, Freeze Warning, or No Warning depending on weather
conditions in the Warning Text Description text box.
Note Text you enter in the Warning Text Tip text box displays in a tip strip. Tip strips are brief descriptions that appear when you move the cursor over
a front panel object while a VI runs.
National Instruments
3-7
Lesson 3
Implementing a VI
ni.com
3-8
Owned labelsRight-click a function and select Visible ItemsLabel from the shortcut menu to display the owned label for that function.
Move labelClick the owned label of the Greater or Equal? function with the Positioning tool and move it beneath the function.
Move functionUse the positioning tool to move the function. Notice that the owned label moves with the function.
Owned labelRight-click the wire and select Visible ItemsLabel from the shortcut menu. Move the label with the Positioning tool.
Free labelDouble-click a blank area of the block diagram and use the Labeling tool to create comments for the code.
6. Document the VI Properties, giving a general description of the VI, a list of inputs and outputs, your name, and the date the VI was created. To access the VI
Properties dialog box, select FileVI Properties and select Documentation from the Category pull-down menu.
7. Save the VI.
Test
1. Test the VI by entering a value for Current Temperature, Max Temperature, and Min Temperature, and running the VI for each set.
Table 3-2 shows the expected Warning Text string and Warning? Boolean value for each set of input values.
Note
The temperature range in the table is only an example of temperatures selected to indicate freeze or heatstroke warnings.
Max Temperature
Min Temperature
Warning Text
Warning?
30
30
10
Heatstroke Warning
True
25
30
10
No Warning
False
10
30
10
Freeze Warning
True
What happens if you input a maximum temperature value that is less than the minimum temperature value? What would you expect to happen? You learn to
handle issues like this one in Exercise 3-6.
2. Save and close the VI and the project.
National Instruments
3-9
Exercise 3-2
Auto Match VI
Goal
Use a While Loop and an iteration terminal and pass data through a tunnel.
Scenario
Create a VI that continuously generates random numbers between 0 and 1000 until it generates a number that matches a number selected by the user. Determine how
many random numbers the VI generated before generating the matching number.
Design
Use the following flowchart and input/output list to create the VI for this exercise.
National Instruments
3-11
Lesson 3
Implementing a VI
ni.com
Flowchart
The flowchart in Figure 3-5 illustrates the data flow for this design.
Figure 3-5. Auto Match Flowchart
Generate a
Random Number
Between 0 and 1
No
Multiply by 1000
Round to Nearest
Integer Value
Display Value
to User
= Number
to Match?
Yes
Add 1 to
Iteration
Terminal
Display Number
of Iterations
3-12
Name
Properties
Numeric control
Number to Match
Numeric indicator
Current Number
Double-precision, floating-point
Numeric indicator
Number of Iterations
Integer
Implementation
1. Create a blank project and save it as Auto Match.lvproj in the <Exercises>LabVIEW Core 1\Auto Match directory.
2. Create a new VI in the project and save it as Auto Match.vi in the same directory as the project.
National Instruments
3-13
Lesson 3
Implementing a VI
ni.com
3-14
Set the default value of the Number to Match control to 50Enter 50 in the Number to Match control and then right-click the control and select Data
OperationsMake Current Value Default.
Set Number of Iterations indicator to output a signed, long integerRight-click the indicator and select RepresentationI32.
4. Set the properties for the Number to Match control so that the data type is a 32-bit unsigned integer, the data range is from 0 to 1000, the increment value is 1,
and the digits of precision is 0.
Right-click the Number to Match control and select RepresentationU32 from the shortcut menu.
Right-click the Number to Match control and select Data Entry from the shortcut menu. Set the properties on the Data Entry and Display Format tabs as
shown in Figure 3-7.
National Instruments
3-15
Lesson 3
Implementing a VI
ni.com
5. Set the data type of the Current Number indicator to an unsigned, 32-bit integer and set the digits of precision for the Current Number output to 0.
Right-click the Current Number indicator and select RepresentationU32 from the shortcut menu.
Right-click the Current Number indicator and select Display Format. Set the properties as shown in Figure 3-7.
Figure 3-8. Current Number Indicator Display Format Properties
3-16
MultiplyMultiplies the random number by the y input to produce a random number between 0 and y.
Numeric ConstantRight-click the y input of the Multiply function and select CreateConstant. Enter a value of 1000. Because the Random Number (0-1)
function generates a double-precision, floating point number between 0 and 1, multiplying the number by 1000 produces a range of numbers between 0 and
1000.
Equal?Compares the random number with Number to Match and returns FALSE if the numbers are not equal. Otherwise, it returns TRUE.
While LoopRepeats the algorithm until the Equal? function returns TRUE because the Equal? function is wired to the conditional terminal, which is set to Stop
if True.
Iteration terminalEach time the loop executes, the iteration terminal increments by one.
Coercion dotsRed coercion dots appear on block diagram nodes when you connect a wire of one numeric type to a terminal of a different numeric type. In this
case, the output from Round To Nearest is a double-precision, floating point but Current Number is an integer.
Tip
Coercion dots can cause a VI to use more memory and increase its run time, so try to keep data types consistent in the VIs you create.
National Instruments
3-17
Lesson 3
Implementing a VI
ni.com
3-18
Right-click the wire coming from the Round To Nearest function and select InsertNumeric PaletteConversionTo Unsigned Long Integer as shown in
Figure 3-10. This inserts the To Unsigned Long Integer function on the wire.
Figure 3-10. Inserting the To Unsigned Long Integer Function on a Wire
8. Notice that converting the output from the Round To Nearest function removes all the coercion dots on the block diagram, as shown in Figure 3-11.
Figure 3-11. Completed Auto Match VI
Test
1. Change the number in the Number to Match control to a number that is in the data range, which is 0 to 1000 with an increment of 1.
2. Run the VI.
3. Change the Number to Match value and run the VI again. Current Number updates at every iteration of the loop because it is inside the loop. Number of Iterations
updates upon completion because it is outside the loop.
National Instruments
3-19
Lesson 3
Implementing a VI
ni.com
3-20
On the block diagram toolbar, click the Highlight Execution button to enable execution highlighting. Execution highlighting shows the movement of data
on the block diagram from one node to another so you can see each number as the VI generates it.
Change the Number to Match value to a number that is out of the data range, 0 1000.
Run the VI.
Notice LabVIEW coerces the out-of-range value to the nearest value in the data range you specified in step 4 of the Implementation section.
8. Close the VI.
Exercise 3-3
Goal
Understand when to use a While Loop and when to use a For Loop.
Description
For the following scenarios, decide whether to use a While Loop or a For Loop.
Scenario 1
Acquire pressure data in a loop that executes once per second for one minute.
1. If you use a While Loop, what is the condition that you need to stop the loop?
2. If you use a For Loop, how many iterations does the loop need to run?
3. Is it easier to implement a For Loop or a While Loop?
Scenario 2
Acquire pressure data until the pressure is greater than or equal to 1400 psi.
1. If you use a While Loop, what is the condition that you need to stop the loop?
2. If you use a For Loop, how many iterations does the loop need to run?
3. Is it easier to implement a For Loop or a While Loop?
National Instruments
3-21
Lesson 3
Implementing a VI
Scenario 3
Acquire pressure and temperature data until both values are stable for two minutes.
1. If you use a While Loop, what is the condition that you need to stop the loop?
2. If you use a For Loop, how many iterations does the loop need to run?
3. Is it easier to implement a For Loop or a While Loop?
Scenario 4
Output a voltage ramp starting at zero, increasing incrementally by 0.5 V every second, until the output voltage is equal to 5 V.
1. If you use a While Loop, what is the condition that you need to stop the loop?
2. If you use a For Loop, how many iterations does the loop need to run?
3. Is it easier to implement a For Loop or a While Loop?
ni.com
3-22
Answers
Scenario 1
Acquire pressure data every second for one minute.
1. While Loop: Time = 1 minute
2. For Loop: 60 iterations
3. Both are possible.
Scenario 2
Acquire pressure data until the pressure is 1400 psi.
1. While Loop: Pressure = 1400 psi
2. For Loop: unknown
3. A While Loop. Although you can add a conditional terminal to a For Loop, you still need to wire a value to the count terminal. Without more information, you
do not know the appropriate value to wire to the count terminal.
National Instruments
3-23
Lesson 3
Implementing a VI
ni.com
3-24
Scenario 3
Acquire pressure and temperature data until both values are stable for two minutes.
1. While Loop: [(Last Temperature = Previous Temperature) for 2 minutes or more] and [(Last Pressure = Previous Pressure) for 2 minutes or more]
2. For Loop: unknown
3. A While Loop. Although you can add a conditional terminal to a For Loop, you still need to wire a value to the count terminal. Without more information, you
do not know the appropriate value to wire to the count terminal.
Scenario 4
Output a voltage ramp starting at zero, increasing incrementally by 0.5 V every second, until the output voltage is equal to 5 V.
1. While Loop: Voltage = 5 V
2. For Loop: 11 iterations (Including the two end points, count the iteration for each value 0, 0.5, 1.0, 1.5, ... 4.5, 5.0.)
3. Both are possible.
Exercise 3-4
Goal
Use a While Loop and shift registers to average data.
Scenario
The Temperature Monitor VI acquires and displays temperature. Modify the VI to average the last five temperature measurements and display the running average
on the waveform chart.
Design
Figure 3-12 and Figure 3-13 show the Temperature Monitor VI front panel and block diagram.
Figure 3-12. Temperature Monitor VI Front Panel
National Instruments
3-25
Lesson 3
Implementing a VI
ni.com
3-26
To modify this VI, you need to retain the temperature values from the previous four iterations of the While Loop and average the values. To accomplish this, you
modify this VI as follows:
Use a shift register with additional elements to retain data from the previous four iterations.
Initialize the shift register with a reading from the simulated thermometer.
Implementation
1. Test the VI.
Shift RegistersMultiple shift registers collect multiple temperature readings. Right-click the border of the While Loop and select Add Shift Register. Drag the
lower resizing handle of the shift register to display a four shift registers.
Create a copy of the Thermometer (Demo) VIPress <Ctrl> while dragging the subVI outside the While Loop to create a copy. The Thermometer (Demo) VI
returns one temperature measurement and initializes the left shift registers before the loop starts.
Compound ArithmeticReturns the sum of the current temperature and the four previous temperature readings. Resize the function to have five terminals.
National Instruments
3-27
Lesson 3
Implementing a VI
ni.com
3-28
Test
1. Run the VI.
During each iteration of the While Loop, the Thermometer VI takes one temperature measurement. The VI adds this value to the last four measurements stored
in the left terminals of the shift register. The VI divides the result by five to find the average of the five measurementsthe current measurement plus the previous
four. The VI displays the average on the waveform chart. Notice that the VI initializes the shift register with a temperature measurement.
2. Stop the VI by clicking the Stop button on the front panel and save the VI.
3. Save and close the VI and the project.
Exercise 3-5
Goal
Plot multiple data sets on a single waveform chart and customize the chart view.
Scenario
Modify the VI from Exercise 3-4 to plot both the current temperature and the running average on the same chart. In addition, allow the user to examine a portion of
the plot while the data is being acquired.
Design
Figure 3-15 shows the front panel for the existing Temperature Monitor VI and Figure 3-16 shows the block diagram.
Figure 3-15. Temperature Monitor VI Front Panel
To allow the user to examine a portion of the plot while the data is being acquired, display the scale legend and the graph palette for the waveform chart. Also, expand
the legend to show additional plots.
National Instruments
3-29
Lesson 3
Implementing a VI
ni.com
3-30
To modify the block diagram in Figure 3-16, you must modify the chart terminal to accept multiple pieces of data. Use a Bundle function to combine the average
temperature and the current temperature into a cluster to pass to the Temperature History terminal.
Figure 3-16. Original Temperature Monitor VI Block Diagram
Implementation
1. Open the Temperature Monitor VI you created in Exercise 3-4.
BundlePasses the current temperature and average temperature to the Temperature History chart.
National Instruments
3-31
Lesson 3
Implementing a VI
ni.com
3-32
Show both plots in the plot legendUse the positioning tool to resize the plot legend to show two objects. Double-click the label to edit the plot names. The order
of the plots listed in the plot legend is the same as the order of the items wired to the Bundle function on the block diagram.
Change the plot type of Current TemperatureUse the Operating tool to select the plot in the plot legend. Click the plot icon, select Common Plots from the
menu, and choose the plot you want.
Display Graph PaletteRight-click the Temperature History chart and select Visible ItemsGraph Palette.
Display Scale LegendRight-click the Temperature History chart and select Visible itemsScale Legend.
Test
1. Run the VI. Use the tools in the scale legend and the graph palette to examine the data as it generates.
2. Click the Stop button to stop the VI.
3. Close the VI and project when you are finished.
National Instruments
3-33
Exercise 3-6
Goal
Modify a VI to use a Case structure to make a software decision.
Scenario
You created a VI where a user inputs a temperature, a maximum temperature, and a minimum temperature. A warning string generates depending on the relationship
of the given inputs. However, a situation could occur that causes the VI to work incorrectly. For example, the user could enter a maximum temperature that is less
than the minimum temperature. Modify the VI to generate a different string to alert the user to the error: Upper Limit < Lower Limit. Set the Warning?
indicator to TRUE to indicate the error.
Design
Modify the flowchart created for the original Temperature Warnings VI as shown in Figure 3-19.
Figure 3-19. Modified Temperature Warnings Flowchart
MinTemp
Temp>=
Min
Max Temp
Yes
Create Error
No
Current
Temp
Min Temp
>=
MaxTemp
Temp
Max
Yes
Output
Heatstroke
Warning and
TRUE
No
Current Temp
Min Temp
No
Output
No Warning
and
FALSE
Yes
Output
Freeze Warning
and
TRUE
Output
Upper Limit
< Lower Limit
and TRUE
National Instruments
3-35
Lesson 3
Implementing a VI
ni.com
3-36
The original block diagram for the Temperature Warnings VI appears in Figure 3-20. You must add a Case structure to the Temperature Warnings VI to execute the
code if the maximum temperature is less than or equal to the minimum temperature. Otherwise, the VI does not execute the code. Instead, the VI generates a new
string and the Warning? indicator is set to TRUE.
Figure 3-20. Original Temperature Warnings VI Block Diagram
Implementation
1. Open Weather Warnings.lvproj in the <Exercises>\LabVIEW Core 1\Weather Warnings directory.
2. Open Temperature Warnings.vi from the Project Explorer window. You created the Temperature Warnings VI in Exercise 3-1.
3. Place an Error In and Error Out cluster on the front panel as shown in Figure 3-21.
Figure 3-21. Temperature Warnings VI Front Panel with Error Clusters
4. Open the block diagram and create space to add the Case structure.
Select the Current Temperature, Max Temperature, Min Temperature, and Error In control terminals.
Tip
To select more than one item press the <Shift> key while you select the items.
While the terminals are still selected, use the left arrow key on the keyboard to move the controls to the left.
Tip
Press and hold the <Shift> key to move the objects in five pixel increments.
National Instruments
3-37
Lesson 3
Implementing a VI
Tip
ni.com
Press the <Ctrl> key and use the Positioning tool to drag out a region of the size you want to insert.
Select the Warning?, Warning Text, and Error Out indicator terminals.
Align the terminals by clicking the Align ObjectsLeft Edges button in the LabVIEW toolbar.
While the terminals are still selected, use the right arrow key on the keyboard to move the indicators to the right.
5. Modify the block diagram similar to that shown in Figure 3-22, Figure 3-23, and Figure 3-24. This VI is part of the temperature weather station project.
3-38
Less?Compares the Max Temperature and Min Temperature. Make sure the Less? function is outside the Case structure.
Case StructureDo not include the Current Temperature, Max Temperature, Min Temperature, Error In, Warning?, Warning Text, or Error Out terminals
in the Case structure because these controls and indicators are used by both cases.
Set True and False casesWith the True case visible, right-click the border of the Case structure and select Make this Case False.
Case StructureWire the Error In terminal to the selector terminal to create No Error and Error cases. By default, the Case structure has True and False cases.
These cases change to Error and No Error cases only after you wire Error In to the selector terminal.
National Instruments
3-39
Lesson 3
Implementing a VI
ni.com
3-40
True caseIf the Max Temperature is set lower than the Min Temperature, the True case executes. Click the case selector label to choose the True case.
True ConstantWhen the True case executes, the Warning? LED illuminates.
String ConstantIf the Max Temperature is set lower than the Min Temperature, the warning Upper Limit < Lower Limit displays on the front panel.
Enter the text in the String Constant.
6. Create the Error case in the outer Case structure so this VI can be used as a subVI.
Figure 3-24. Temperature Warnings VIError Case
National Instruments
3-41
Lesson 3
Implementing a VI
ni.com
3-42
Test
1. Switch to the front panel of the VI.
2. Resize the Warning Text indicator to a length to accommodate the new string.
3. Test the VI by entering values from Table 3-4 in the Current Temperature, Max Temperature, and Min Temperature controls and running the VI for each
set of data.
Table 3-4 shows the expected Warning Text and Warning? Boolean value for each set of data.
Table 3-4. Testing Values for Determine Warnings VI
Current Temperature
Max Temperature
Min Temperature
Warning Text
Warning?
30
30
10
Heatstroke Warning
True
25
30
10
No Warning
False
10
30
10
Freeze Warning
True
25
20
30
True
4. Test the Error case. To use this VI as a subVI, the VI must be able to handle an error coming into the VI. Test the Error case to make sure that this VI can output
the error information it receives.
On the front panel, use the Operating tool to click the status Boolean indicator inside the Error In cluster so that the indicator turns red and enter 7 in the
code control.
Run the VI. The error information you entered passes through the Error case in the VI and is output in the Error Out cluster.
Display the block diagram, select the No Error case, highlight execution, and then run the VI again to see the error pass through the Error case.
On the front panel, right-click the border of the Error Out cluster and select Explain Error to display information about the error that was returned.
5. Save and close the VI.