LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Repeated code

Hello

I am writing an program in LabView 6i where I have 20 buttons that needs to
be tested in every program loop. If button n is pressed the program runs an
subvi with the parameter n. Right now I am doing this with an sequence (20
long) and in frame n I test button n with an case and if true run the subvi
with n.
Please tell how to do this without repeating the same code 20 times.


/ Peter
0 Kudos
Message 1 of 8
(3,369 Views)
Hi,

you can build array of booleans (connect all button terminals to "build array" vi) and check state of each button in for loop. You can also create this array programmatically by using Controls[] property of VI server. You check in loop each control by its name: if this is name of button, read its value property.
0 Kudos
Message 2 of 8
(3,369 Views)
Hi,

Put the buttons in an array. Use the Search 1D Array function to find the
first TRUE value. Now you have an index. (If the buttons are in a bundle,
use Bundle to Array function. This might be a good idea, it can be more
practical.)

Now you have three options:

1) Put Parameter N in an array, and use the index from previous code as an
index. (You might want to add 1 to the index, if no button is pressed,
previous code retuns -1. Plus 1, it will give 0, make the first index the
default value.)

2) Put functionality you want to execute in a case structure. If you reuse
functionality, make the case execute in all cases that apply (e.g. put 1,2,3
in the case if you want to execute them in case 1, 2 or 3. Use 1..3 as a
shortcut.) Be carefull with the d
efault case.

3) Combine 1) and 2). This can be done in unlimited ways.

Regards,

Wiebe.


"none" wrote in message
news:ahj2la$8gu$1@news.net.uni-c.dk...
> Hello
>
> I am writing an program in LabView 6i where I have 20 buttons that needs
to
> be tested in every program loop. If button n is pressed the program runs
an
> subvi with the parameter n. Right now I am doing this with an sequence
(20
> long) and in frame n I test button n with an case and if true run the
subvi
> with n.
> Please tell how to do this without repeating the same code 20 times.
>
>
> / Peter
>
>
Message 3 of 8
(3,369 Views)
An improvement on the array of boolean buttons with search 1D array
and a increment one and case structure architecture would be to use
the index value from the search 1D array to select from an array of
strings containing the names of VI's to be dynamically launched. In
fact the names of the VI's to be dynamically launched and the button
labels associated with those VI's could even be kept in an SQL
database (like Microsoft Access) so that you could change how the
program works merely by changing the database entries.

This way you wouldn't need to revalidate your user interface just
because you added new cases or removed them, or changed the
functionality of individual cases. You would only need to validate
the code that gets launched dynamically if you change or add it.

Douglas De Clue
LabVIEW developer
ddeclue@bellsouth.net


"Wiebe@AIR" wrote in message news:...
> Hi,
>
> Put the buttons in an array. Use the Search 1D Array function to find the
> first TRUE value. Now you have an index. (If the buttons are in a bundle,
> use Bundle to Array function. This might be a good idea, it can be more
> practical.)
>
> Now you have three options:
>
> 1) Put Parameter N in an array, and use the index from previous code as an
> index. (You might want to add 1 to the index, if no button is pressed,
> previous code retuns -1. Plus 1, it will give 0, make the first index the
> default value.)
>
> 2) Put functionality you want to execute in a case structure. If you reuse
> functionality, make the case execute in all cases that apply (e.g. put 1,2,3
> in the case if you want to execute them in case 1, 2 or 3. Use 1..3 as a
> shortcut.) Be carefull with the default case.
>
> 3) Combine 1) and 2). This can be done in unlimited ways.
>
> Regards,
>
> Wiebe.
>
>
> "none" wrote in message
> news:ahj2la$8gu$1@news.net.uni-c.dk...
> > Hello
> >
> > I am writing an program in LabView 6i where I have 20 buttons that needs
> to
> > be tested in every program loop. If button n is pressed the program runs
> an
> > subvi with the parameter n. Right now I am doing this with an sequence
> (20
> > long) and in frame n I test button n with an case and if true run the
> subvi
> > with n.
> > Please tell how to do this without repeating the same code 20 times.
> >
> >
> > / Peter
> >
> >
0 Kudos
Message 5 of 8
(3,369 Views)
An improvement on the array of boolean buttons with search 1D array
and a increment one and case structure architecture would be to use
the index value from the search 1D array to select from an array of
strings containing the names of VI's to be dynamically launched. In
fact the names of the VI's to be dynamically launched and the button
labels associated with those VI's could even be kept in an SQL
database (like Microsoft Access) so that you could change how the
program works merely by changing the database entries.

This way you wouldn't need to revalidate your user interface just
because you added new cases or removed them, or changed the
functionality of individual cases. You would only need to validate
the code that gets launched dynamically if you change or add it.

Douglas De Clue
LabVIEW developer
ddeclue@bellsouth.net


