General Strategy.
Before beginning, make a list of the programming parameters (VARIABLES) which you would like to be able to change within the program. All variables in LabView are either CONTROLS or INDICATORS. They are distinguished by their appearance in the block diagram. Controls are variables which you can change. Indicators simply display the value of the variable. You must note the difference when you wire the block diagram.
Programming and Algorithm Development.
Algorithm
development: After you
define the problem, develop a logical, well thought out set of steps before you
embark on “translating” the steps into a computer language:
(A) Define the quantities you want to input into your program, and what you would like it to output. Write these down.
Inputs: Numbers that the user enters, text that the user inputs (e.g. answers to questions or prompts).
Outputs:
Numbers that
the computer calculates, text that the computer generates, graphical
representation of the data.
(B)
Prepare an algorithm or series of step-by-step instructions of how you
will proceed.
(C) Translate (A) and (B) into computer language
Description
of Programming Terms and Structures.
(A) Variables: Variables are actually “placeholders” in computer memory
whose values can be changed during the execution of a computer program and can
be used in calculations within a program. Variables
take on the following types and, in LabView, they are specified to be either
controls or indicators.
(1)
BOOLEAN variables can take on only two values (TRUE or FALSE) and are used
primarily in conjunction with CASE statements described below.
A boolean variable is placed on the front panel of a LabView VI from the
control panel. (GREEN on the block diagram)
(2)
NUMERIC variables can take on “any” number (there are limitations as to the
size of this number). Numeric
variables can be either:
(a) INTEGER (i.e. -2, 0 10, -35) (BLUE on the
block diagram)
(b) DECIMAL (i.e. -2.13, 5.162, 1.0) (ORANGE
on the block diagram)
(3)
STRING variables can take on any group of characters (i.e. “A”, “P-Chem”,
“Input File Name”) (PINK on the block diagram)
(B) Data Structures:
Data structures are sets of variables (which are themselves either
controls or indicators) which are grouped together in organized ways.
As with variables, data structures can be either controls or indicators.
(1)
ARRAYS are 1-dimensional or 2-dimensional groups of variables of the same type
which can either be called as a group (i.e. to plot a Y-array vs. an X-array on
a graph), or an individual element in the group can be accessed (i.e.
Y-array[1]). You will plot arrays
which you generate in your program on a graph, simply by wiring them in as the X
and Y axes.
(2)
CLUSTERS are groups of elements which are “bundled” together.
The elements can be of different types.
An example of this is a “record” of student information which might
appear in a database:
Student
Name (a STRING)
Student
ID # (an INTEGER or a STRING)
Student
GPA (a DECIMAL)
(C) Looping Structures:
Looping structures are used whenever you want to do a calculation
repetitively. For example, if you
have an array of integer numbers, and you want to generate a new array of
integers by looping through N number of times, where N is the size of the array,
you would probably use a FOR LOOP. Alternatively,
if you would like the program to do a repetitive calculation until you tell it
to stop, you would use a WHILE LOOP, where the “stop” condition is you
flipping a boolean toggle switch. In
LabView, the procedures which you would like to repeat are “boxed” in by the
appropriate loop structure, which you choose from the functions panel.
(1)
A FOR LOOP is an iterative loop structure which executes a set of commands a set
number of times. Note that this set
number of times may be a control which you set on the front panel, and which you
can change with each run of the program.
(2)
A WHILE LOOP is a loop structure that repeats a section of code until a
condition is met, as governed by a boolean which is wired into the loop.
A while loop necessarily performs the commands within the loop at least
once, since it executes the contents, then checks the condition of the boolean.
(D) CASE STATEMENTS:
Often times, the execution of a command or set of commands depends upon a
previous result. In other words, you may want to build in “branch points”
into your code. For example, say
you perform a calculation and the result of the calculation can be either a
positive or a negative number. If
you want to perform a subsequent calculation with that number (say take the log
of it), you would only want to do this if the number was positive, otherwise the
program would crash. If the number
was negative, you might want to avoid taking the log of it, and warn the
operator that you are generating negative numbers.
In this scenario, you would use a case statement where you first check to
see if the number is negative and, if it is, you generate a warning message and,
if it isn’t, you continue the execution of the program.
(E) SEQUENCE STATEMENTS:
Since LabView programs are not “top-down” like traditional languages,
you often need a way to control the order of execution of your program.
For this purpose, you should include your code in a sequence statement,
where each “frame” of the sequence represents consecutive steps in the
execution of the program.