In the last tutorial I've described how to connect Arduino to LabVIEW and load first sample project. As you may realized the language used for development as well as the development environment is nothing like others. That's why in this tutorial I will focus on describing LabVIEW's interface as well as creating first application - blinking light.
After opening LabVIEW you can see starting screen. From there you can create new blank VI, open recent or existing project or create one from template. Also you can edit multiple settings going to appropriate menu options.
For now we are interested only in creating new blank VI. To do that go to File->New Vi or press Ctrl+N. You will see two screens appeared:
Front Panel is a user interface view for your application. Here you can place various GUI blocks. Later on we can get dipper into this view making better looking interfaces thanks to changing backgrounds, modifying displayed values etc. For now my interface will be rather simple. Good thing to remember is that closing this window automatically close whole VI.
Block diagram is view where you "write" your code. Here you only use GUI elements from front panel as a indicators or controls. We will mostly focus on this window where you can add loops, logic elements, and very sophisticated Express VIs. If you'll accidentally close this view to open simply press Ctrl+E or go to Window->Show Block Diagram.
Even though two windows have different purposes they share some important interface elements highlighted below.
Those buttons are responsible for running application. Looking from the left the first arrow is Run. It is used to execute program only once. Second one is Run Continuously. As a name suggests after pressing this one application will run until some type of stop signal won't be triggered. What's the actual difference between those two and when use which you'll see in just a moment. Next is Abort Execution. It will force application to stop no matter which option has been used to start it. The last one is Pause.
Other buttons on front panel are responsible for font modification and elements placement on the screen.
As for the block diagram there are five additional important buttons.
These are used for debugging purposes. From the left light bulb is Highlight Execution button. It shows how values are moving between blocks and on the wires. Next one is Retain Wire Value. This way values will stay on the wire and not disappear. Last three are used for in-debug navigation.
There is also one very useful button on the very right with question mark on it - Show Context Help Window. You will use this button a lot. After selecting it and hovering over any element you will see some basic explanation. For example what wires are used for each input of the block, or what values types are transferred on given wire.
As mentioned before each of the windows beside common blocks (displayed differently) there are also unique blocks that can be add only to this one view. To display Controls Palet as it's called press right mouse button on the panel are or go to View->Controls Panel. Yous should see something similar to this:
I will not explain each one of the blocks now. I'll do it as we'll go. Also if you will have some problems later use question mark button, or write it down in the comments. One more thing - because there are a lot of possible blocks and sometimes it is hard to find the one that you are looking for search box in Controls Panel is very helpful thing.
So that's all about interface for now, lets create some basic VI.It will turn on and of LED when switch will be pressed. Very basic project but will allow us to explain some basic ideas behind graphical programming and show how everything is connected (literally).
To start with place Round LED and Rocker on the front panel. You'll find both controls in Modern->Boolean. Place them as you want and that's all for the moment. Now switch to block diagram.
Here you should see two blocks - one rocker and one LED. Click you mouse on the arrow on the right side of rocker and then on the left arrow of the LED. You should have something like that:
This way you've just connected two blocks and created your first (very basic) program. Now when you'll get back to front panel and run it you will see almost nothing.... And this is why there are two different run buttons. In this case simple Run button will just send whatever signal is set on rocker to LED and that's all. Program will stop. To be able to change the state and see LED turning on and off for now you need to use Run Continuously button. To stop it press Abort Execution.
Even though Run Continuously looks like good solution with MakerHub's Arduino blocks it's not. There you need to use something else. In most cases you will use While Loop structure. To add this in block diagram go to Structures->While Loop. Now, when you selected it simply draw a square - that's your while loop.
For now you need to know that while loop has two control elements: marked as i information about iteration number (called Loop Iteration) and box with red circle used for stop signal (called Loop Condition). Also as each structure it allows signals to enter and exit through Loop Tunnels.
I'll explain deeper various loops in special tutorial about them, because the topic is a little bit complex.
Place now your blocks inside. The one thing you need to add is some control to Loop Condition. For now let it be simple control button. Press right mouse on the arrow and select Create Control.
You will see new button on front panel. Now press Run button. As you can see now it's not stopping and you can actually modify if the LED is on or off. To stop press newly added stop button.
Great now we have base for our Arduino Hello World application, that is blinking.
So, what we need to have for this:
- We need to connect to Arduino,
- We need to send signal automatically.
For now you know how to manually change the LED state, but we want to have it changed automatically, let say every 30 seconds. Writing typical Arduino script we will just do simple loop and count some temporary value. With LabVIEW the approach is a little bit different. Of course, you can use the old idea, but the problem here is that the number of iterations you need to count depends on the CPU you've got and it's speed, not Arduino.
Start with placing LINX blocks into our current project. In block diagram go to MakerHub->LINX and add Initialize and Close. Also from MakerHub->LINX->Peripherals->Digital take Write. Initialize and close place outside loop, while write inside. The Initialize block connections look like this:
We only need to pass LINX Ref wire to all of LINX blocks, but I also like to pass Error Out (it can be useful from time to time). So drag wire(s) to write block and then from write to close. As for the rest of initialize wires we only need to add Serial Port. To do that press right mouse button on appropriate connection and add control. This way you'll add new drop down list to front panel.
Now, for the Write block let's take a look at it:
Here, beside previously mentioned wires, we need to add DO Channel value as well as Output Value. For DO Channel you can pick either control or constant. Also, think what will be the difference if you'll place it inside or outside loop. For Output Value you need to use signal currently used for LED control. But we've decided we don't want to use rocker, right? Instead you need to add constant value (I hope now you know how to do this). Oh and delete rocker block - we won't use it anymore.
The question now is, how you can make the LED blink every 30 seconds. The simplest solution for now is to use Wait block (Timing->Wait (ms)). To define value of wait add it as a constant. Remember it is in milliseconds. Place this group inside loop (it's very important).
Just to make it clear, there are other options to do this. I am aware that this one is not perfect, and the main drawback here is the fact that whole loop is froze during waiting. But I've picked this one, because I thought it is the easiest way for the beginner.
The last part missing is altering the signal. I'll do it with partial explanation, just because it is using some extra feature from loop structure, and because I couldn't find any simpler solution I need to use this one. I'll explain in detail how does this work in later tutorials.
Place your bool constant outside loop. Select the loop with right mouse button and pick Add Shift Register from drop-down menu. Connect your bool constant to left shift register. Next from control panel add Not block (Boolean->Not). Use signal from left register as an input and connect output to right shift register. Now only thing missing is connecting missing signal from just created wire to LED and Output Value of Write block and you are good to go. You should end up having something like this:
Now connect Arduino to computer, pick correct COM port and run your application.
Next time I'll talk about reading values from Arduino - both digital and analog.
One more thing, I've forget to mention. There are two quite handy shortcuts you can use during development:
- Ctrl+B - removes broken wires
- Ctrl+U - cleans up diagram, that means place all blocks nicely (more or less). It's very useful f you have big diagrams.
1. I cannot run my application, because the arrow sign is broken.
If you see this sign, that means there is some error in your diagram. Most likely this is some wire connection problem (maybe some old wires are not deleted - use Ctrl+B).
2. When I run my application I get this error when I stop it:
Most likely if you see this, that means connection with you Arduino was not correctly established. Check if COM port is correct and check if you have firmware installed on Arduino.