Objective
To learn programming using LabVIEW using simple addition programs.
Launch LabVIEW from Start-> Programs-> National Instruments LabVIEW 8.6. LabVIEW is big, so it might take some time to load. You should see this window:
Figure : LabVIEW Getting Started Window at startup.
In the Getting Started window, click on Blank VI, and two windows will come up, the Front Panel and the Block Diagram windows. Before starting with the program we need to make sure we have the Functions Palette (this palette contains all the "blocks" of programming that we will need to "write" our program") and the Context Help (to see short descriptions of the blocks as and when we use them). To do this, first check if you already have these two small windows on your screen
Figure : The Controls Palette and the Context Help
The above figure would be seen if the window in focus is the Front Panel, i.e, if the two main windows look like this
Figure : The Front Panel and Block Diagram windows, with Front Panel in focus.
If the windows are the other way around, make sure the Functions Palette and the Context Help are visible on the screen.
Figure : The Functions Palette and the Context Help windows as seen if the Block Diagram is in focus.
Now, we are set to start the program. Switch focus to the Front Panel window. Here we are going to create two Controls, through which we can input numbers, and one Indicator, through which we are going to display the sum of the two numbers input. To do this, first create a Numeric Control as shown below
Figure : The selections shows Numeric Control, which is the one we need to place within the Front Panel
Create two such Numeric Controls and a Numeric Indicator as shown below.
Figure : Two Numeric Controls (Numeric and Numeric 2) and one Numeric Indicator (Numeric 3)
Now that the Numeric Indicator and Controls have been placed, we need to make the "connections" to use the Controls and display their sum in the Indicator. To make these connections, we need to switch to the Block Diagram. In the Block Diagram, we place an Add block and make the connections as shown below.
Figure : The Add Block on the Numeric menu of the Programming menu in the Functions Palette. Select Add and click within the Block Diagram to Place the block.
Figure : The Block Diagram window with the Numeric Controls connected to the input of the Add block and the output of the Add block connected to the Numeric Indicator.
To make the connections, click on the terminal on the Numeric Control (Numeric) and move the mouse and click on the 'x' terminal of the Add block. Similarly click on the terminal of Numeric 2 and move the mouse and click on the 'y' terminal of the Add block. Now click on the 'x+y' terminal on the Add block and move the mouse and click on the terminal on Numeric 3. Now all the connections are set, and the program is all set to run.
Switch to the Front Panel, and enter a number each in the boxes under Numeric and Numeric 2 as shown.
Figure : Two numbers entered in the Controls boxes. The program will give the sum of these two numbers when the white arrow button is pressed
Now, press the Run button. This runs the program, and the sum of the two numbers is displayed in the box Numeric 3. Notice that, the program gives the output and stops running. To add two other numbers you should change the entries in the Numeric and Numeric 2 controls and run the program again to see the new sum.
Figure : This shows the output of the program after the two numbers have been added. Note that on pressing thebutton, the program does not work like a calculator, i.e, it does not let you input numbers again and add them automatically.
To make the program run continuously, so that we can keep changing the input numbers and see the sum of the two numbers as we change the numbers, we need to introduce a "While Loop". With a while loop, we can make this program until we ask it to stop. To do this, let us introduce a STOP button in the Front Panel and a While loop in the Block Diagram. First, let us switch to the Block Diagram and create a While Loop.
Figure : Introducing the While loop into the Block Diagram window.
Figure : Once the While loop is drawn, we will need to switch to the Front Panel and introduce a Stop button which stops the program when we need to.
To make this stop button work, we need to connect it to the "Loop Condition" of the While loop. To do this, switch to the Block Diagram and connect the terminal on the Stop block to the Loop Condition. Now, this would ensure that pressing the Stop button on the Front Panel would stop the program and until the Stop button is pressed, the program will keep running, i.e, the two numbers will be added over and over again. On pressing the the Front Panel will look like this:
Figure : This figure shows that the programming is repeatedly adding Numeric and Numeric 2 and displaying it in Numeric 3. Notice that the run
button is now indicating that the program is still running.
To see if the program does the intended purpose, change the numbers and check what happens to the displayed result. Also, click the Stop button and check if the program stops.
CHECKPOINT : Why did we have to use a while loop? Why did the program that we wrote earlier stop after adding the two numbers once? What changes made the difference and how?
You would have notice that there is a which is the loop iteration counter. Let us see what this counter does by connecting an indicator to it.
Figure : This shows the an indicator connected to the Loop Iteration Counter. To do this, right click at the terminal on the Loop Iteration counter and select Create Indicator.
CHECKPOINT: Run the program and see what is displayed in the box labeled Numeric 4 on the Front Panel. What do you think this number is?
Let us move the indicator outside the while loop and see how it changes the display on Numeric 4. To do this, first click and drag the Numeric 4 block (in the Block Diagram) outside the While Loop. Now you will notice that there is a "broken" connection. Click on the wire as shown below and press the Delete key. Then make a connection to the Numeric 4 block with the Loop Iteration block.
Figure : Numeric 4 brought outside the While Loop. Note the error message about the loose ends of the wire.
Figure : The connection through the while loop to the Numeric 4 outside.
CHECKPOINT: Now, run the program again, and see what is displayed on the Front Panel under the Numeric 4 display. What is different now? Why? What does it mean to have an indicator of the Loop Iteration counter outside the while loop?
Now, let us answer the above question by "seeing" what happens when the program runs. To do this, click on the Highlight Execution button, in the Block Diagram window. Now, run the program, and switch to the Block Diagram window to see what the Highlight Execution does. A screenshot is shown below
Figure : The Block Diagram window with Highlight Execution turned on and the program running. Can you identify what the boxes with the numbers mean? Also, what does the 'F' on the Stop block mean?
The next step is to make the program add two numbers and display the sum after a brief delay. For this example, let us make the delay to be 1 second. To do this, we make use of a new structure called the Flat Sequence Structure in the Structures sub menu under 'Programming' in the Functions Palette and a new block called the Wait block. The following screenshots will detail this procedure.
Figure : Delete the wire between the add block and Numeric 3 and select Flat Sequence Structure from the Functions Palette.
Figure : Click and draw a rectangle to create a frame of the Flat Sequence Structure as shown above.
Figure : The Wait block is in the Timing sub menu under Programming, in the Functions Palette.
Figure : Place the Wait block inside the Flat Sequence Structure as shown above.
Figure : The left terminal of the Wait block has a terminal which says "milliseconds to wait" when you mouseover it. Right click on this terminal and select Create -> Constant
Figure : Double click on the box that appears and type 1000 inside and press enter.
The next step is where we make the delay feature into the program. In the above figure, you can see that the Add block is not connected to the Numeric 3. Now, we make the connection through the Flat Sequence Structure.
Figure : Click on the 'x+y' terminal on the drag a wire through the Flat Sequence structure to look like the above figure. Then click again so that you make the connection as shown below.
Figure : Once you click, the Block Diagram should look as above. Now, you have made a connection such that, the sum of the two numbers will be taken into the Flat Sequence structure, and will be taken out, as soon as all the blocks within the Flat Sequence structure have been evaluated.
Figure : Click on the orange box on the right side of the Flat Sequence structure and move the mouse, and click on the terminal on Numeric 3. This completes all the connections that we needed to make for the time control of the program.
CHECKPOINT: Turn on Highlight Execution button on. Run the program, change the numbers in Numeric and Numeric 2 and see what happens in the Block Diagram. Do you notice the difference? What do you think the Flat Sequence Structure does here? Click on the Stop button. Does this also happen after the delay? Why?
You would have noticed that the program is passive, i.e, it takes two numbers in, and adds them, but there is not decision making involved. What if we want the program to display a message if the sum is 100? This section explains how we can do it in LabVIEW. In LabVIEW, decision making is handled through the use of Case Structures. The following explains how we can introduce the Case Structure into our addition program.
As a first step, let us remove the Flat Sequence Structure and bring the program back to the state where it did just the addition without any delay.
Figure : Delete the Flat Sequence Structure and make the connection between the Add block and the Numeric 3 indicator block.
Figure : The Case Structure is in the Structures sub menu under Programming in the Functions Palette.
Before continuing further, let us move the blocks around a bit so that we make a little more space for the blocks and structures that we are going to introduce as shown.
Figure : The Block Diagram cleaned up to make space for new blocks which are going to be placed.
Now, as introduced before, let us draw a Case Structure within the While Loop.
Figure : Case Structure within the While Loop
Now, the following figures will give you the order in which we are going to place the blocks which would make the program give a message when the sum is a particular number, say, 100.
Figure : Draw a Flat Sequence Structure within the Case Structure. This figure also shows the way to choose a One Button Dialog box, through which we are going to display a message to the user, when the sum is 100. This block is under the Dialog & User Interface sub-menu under Programming in the Functions Palette.
Figure : One Button Dialog block placed within the Flat Sequence Structure. The mouse pointer is at the 'message' terminal of the One Button Dialog block and the next step will show how to place the message.
Figure : Right click on the message terminal and select Create -> Constant to display a pink box.
Figure : Click on the pink box to type a message. When done typing, click outside the pink box to finish.
Figure : Right Click on the Flat Sequence structure and select Add Frame After in the dropdown menu.
Figure : In the frame that is created, insert a Wait block with 2000 milliseconds to wait, as before.
CHECKPOINT: What do you think this Flat Sequence Structure will do?
The next step is to make the comparison and make the "decision" when the sum is 100. The following figures will show the way to do this.
Figure : From the Functions Palette choose the Equal? block from the Comparison submenu under Programming and place it as shown below.
Figure : Equal Block placed in the Block Diagram.
Figure : Wire the 'x+y' terminal of the Add block to the 'x' terminal of the Equal? block and create a constant with value 100 at the 'y' terminal of the Equal? block.
Figure : Connect the 'x= y?' terminal of the Equal? block to the (Case Selector of the Case Structure).
CHECKPOINT: Run the Program from the Front Panel. Try entering numbers that add to 100 in Numeric and Numeric 2. What happens? See what happens if you don't change the numbers after pressing OK in the dialog box. Why does this happen? What happens if you change the numbers after pressing OK?