Global variables are built-in LabVIEW objects. You can use variables to access and pass data among several VIs that run simultaneously. A local variable shares data within a VI; a global variable also shares data, but it shares data with multiple VIs. For example, suppose you have two VIs running simultaneously. Each VI contains a While Loop and writes data points to a waveform chart. The first VI contains a Boolean control to terminate both VIs. You can use a global variable to terminate both loops with a single Boolean control as shown in Figure 4.25. If both loops were on a single block diagram within the same VI, you could use a local variable to terminate the loops.
When you create a global variable, LabVIEW automatically creates a special global VI, which has a front panel but no block diagram. Add controls and indicators to the front panel of the global VI to define the data types of the global variables. Select a global variable as shown in Figure 4.25 from the Functions palette and place it on the block diagram. Double-click the global variable node to display the front panel of the global VI. Place controls and indicators on this front panel the same way you do on a standard front panel. LabVIEW uses owned labels to identify global variables, so label the front panel controls and indicators with descriptive owned labels.
Figure 4.26 shows a global variable front panel with a numeric, a string, and a cluster containing a digital and a Boolean control. The toolbar does not show the Run, Stop or related buttons as a normal front panel.
Figure 4.26 Global variable front panel.
After you finish placing objects on the global VI front panel, save it and return to the block diagram of the original VI. You then must select which object in the global VI that you want to access. Right-click the global variable node and select a front panel object from the Select Item shortcut menu. The shortcut menu lists all the front panel objects that have owned labels. You also can use the operating tool or labeling tool to click the local variable node and select the front panel object from the shortcut menu. If you want to use this global variable in other VIs, select
Functions»All Functions»Select a VI. By default, the global variable is associated with the first
front panel object with an owned label that you placed in the global VI. Right-click the global variable node you placed on the block diagram and select a front panel object from the Select Item shortcut menu to associate the global variable with the data from another front panel object.
The two common problems that globals will cause in an application are race conditions and performance problems. These problems are pretty easy to diagnose and relatively easy to fix once you understand them. A race condition occurs when more than one piece of code needs to update a global and there is no way of ensuring that they take turns and leave the global in a valid state. The solution is to control the access and making the code take turns. The performance problem is that it is also possible to use globals in ways that greatly slow down your VI’s execution. This is very different from a race condition in that the program executes correctly, but it takes longer than it should to complete. There is a very good solution that will actually make the code easier to read at the same time it restores the performance. To speed this up, make a subVI that takes the parameters for indexing the global and returns the data. Local and global variables are advanced LabVIEW
concepts. They are inherently not part of the LabVIEW dataflow execution model. Block diagrams can become difficult to read when you use local and global variables, so you should use them carefully.
SUMMARY
● Use structures on the block diagram to repeat blocks of code and to execute code conditionally or in a specific order.
● The While Loop executes the subdiagram until the conditional terminal receives a specific Boolean value. By default, the While Loop executes its subdiagram until the conditional terminal receives a TRUE value.
● The For Loop executes a subdiagram a set number of times.
● You create loops by using the cursor to drag a selection rectangle around the section of the block diagram you want to repeat or by dragging and dropping block diagram objects inside the loop.
● The Wait Until Next ms Multiple function makes sure that each iteration occurs at certain intervals. Use this function to add timing to loops.
● The Wait (ms) function waits a set amount of time.
● Coercion dots appear where LabVIEW coerces a numeric representation of one terminal to match the numeric representation of another terminal.
● Use shift registers on For Loops and While Loops to transfer values from one loop iteration to the next.
● Create a shift register by right-clicking the left or right border of a loop and selecting Add
Shift Register from the shortcut menu.
● To configure stacked shift register to remember values from multiple previous iterations and carry over values to the next iteration, right-click the left terminal and select Add
Element from the shortcut menu.
● The feedback node stores data when the loop completes iteration, sends that value to the next iteration of the loop and transfers any data type.
● Use the feedback node to avoid unnecessarily long wires. ● Local variables allow data to be passed between parallel loops. ● Global variables allow data to be passed between VIs.
MISCELLANEOUS SOLVED PROBLEMS
Problem 4.1 Create a VI to find the factorial of the given number using For Loop and Shift Registers.
Solution The front panel has the number and its factorial, while the block diagram contains the codes to solve the problem as shown in Figures P4.1(a) and P4.1(b).
Figure P4.1
Problem 4.2 Create a VI to find the sum of first n natural numbers using a While Loop with a feedback node.
Solution The front panel and the block diagram to solve the problem are shown in Figures P4.2(a) and P4.2(b). Given a number n, the sum of first n natural numbers is obtained when the program is run.
Figure P4.2
Problem 4.3 Create a VI to change the state of the Boolean indicator n times between TRUE and FALSE.
Solution Build the front panel and the block diagram as shown in Figures P4.3(a) and P4.3(b) to solve the problem.
Problem 4.4 Create a VI to find the sum of first 10 natural numbers using a For Loop.
Solution The front panel and the block diagram to solve the problem are shown in Figures P4.4(a) and P4.4(b).
Figure P4.4
Problem 4.5 Create a VI which converts a decimal number to a binary number using For Loops.
Solution The front panel and the block diagram to convert a decimal number to a binary number are shown in Figures P4.5(a) and P4.5(b).
Figure P4.5
Problem 4.6 Create a VI to find the factorial of a given number using a While Loop.
Solution The front panel and the block diagram to find the factorial are shown in Figures P4.6(a) and P4.6(b).
Figure P4.6 Problem 4.7 Create a VI to find nc
r and npr of a given number using a For Loop.
Solution The front panel and the block diagram to find nc
r and npr are shown in Figures P4.7(a)
and P4.7(b).
Figure P4.7
Problem 4.8 Create a VI to animate a bird flying. Use the Picture Ring control to insert and display pictures to be animated.
Solution The front panel and the block diagram to solve an animate-a-bird-flying problem are shown in Figures P4.8(a) and P4.8(b).
Figure P4.8
Problem 4.9 Create a global VI (Global1.vi) which consists of a knob and a stop button. Create another VI (plot.vi) consisting of a waveform chart. Update the values of Global1.vi’s knob and plot.vi’s slide in the waveform chart. Press the stop button of the Global1.vi to stop both the VIs.
Solution The front panel is shown in Figures P4.9(a) and P4.9(b) and the block diagram to solve the global variable problem are shown in Figure P4.9(c).
Problem 4.10 Using the Global1.vi created in Problem 4.9, write the variation of knob values in a file using Write to Measurement File.vi. Stop the VI using the stop button of Global1.vi.
Solution The block diagram to solve the problem is shown in Figure P4.10.
Figure P4.10 Block diagram.
Problem 4.11 Create a global VI (Global2.vi) with a waveform chart and a stop button. Update the waveform chart using a random number generator from another VI (chart.vi). Stop the VIs by pressing the stop button of any one of the VIs.
Solution The front panel is shown in Figures P4.11(a) and P4.11(b), and the block diagram to solve the problem is shown in Figure P4.11(c).
Figure P4.11
REVIEW QUESTIONS
1. What is looping in LabVIEW? State the advantages of using loops. 2. What are the different types of loops used in LabVIEW?
3. What is a For Loop? Under what circumstances are For Loops used? 4. What is a While Loop? Under what circumstances are While Loops used? 5. How does a While Loop vary from a For Loop?
6. What is the difference between using conditional terminals, Stop if True and Continue if True in a While Loop?
7. What is a tunnel? How is it used in loops?
8. Why are shift registers and feedback nodes used in loops?
9. What is the difference between a shift register and a feedback node? 10. What are stacked shift registers? When are they used?
11. Describe the need for initializing shift registers and feedback nodes.
12. What is the difference between connecting the feedback node before and after branching? 13. What are the functions available in LabVIEW to control the timing of loops?
14. How can you control the speed at which the loop executes? 15. What is a coercion dot?
16. What are the advantages and disadvantages of local and global variables?
EXERCISES
1. Find the sum and average of the given numbers.
3. Program to display a name 27 times using a For Loop. 4. Program to find the sum of first 100 natural numbers.
5. Program to display the sum and average of the given numbers using a While Loop. 6. Program to display the numbers from 1 to 100 in steps of 3.
7. Write a program in LabVIEW to find the square of the numbers from 1 to 100 using (a) a
For Loop and (b) a While Loop.
8. Write a program to display the numbers and its cube from 1 to 10 using (a) a For Loop and
(b) a While Loop.
9. Write a program in LabVIEW to print the number, the square and the cubes of only even
numbers from 0 to 100.
10. Write a program in LabVIEW that finds the factorial of the given numbers using (a) a For
Loop and (b) a While Loop.
11. Write a program in LabVIEW that finds whether the given number is a prime number or not
using (a) a For Loop and (b) a While Loop.
12. Write a program in LabVIEW to find the sum of the following series using (a) a For Loop
and (b) a While Loop: (i) 1 + 2 + 3 + … + n
(ii) 1 – (1/1!) + (2 / 2!) – (3 / 3!) (iii) x + (x2/2!) + (x4/4!) + … + (xn/n!)
(iv) x – (x3/3!) + (x5/5!) + … + (xn/n!)
13. Write a program in LabVIEW to solve a quadratic equation to check all possibilities of
their coefficients (x2 + bx + c = 0).
14. Write a program in LabVIEW to generate a Fibonacci series of n numbers where n is
defined by the programmer. Example for the Fibonacci series is: 1 1 2 3 5 8 13 21 34 and so on.
15. Write a program in LabVIEW to read a positive number n and to generate the following
number series using (a) a For Loop and (b) a While Loop: (i) 1, 2, 3, 4, …, n
(ii) 0, 2, 4, 6, …, n (iii) 1, 3, 5, 7, …, n (iv) 1, 22, 32, 42, …, n2
91
5
ARRAYS
ARRAYSARRAYS
ARRAYSARRAYS
5.1 INTRODUCTIONA group of homogeneous elements of a specific data type is known as an array, one of the simplest data structures. Arrays hold a sequence of data elements, usually of the same size and same data type placed in contiguous memory locations that can be individually referenced. Hence arrays are essentially a way to store many values under the same name. Individual elements are accessed by their position in the array. The position is given by an index, which is also called a subscript. The index usually uses a consecutive range of integers. Some arrays are multi-dimensional, but generally, one- and two-dimensional arrays are the most common.
You can consider using arrays when you work with a collection of similar data and when you perform repetitive computations. Arrays are ideal for storing data you collect from waveforms, graphs, data generated in loops or data collected from multiple sensors. LabVIEW like other conventional structured languages uses arrays and data structures (data structures include clusters and type definitions which are discussed in later chapters) for this purpose.