Tutorial 5: Structures in LabVIEW: In our last tutorial, we have seen how to form a weather station using LabVIEW and Arduino which shows the ambient temperature and intensity of light on the front panel of LabVIEW. In previous tutorials we worked on basics of LabVIEW and its interfacing with Arduino. We used analogue and digital read and write blocks, PWM and controls. They were very basic functions of LabVIEW. In today’s tutorial we will move to some advance level and see how to use different structures. They help us in iterating some peace of code more than once. These functions give LabVIEW many extra powers as a programming language. I recommend you to check this getting started tutorial on labview.
We will discuss following structures here in detail and get ourselves familiarized with their use and their power.
- Loop Structure
- While Loop Structure
- For Loop Structure
- Case Structure
- Sequence Structures
- Flat sequence structure
- Stacked sequence structure
Table of Contents
While Loop Structures in labview
The While loop structure keeps on running a program placed inside it until a stop condition is satisfied. While loop structure is used to keep a program running until a STOP button is pressed (clicked). Following While loop structure keeps on running and generating different values until a STOP condition is satisfied which is generated value is equal to 50.
- Create While loop as follows:
- Create random number generator as shown.
- Create multiply sign as follows.
- Create constant 100.
- Create “round of to -infinity” as shown
- Create “Equal to as follows”
- Create constant 50 as done previously.
- Create indicator for output of “round of to – infinity” as
- Wire the blocks as shown below
- Now run the program and you will see as soon as random number generator generates value 50. While loop STOP generating further values.
- We have included “wait timer” so that you can see values being generated on “front panel” of LABVIEW. Its value is in milliseconds. Here 200 milliseconds. It introduces delay of 200 seconds between iterations of loop.
For Loop structure in labview
A for loop structure repeats the program a specific number of times. In For loop we have “N” which is count terminal and “i” which is iteration terminal. Value of “I” changes from 0 to N-1 each time loop executes.
- Crete for loop as we created while loop previously from structures.
- Create constant for input of N
- Create indicator for output of i.
- Create a “wait” timer as done in while loop
- RUN the program in LabVIEW. You will see on front panel of LabVIEW “current value” keeps on increasing. Until it reaches to N-1 which is 14 in this case.
- You can also store values of “i” in an array as follows.
- Crate indicator for output of “i” outside the for loop.
- Run the program in LabVIEW you will see as the program ends all the values will be shifted to array on front Panel of LabVIEW.
- You can also create graphs from arrays. Draw a block diagram like shown below. You can find all blocks by going to search bar and typing names of these block as shown on the top of blocks like “pi”.
- Run the program from LabVIEW and observe the graph on the front panel of LabVIEW.
Auto-Indexing a For Loop
When working with arrays, it is often useful to work with each element in the array one at a time, one after the other. LabVIEW will do this automatically whenever you wire an array to an input tunnel in a For Loop, if the tunnel is enabled for indexing. This is called auto-indexing the For Loop.
- When indexing is enabled on the input tunnel, the value of N is set to equal the number of elements in the array, and one array element enters the For Loop each time the loop cycles.
- When indexing is disabled on the input tunnel, the value of N is not set (this
generates an error if the value of N is not set in some other way) and the entire
array is sent into the For Loop immediately.
- Arrays can be wired to While Loops input tunnels too, but the While Loop is
In LabVIEW Case Structures allow specific programming part to run depending on the value of variable. Case structure can be found in LabVIEW in structure palette.
- You need to draw to cases one for true value and other for false value. They can be selected from to menu of case structure in LabVIEW.
- In this example we want to calculate log of number as log is defined for only positive values so we monitor this condition
- True case is as follows.
- False case is this one.
- Run the program in LabVIEW and put the value of x first a positive value in front panel of LabVIEW and then a negative value in front panel of LabVIEW.
- Make sure you run a valid value. Then stop front panel of LabVIEW (done automatically) and then change the value in front panel of LabVIEW and again run the program to observe both cases in LabVIEW.
- Result of true case in front panel of LabVIEW.
A sequence structure is used when we require some program to be executed in sequential order. In LabVIEW you cannot always control the order of execution. When next calculation depends upon previous one, we are sure it will occur in correct order. But when calculations are in parallel, we cannot control the order in which calculation take place. A sequence structure is used to force calculations to take place in a defined sequence.
- Form a diagram as shown below
- Run the program and you will see result like this on.
- This program generates the two random numbers for 4 seconds and then displays there addition for 2 seconds.
- In this example the Flat Sequence structure was used. The Flat Sequence structure shows all of the frames (steps) in the block diagram, but can take up a lot of space if there are several frames. The alternative is the Stacked Sequence structure. With the Stacked Sequence structure, the frames are still sequential, but they are stacked one on top of the other (like the Case structure). You use the Selector Label to choose the displayed frame.
I am closing this article with the hope that you grasped the structures concept thoroughly and would be able to implement them.