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.
At this point, your block diagram should look like this.
- 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.
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.
- 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.
- 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 will take care of those two concerns next.
- We want to be able to set the time interval (in seconds).
- We want to display the elapsed time (in seconds).
Now, your block diagram should look something like this.
- 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, 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.
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.
You should notice the following in this block diagram.
P2 Modify the program above so that you can input the amplitude of the sine wave.