• No results found

Local Variable Example

In document LabVIEW Basics II Course Manual (Page 110-114)

As an example of when you might need a local variable, consider how you would use a single front panel switch to control two parallel While Loops.

Parallel While Loops are not connected by a wire, and they execute simultaneously. First, we will study two unsuccessful methods using one terminal for the switch on the block diagram (Methods 1 and 2). Then, we will show how a local variable accomplishes the task (Method 3). The front panel of this example VI appears below.

Method 1 (Incorrect)

As a first attempt, we place the Loop Control terminal outside of both loops and wire it to each conditional terminal. In this case, the Loop Control terminal is read only once, before either While Loop begins executing.

Recall that this happens because LabVIEW is a dataflow language, and the status of the Boolean control is a data input to both loops. If a TRUE is passed into the loops, the While Loops run indefinitely. Turning off the switch does not stop the VI because the switch is not read during the iteration of either loop. This solution does not work.

Method 2 (Incorrect)

Now we move the Loop Control terminal inside Loop 1 so that it is read in each iteration of Loop 1. Although Loop 1 terminates properly, there is a problem with this approach. Loop 2 does not execute until it receives all its data inputs. Remember that Loop 1 does not pass data out of the loop until the loop stops. Thus, Loop 2 must wait for the final value of the Loop

Control, available only after Loop 1 finishes. Therefore, the loops do not execute in parallel. Also, Loop 2 executes for only one iteration because its conditional terminal receives a FALSE value from the Loop Control switch in Loop 1.

Method 3 (Correct)

In this example, Loop 1 is again controlled by the Loop Control switch, but this time, Loop 2 reads a local variable associated with the switch. When you set the switch to FALSE on the front panel, the switch terminal in Loop 1 writes a FALSE value to the conditional terminal in Loop 1. Loop 2 reads the Loop Control local variable and writes a FALSE to Loop 2’s conditional terminal. Thus, the While Loops run in parallel and terminate

simultaneously when the single front panel switch is turned off.

This simple example demonstrates the need for local variables. As previously shown, using the local variable gives access to a single front panel object from several locations on the block diagram. Local variables are also necessary when you cannot accomplish your goal using wires to carry the data.

Thus far, you have learned that you can read input data from controls and send results to an indicator. But, for example, what if you want to determine which parameters were used to run a VI previously and you want to place those values in controls for the users to modify? How can you write those

You cannot do this with standard controls and indicators. Using local variables, you can overcome this limitation. You can update a control from the block diagram. Also, you can have any number of local variable references for a given front panel control, with some in write mode and others in read mode. With a local variable reference, you can access a front panel object as both an input and an output.

To understand this concept, we will look at an example that shows another use of local variables. Below is a single string indicator. Suppose you want to update that indicator to display the loop that is currently executing.

Without a local variable, there is no way to accomplish this task. You can place the indicator terminal in only one loop.

However, using a local variable, you can access the same front panel indicator from more than one location on the block diagram, so that a single indicator displays the loop that is executing. The Which Loop? indicator is placed in Loop 1 and a local variable instance of that indicator is placed in Loop 2. Although this example is simple, it shows how an indicator can be updated from two separate locations on the block diagram.

Exercise 3-1 Login VI

Objective: To use local variables to initialize controls on the front panel.

Note Use this VI in the project in Lesson 5.

Front Panel

1. Open the Login VI in thec:\exercises\LV Basics 2directory.

The front panel of the VI is already created. Finish building the block diagram.

Block Diagram

1. Complete the block diagram. Notice that the local variables are enclosed in a single-frame Sequence structure, and that the empty string constant is wired to the border of the While Loop. This setup ensures that both local variables are updated before the While Loop starts.

a. Login Name local variable set to write local—Resets the login name to an empty string. To create this local variable, right-click the Login Name terminal and select Create»Local Variable from the pop-up menu.

b. Password local variable set to write local—Resets the password string to an empty string. To create this local variable, right-click the Password terminal and select Create»Local Variable from the

c. Empty String constant, available on the Functions»String

palette—Passes string values to the Login Name and Password local variables.

d. Login Name Property Node—To create this node, right-click the Login Name terminal and select Create»Property Node. Change the property to key focus and wire a TRUE Boolean constant to it.

e. Place the Verify Information VI on the block diagram. To select this VI, select Functions»Select a VI, navigate toc:\exercises\LV Basics 2, double-click the Verify Information VI, which you built in Exercise 1-2, and place the VI on the block diagram. This VI uses the name and password and checks for a match in a table of

employee information.

Note If you have trouble wiring the string constant to a local variable, right-click the local and select Change to Write Local.

2. Save the VI under the same name.

3. Return to the front panel and run the VI. Notice that the Login Name and Password controls reset to empty strings when you start the VI.

4. Now resize the front panel to show only the necessary objects, as shown:

5. Save and close the VI.

In document LabVIEW Basics II Course Manual (Page 110-114)