Introduction
This lesson shall demonstrate how to use loops within the LabVIEW development platform. Loops are a programming structure common within traditional text-based programming languages in which a set of code is executed repeatedly based on a governing condition. The most common forms of the loop structure in traditional text-based programming languages are the DO-WHILE loop and the FOR loop and both of these structures are recreated in the LabVIEW object-oriented programming environment.
This lesson shall also introduce the concept of charts. In LabVIEW charts are a type of indicator that is used to display sets of correlated data, making it easier for a user to see relations across a dataset.
Loops
Both the WHILE and FOR loops are implemented in the Block Diagram window and selected from within the Functions Palette under the subfolder Structures.
The WHILE loop differs from the FOR loop in that the WHILE loop executes until a specified condition is met, whereas the FOR loop executes a set number of times.
The WHILE loop:
- Is controlled by a conditional terminal, which assesses the value of the controlling condition.
Always executes at least one time before being terminated.
The FOR loop:
- Is controlled by a count terminal.
Executes a set number of times as defined at the count terminal.
To place a loop in the Block Diagram, select the loop from the ‘Functions Palette >> Structures subfolder’, click and drag the loop around the section to be encapsulated within the loop on the Block Diagram. Blocks can then be dragged into or out of the loop structure to add them into or remove them from the looped section of code.
The WHILE Loop
Similar to a DO-WHILE loop in text-based programming languages, a LabVIEW WHILE loop executes a section of code, the encapsulated sub-diagram, until a condition is met.
This condition is assessed at the ‘conditional terminal’ (lower right), an input terminal that receives a specific Boolean control value.
Note: that the default behaviour of the conditional terminal is ‘Continue if TRUE’ - the sub-diagram shall continue to be executed repeatedly until the conditional terminal receives a FALSE Boolean value.
The ‘iteration terminal’ (lower left), an output terminal, stores the number of completed loops (as integer), where the first loop is counted as zero.
Loop diagram
The FOR Loop
A FOR loop executes its sub-diagram a predetermined number of times. The value fed to the ‘count terminal’ (N), an input terminal, is the control that determines how many times to repeat the sub-diagram.
Note: it is common practice to control the count terminal using a ‘constant’ within the Block Diagram, but the controlling value could also be determined from some other section of the code.
As with the WHILE loop the ‘iteration terminal’ (i), an output terminal, stores the number of completed loops, where the first loop is counted as zero.
Figure 1: LabVIEW Help - FOR loop
Charts
The waveform chart is a special numeric indicator that displays sets of correlated data, making it easier for a user to see relations across a dataset.
The waveform chart can be selected from the ‘Controls Palette >> Graph subfolder’ and placed on the Front Panel.
Waveform charts can be used to display a single set of data or multiple sets of data on the same plot.
The range of the x and y-axes can be adjusted by double clicking on the values of the axis and manually entering new limits. The axis auto-scale by default. The appearance of the chart can also be edited further by right-clicking on the legend of the plot and selecting from the various options.
Waveform chart
Wiring Data into a Waveform Plot
You can wire values directly into the waveform object on the Block Diagram to create a single waveform plot.
To display multiple sets of data on the same plot, the ‘Merge Signals’ object can be used to bundle multiple sets of data as illustrated.
The Merge Signals object is located in the ‘Functions Palette >> Express >> Signal Manipulation subfolder’. The default object takes two inputs but can be resized (using the Positioning Tool on the Tools Palette) to increase or decrease the number of inputs available.
Exercise 1: FOR Loops and Charts
In this exercise, students are required to create a program in LabVIEW (starting from the Blank VI template) that generates a random number twenty times and displays the readings on a Waveform Chart on the Front Panel. The program should use a FOR loop that includes a 100 millisecond delay, so that each iteration of the loop can be viewed in real time by the user as the VI is executed.
Figure 2: Block Diagram
Figure 3: Front Panel
Note: the delay function is located in the ‘Functions Palette >> Timing’.
Once completed, students should save the VI with an appropriate name, e.g.: ‘Basic For Loop.vi’.
Exercise 2: WHILE Loops and Charts
In this exercise, students are required to create a programme in LabVIEW (starting from the Blank VI template) that generates two random numbers continuously until the user presses a STOP Button on the Front Panel. Both random numbers should be illustrated on a single multichart. Note, on this occasion, students should use a WHILE loop structure with a Merge Signal Function in the Block Diagram. The WHILE loop should be controlled at its conditional terminal by a STOP Boolean control placed on the Front Panel.
Figure 4: Illustration of Multichart
Once completed, students should save the VI with an appropriate name, e.g.: ‘Basic While Loop.vi’.
Summary
This lesson has provided a brief introduction to the basic FOR and WHILE loop structures available in LabVIEW. These structures will be used extensively in later lessons with various aspects of their functionality expanded upon.
This lesson also covered the basic methodology of using a waveform chart as an indicator to display sets of correlated data.