"Wiebe@AIR" wrote in message news:...
> Hi,
>
> Put the buttons in an array. Use the Search 1D Array function to find the
> first TRUE value. Now you have an index. (If the buttons are in a bundle,
> use Bundle to Array function. This might be a good idea, it can be more
> practical.)
>
> Now you have three options:
>
> 1) Put Parameter N in an array, and use the index from previous code as an
> index. (You might want to add 1 to the index, if no button is pressed,
> previous code retuns -1. Plus 1, it will give 0, make the first index the
> default value.)
>
> 2) Put functionality you want to execute in a case structure. If you reuse
> functionality, make the case execute in all cases that apply (e.g. put 1,2,3
> in the case if you want to execute them in case 1, 2 or 3. Use 1..3 as a
> shortcut.) Be carefull with the default case.
>
> 3) Combine 1) and 2). This can be done in unlimited ways.
>
> Regards,
>
> Wiebe.
>
>
> "none" wrote in message
> news:ahj2la$8gu$1@news.net.uni-c.dk...
> > Hello
> >
> > I am writing an program in LabView 6i where I have 20 buttons that needs
> to
> > be tested in every program loop. If button n is pressed the program runs
> an
> > subvi with the parameter n. Right now I am doing this with an sequence
> (20
> > long) and in frame n I test button n with an case and if true run the
> subvi
> > with n.
> > Please tell how to do this without repeating the same code 20 times.
> >
> >
> > / Peter
> >
> >
0 Kudos
Message 6 of 8
(3,367 Views)
Or...... if you would like to try a Labview 6.1 feature to prevent polling
the buttons:

Use an event structure. On Value Changed for the button, run your subvi.
Label your buttons such that you can pass in the label to the subVI

-joey

"none" wrote in message
news:ahj2la$8gu$1@news.net.uni-c.dk...
> Hello
>
> I am writing an program in LabView 6i where I have 20 buttons that needs
to
> be tested in every program loop. If button n is pressed the program runs
an
> subvi with the parameter n. Right now I am doing this with an sequence
(20
> long) and in frame n I test button n with an case and if true run the
subvi
> with n.
> Please tell how to do this without repeating the same code 20 times.
>
>
> / Peter
>
>
0 Kudos
Message 4 of 8
(3,364 Views)
Go to the programming design discussion under Topics. If you go to the sample code thread, you will see a posting for a state machine. In that state machine is an example of exactly how to do this. I always prefer clusters or arrays when handling buttons (clusters allow more design difference between the buttons, so I prefer those.) In this sample, I used a cluster of booleans, and as described above, changed the cluster to an array, and searched for the first true button. Of course, the disadvantage to this is that it only checks for the first button that is pressed. If you want to check for more, you will have to use a queued state machine.
0 Kudos
Message 7 of 8
(3,363 Views)
Douglas,


This is not necessarily an improvement. Calling the VI's dynamically can be
difficult if the VI's take a lot of inputs, or when all the VI's need
different inputs.



But, I agree, the possibilities are numerous. The array of strings (or
enumerated type def, if you like) can be used to get the next state in a
state machine.



I used this method (with strings) in an application using a tree. The button
names change when a different tree item is chosen. The "read buttons" state
jumps to the next state (the button name that is pressed), using an array
that has the names of the current button names. (The names of the buttons
can be changed from the ini file. The ini file also cahnges the tip and
description of the buttons. The data in the tree is filled from a database,
actually four databases.)



All of these methods can be easily enhanced to handle menu items and events.



I'm not a big fan of dynamically calling VI's because it's (slightly) more
difficult to read. Not for me, but for my colleagues that may have to
service the code when I am not available.



Regards,



Wiebe.



"Douglas De Clue" wrote in message
news:6f0395b7.0207231319.6a29014e@posting.google.com...
> An improvement on the array of boolean buttons with search 1D array
> and a increment one and case structure architecture would be to use
> the index value from the search 1D array to select from an array of
> strings containing the names of VI's to be dynamically launched. In
> fact the names of the VI's to be dynamically launched and the button
> labels associated with those VI's could even be kept in an SQL
> database (like Microsoft Access) so that you could change how the
> program works merely by changing the database entries.
>
> This way you wouldn't need to revalidate your user interface just
> because you added new cases or removed them, or changed the
> functionality of individual cases. You would only need to validate
> the code that gets launched dynamically if you change or add it.
>
> Douglas De Clue
> LabVIEW developer
> ddeclue@bellsouth.net
>
>
> "Wiebe@AIR" wrote in message
news:...
> > Hi,
> >
> > Put the buttons in an array. Use the Search 1D Array function to find
the
> > first TRUE value. Now you have an index. (If the buttons are in a
bundle,
> > use Bundle to Array function. This might be a good idea, it can be more
> > practical.)
> >
> > Now you have three options:
> >
> > 1) Put Parameter N in an array, and use the index from previous code as
an
> > index. (You might want to add 1 to the index, if no button is pressed,
> > previous code retuns -1. Plus 1, it will give 0, make the first index
the
> > default value.)
> >
> > 2) Put functionality you want to execute in a case structure. If you
reuse
> > functionality, make the case execute in all cases that apply (e.g. put
1,2,3
> > in the case if you want to execute them in case 1, 2 or 3. Use 1..3 as a
> > shortcut.) Be carefull with the default case.
> >
> > 3) Combine 1) and 2). This can be done in unlimited ways.
> >
> > Regards,
> >
> > Wiebe.
> >
> >
> > "none" wrote in message
> > news:ahj2la$8gu$1@news.net.uni-c.dk...
> > > Hello
> > >
> > > I am writing an program in LabView 6i where I have 20 buttons that
needs
> > to
> > > be tested in every program loop. If button n is pressed the program
runs
> > an
> > > subvi with the parameter n. Right now I am doing this with an
sequence
> > (20
> > > long) and in frame n I test button n with an case and if true run the
> > subvi
> > > with n.
> > > Please tell how to do this without repeating the same code 20 times.
> > >
> > >
> > > / Peter
> > >
> > >
0 Kudos
Message 8 of 8
(3,365 Views)