Example 1 - counter with a gauge
This is a fairly simple program with simple user interaction. Once the program has been launched, it uses a while
loop to wait for the user input. This is a typical behavior of almost any user-friendly program. For example, if the user launches Microsoft Office, the program launches and waits for the user to pick a menu item, click on a button, or perform any other action that the program may provide. Similarly, this program starts execution but waits in a loop for the user to choose a command. In this case only a simple Start or Stop is available. If the Start button is clicked, the program uses a for
loop function to simply count from 0 to 10 in intervals of 200 milliseconds. After each count is completed, the gauge on the Front Panel, the GUI part of the program, is updated to show the current count. The counter is then set to the zero location of the gauge and the program awaits subsequent user input. If the Start button is clicked again, this action is repeated, and, obviously, if the Stop button is clicked, the program exits. Although very simple, in this example, you can find many of the concepts that are often used in a much more elaborate program. Let's walk through the code and point out some of these concepts.
The following steps not only walk the reader through the example code but are also a brief tutorial on how to use LabVIEW, how to utilize each working window, and how to wire objects.
Launch LabVIEW and from the File menu, choose New VI and follow the steps:
- Right-click on the Block Diagram window.
- From Programming Functions, choose Structures and select While Loop.
- Click (and hold) and drag the cursor to create a (resizable) rectangle.
- On the bottom-left corner, right-click on the wire to the stop loop and choose Create a control. Note that a Stop button appears on both the Block Diagram and Front panel windows.
- Inside the
while
loop box, right-click on the Block Diagram window and from Programming Function, choose Structures and select Case Structures. Click and (and hold) and drag the cursor to create a (resizable) rectangle. - On the
Front Panel
window, next to the Stop button created, right-click and from Modern Controls, choose Boolean and select an OK button. Double-click on the text label of the OK button and replace the OK button text withStart
. Note that an OK button is also created on the Block Diagram window and the text label on that button also changed when you changed the text label on the Front Panel window. - On the Front Panel window, drag-and-drop the newly created Start button next to the tiny green question mark on the left-hand side of the Case Structure box, outside of the case structure but inside the
while
loop. Wire the Start button to the Case Structure. - Inside the Case Structure box, right-click on the Block Diagram window and from Programming Function, choose Structures and select For Loop. Click and (and hold) and drag the cursor to create a (resizable) rectangle.
- Inside the Case Structure box, right-click on N on the top-left side of the Case Structure and choose Create Constant. An integer blue box with a value of 0 will be connected to the For Loop. This is the number of irritations the
for
loop is going to have. Change0
to11
. - Inside the For Loop box, right click on the Block Diagram widow and from Programming Function, choose Timing and select Wait(ms).
- Right-click on the Wait function created in step 10 and connect a integer value of
200
similar to step 9. - On the Front Panel window, right-click and from Modern functions, choose Gauge. Note that a Gauge function will appear on the Block Diagram window too. If the function is not inside the For Loop, drag and drop it inside the For Loop.
- Inside the For loop, on the Block Diagram widow, connect the iteration count
i
to the Gauge. - On the Block Diagram, right-click on the Gauge, and under the Create submenu, choose Local variable.
- If it is not already inside the
while
loop, drag and drop it inside thewhile
loop but outside of the case structure. - Right-click on the local variable created in step 15 and connect a Zero to the input of the local variable.
- Click on the Clean Up icon on the main menu bar on the Block Diagram window and drag and move items on the Front Panel window so that both windows look similar to the following screenshots: