An Introduction To Using LabView - Using A Timed Loop

In the first LabVIEW note we examined some simple arithmetic operations in LabVIEW.  In this note we will examine how to write a simple program with a timed loop.  After all, it isn't enough just to do arithmetic.  In most applications you need to do calculations over and over again.  Repetitive calculations are best done using a timed loop.

Timed loops are important because you often encounter situations where you want to take measurements repetitively at a constant time interval.  Timed loops let you take data where you can set the timing between measurements.  Before getting to timed loops, we will walk you through some simple loops, and we will end with some timed loops that generate and display sinusoidal signals.

In this note - as in the other LabVIEW notes, you could just read through the note, but you will do better if you work along as we discuss what is happening.

The first thing you are going to do is to construct a simple timed loop.   You need to do the following.

• Start LabView.  Choose New vi.
• We will use some of routines that we have written previously.  You will need to have the Diagram window active.  Choose Window-Show Diagram if the diagram is not active and showing.
• Choose Window-Show Functions Palette.  The Functions palette is shown below.
• On the Functions palette locate the Structures menu (top left) and click to get options.  As you put the cursor over the different menus, a small pop-up window lets you know what the menu contains.
• Place a While Loop on the Block Diagram.
• This is what you get.  Make it at least this size.
• You should notice an iteration counter (i) and a Loop Stop Conditional Terminal.  The while loop executes as long as the Conditional Terminal is true - although you can change that yourself.
• To change the conditional terminal to look like the one above in the lower right corner - if it happens to look like a little stop sign - right mouse click on the icon and set to Continue if True.
• We will write a loop that executes for a number of times that you specify - eventually.  But for now, we are going to add a few things to make this run.  First try to run the vi.  You should get an error message that the conditional terminal is not wired.  That's not the only problem - but it is the problem that you will fix first.
• To start, go to the Front Panel, and add a Numeric Indicator from the Numeric menu.  When you have the numeric indicator on the front panel, right click the indicator and choose Find Terminal.  You will be taken to the diagram and the terminal will be evident.  Note that adding the indicator on the front panel automatically adds a terminal for the indicator to the diagram.  There are some components that can only be added - or deleted - from the front panel or the diagram - even though they must appear on both.
• Move the terminal inside the while loop.
• Change the representation to an integer format.  The control should be blue when you have it right.  (Blue indicates an integer.)
• Now, add a constant to the Conditional Terminal by right-clicking on the terminal to add a constant.  Make that constant equal to True using the hand tool.
At this point, your block diagram should look like this.

• Now, you can run the program.  All that happens is that the indicator shows the loop counter - and it will show that each loop takes very little time!
• As the numbers on the indicator rapidly zip by, hit the Stop button (looks like a stop sign) just under the menu.
That's your first excusion into the world of loops.  The loop you wrote should have executed very rapidly and should have displayed a rapidly increasing number in the indicator on the front panel.  We can now add a timer to the loop to control how rapidly it executes.
• Next, we will set the timing of the loop execution.
• On the time menu select the timer with a metronome icon and place it inside the loop
• Right click the timer to add a constant.  Make that constant equal to 1000 (That's 1000 milliseconds!) using the text tool.
• Your diagram should look like this.  (Except that in generating the picture below, we may have lost the wire going from the TRUE constant to the conditional terminal.  That wire should be there.  It is just not showing in the picture.)

At this point you have a loop that is timed, and since the input to the timer is 1000 (that's milliseconds) it always runs at 1 second intervals.  There are two things we want to do to improve this program.

• We want to be able to set the time interval (in seconds).
• We want to display the elapsed time (in seconds).
We will take care of those two concerns next.
• To set the time interval, you need to add a numeric control on the front panel.  Do that now.  NOTE:  You need to use a control that permits floating point inputs if you want to time the loop at intervals like 1.5 seconds.  Add that control now.
• After adding the control, right mouse click on the control and locate the terminal for the numeric control you just added.
• Then you need to wire the control so that it controls the input to the timer block.  Up above we just used a constant of 1000 to get one second.  If you input the time interval in seconds, you will need to multiply the time interval that the user inputs by a factor of 1000 (to get the timer interval in milliseconds), so you will need a multiplier and a constant input to one side of the multiplier equal to 1000.
Now, your block diagram should look something like this.

• Now, run your program.  Of course, the only thing this program does is to tell you how long it has been running.  It's pretty useless unless you use it as an egg timer.

Problems

P1      Modify the program above so that you can stop the program using an ON-OFF control.  Here's a hint.  Try replacing the constant the is wired to the conditional terminal with a control.  Right-click the constant (set to TRUE in the block diagram above) and change it to a control.  Then find the control on the front panel.

One final program - just to end this by producing something that's getting close to useful.  (The next lesson in this series will take you to the world of IEEE-488 (GPIB) measurements, and when you put them in a loop, then you have some interesting things that you can do.)  Anyhow, this last program will compute a sine wave and display it on the dial.  Here is the block diagram.

You should notice the following in this block diagram.

• In the previous vi we computed the elapsed time and displayed it on the front panel.  Here we take that time and multiply it by a factor that is 2pf.
• The 2p factor is a constant built into LabVIEW.  You can find it on the panel of numeric operations, and you should check out all of the other constants available.
• The result of the multiplications is the input to a sine function.  You can find that function on the numeric palette of functions in a sub-palette of trigonometric functions.  Check out what else is there as well.
• Finally, the output of the sine function is displayed on a Meter indicator.  That indicator has a front panel portion, and you should add it from the front panel.
• Actually, the front panel looks like this when you are done.

Problems

P2      Modify the program above so that you can input the amplitude of the sine wave.

And that is about it for this lesson.  In the next lesson we'll get you acquainted with some simple IEEE-488/GPIB measurements, and after that we'll do those measurements inside a loop.