References: 1. http://www.ni.com/labview/ 2. http://en.wikipedia.org/wiki/labview Introduction: This lab continues from the previous lab by showing additional basic features of LabVIEW including For loops, arrays, dynamically built arrays, graphing, polymorphism, subvis, express VIs, and audio playback. Procedure: Part 1: Iteration in LabVIEW To understand the two looping mechanisms in LabVIEW: while and for. As seen in the previous lab, one way to handle iteration in LabVIEW is with a while loop. While loops are typically used when the number of loop iterations is not known in advance, thus the loop must continue while (or until ) some condition is reached. Sometimes the number of loops needed is known in advance; in these scenarios we use LabVIEW s for loop mechanism. To demonstrate this, let s have LabVIEW answer the question what is the sum of the numbers from 1 to 100? 1. Launch LabVIEW as done previously. 2. Click Create Project. Make sure Blank Project is selected then click Finish. This should open a LabVIEW project explorer window to your new project. Save your new project as Lab12. 3. Right-click on My Computer and create a new VI. Save it as Sum100.vi. 4. On the block diagram, place a Programming -> Structures -> For Loop as shown in figure 1. Note the N which sets the number of times the For loop is repeated and the i which represents the iteration number (i.e. index ) during execution. 5. Add a shift register to the For loop as was done previously with a While loop. Also place Add and Increment operators, wired as shown. Finally, set the constants as shown and add in Indicator to the shift register output. Figure 1: Sum100.vi Block Diagram 6. Note that the For loop index variable i starts at zero; hence the Increment operator so that we sum the numbers 1..100 instead of 0..99. Run your program and verify the answer to the above question. Hint: http://mathcentral.uregina.ca/qq/database/qq.02.06/jo1.html 7. Challenge: reconfigure this VI to accept a value for N from the user and solve arbitrary N problems. (That s the Greek letter sigma which means summation in mathematics.) 8. You may now save and close this VI. 1
Part 2: Arrays in LabVIEW To understand how LabVIEW represents and handles arrays of data. To demonstrate how to visualize data in LabVIEW using its graphing features. One of many impressive features of LabVIEW is its ability to handle and manipulate arrays with ease. An array is a linear collection of similar data values; for example an array of lottery numbers or an array of digitized voice samples. Another neat feature of LabVIEW is its ease in which we can visualize data using its graphing capabilities. Let s explore both of these features. 9. In the LabVIEW project explorer window, create another new VI and save it as Arrays.vi. 10. Refer to figure 2 to build this front panel. Right-click on the front panel and from the Modern -> Array, Matrix & Cluster subpalette, place four Array shells. Rename them to Array1, Array2, Array1*Scale, and Array1+Array2. Within each array shell, place a Modern -> Numeric -> Numeric Indicator which instantly defines each array as an array of numeric data. Resize the height of each array indicator to display 10 elements. 11. Also add a Modern -> Numeric -> Numeric Control and a Modern -> Waveform -> Waveform Graph. Relabel the numeric control to Scale. On the waveform graph, double-click on the X-axis label Time and change it to Element No.. Relabel the Y-axis to Value. You can also reposition and resize the graph legend to show 4 plot legend keys. 12. Refer to figure 3 to build the block diagram for this VI. Figure 2: Arrays.vi Front Panel Tips: The Array and Scale icons are shown in mini icon mode; right-click on each and uncheck View As Icon. Programming -> Array -> Build Array is configured to build a 2D array of arrays from 4 individual input arrays. Right-click it and choose Add Input or simply resize it to have 4 inputs. Also on the right-click menu, make sure Concatenate Inputs is unchecked. 2
Figure 3: Arrays.vi Block Diagram Dissecting this block diagram, the For loop creates two 10-element arrays using the neat tunnel feature, the first containing random values and the second containing the integer sequence 0,1,2..9. Recall orange indicates floating-point data and blue indicates integer data. These generated arrays go to the front panel along with a second pair of arrays resulting from add and multiply operations. The four arrays are then smashed up into a 2D array of arrays which finally get displayed on the front panel graph. Notice how LabVIEW shows scalars (single values) with a thin line while arrays are shown with thicker lines. Also, did you notice how you can freely mix scalars and arrays into a single operator (e.g. multiply)? This is a powerful concept called polymorphism in computer science and computer engineering which allows different forms (poly=many) of inputs and the operator or function will automatically adapt (morph=change) appropriately. How cool is that!? 13. Run your VI several times and verify the results on the graph display. If you get a flat line for Array1*Scale (i.e. an array of all zeros), you forgot to set the Scale control to a non-zero value. Adjust and rerun. Also try a negative Scale value! 14. Challenge: add a fifth array indicator labeled Array1*Array2 with appropriate changes to the block diagram. Include this 5 th array on the graph as well. Review the block diagram you just completed noting how little time it took you to develop it and the amount of work accomplished therein. It is interesting to note that this LabVIEW program does the equivalent of some 300-500 lines of code in a text-based object-oriented language such as C++, which would also consume many hours of development time! 3
Part 3: LabVIEW Blackjack, take 2 To show the application of a dynamically built array to represent a hand of cards as an improvement to the previous LabVIEW implementation of Blackjack. One problem we had in our previous implementation of Blackjack was that only the last card dealt and the total hand value was shown. Wouldn t it be spiffy if we could see all of the cards in the hand as the cards are being dealt? Bet you ve already guessed what we ll do in this part 15. Using Windows Explorer, find your Blackjack2.vi from your previous LabVIEW project. Copy (i.e. duplicate, don t just move) it into your new Lab12 folder. 16. From the LabVIEW Project Explorer, right-click on My Computer and select Add -> File. Select your copied Blackjack2.vi. Open this VI then from the front panel window, File -> Save As -> Rename then Continue Name this new version Blackjack3.vi. 17. Referring to figure 4 and the tips below, modify the block diagram as shown. The old Card # has been deleted, Current Card has been replaced with Hand and a new # Cards has been added. Tips: Figure 4: Blackjack3.vi Block Diagram Note the addition of a second shift register to accumulate the individually dealt cards into a Hand. The Array Constant is found in the Programming -> Array subpalette. Remember that as with the array indicators in part 2 above, this is just a shell until you insert a Programming -> Numeric -> Numeric Constant into the shell. The Insert Into Array and Array Size are also found in the Programming -> Array subpalette. The easiest way to add the Hand array indicator is to right-click on the blue line output of Insert Into Array and Create -> Indicator. On the front panel, resize the Hand array indicator to show at least 6 elements (cards) in the array. 4
18. Once you complete the mods, save the VI then run it a few times to see how it works. You should notice the Hand array dynamically grow each time the While loop iterates. 19. Show all three of your completed VIs to the instructor for completion credit of this lab. Challenge: instead of showing the cards as numeric 1..11 values on the front panel, show actual card images for each card (sike! Save this for a future course?) Part 4: Do you hear what I hear? To demonstrate basic audio capabilities of LabVIEW via calling a built-in subvi. To demonstrate an example of a LabVIEW express VI. Over the years, National Instruments and LabVIEW enthusiasts worldwide have developed a huge LabVIEW ecosystem of hardware and software IP (Intellectual Property). A large part of this ecosystem is built into current versions of LabVIEW. Two such areas of support available in LabVIEW are audio and image file handling. This part explores basic reading and playback of WAV audio files. 20. Per instructor instructions, acquire and save a copy of the given WAV audio file. 21. In your LabVIEW project explorer window, create another new VI and save it as PlayWav.vi. 22. Using figure 5 as a guide, place a Sound File Read Simple on your block diagram then add and connect a path control and a Waveform Chart to it. Note that this is the first you ve seen a VI invoked from within a VI; indeed, this subvi mechanism is equivalent to calling subroutines and functions in text-based languages. Figure 5: PlayWav.vi 23. Before running this VI, click on the folder icon adjacent to the path control then navigate to and click on the WAV audio file you saved above. Then run this VI. You should see a waveform displayed that looks like realtime audio. But what does it sound like? 24. As shown in figure 6, add a Programming -> Graphics & Sound -> Sound -> Output -> Play Waveform to your block diagram. Note that the blue shading of this element indicates it is an Express VI which is specially configurable by double-clicking. Do this, confirm the Device is correctly set and click Test Device. You should hear an audio test sound. Click OK then run this VI to have your ears process and decode the secret audio message. 5
Figure 6: PlayWav.vi with Play Waveform Express VI added 6 21-Nov-2013 :js