04-12-2011 05:52 AM
I have a switch which controls the input to a VI (it is slightly more complicated than that, but that's the basic idea).
The VI is a "set" function. So it really should only execute when the value changes.
Is there some way to set the switch so that there is only data available on the output when the switch is switched, thus executing the VI once?
If not, what is the best way to do this?
Thank you.
B.
04-12-2011 06:29 AM
There are some ways to do this but it really depends on what you are trying to do overall. Is it possible for you to post an example of your implementation so we suggest the right way?
04-12-2011 07:48 AM
The standard way to do this sort of thing is to use an event structure. The VI can be placed in the event which runs when the switch changes value. Alternately, if you use a producer/consumer architecture (recommended), the event structure triggers your command processor to run the VI.
Check out event structures and then look at state machines and producer/consumer design patterns. A search of these forums will probably give you more information than you want , but check the LabVIEW help first.
04-27-2011 04:19 AM
Thank you.
An event structure sounds like just what I need. I am not going to get into the design patterns you mentioned here .... this is a demo program and there is a switch on the front panel that demos a feature in our library, turning it on and off. I just want to call a library function to turn the feature on or off at user request....the rest of the demo works (right now, I call the VI to turn on/off the feature every time through my main loop -- this is ugly and causes us timing problems... but everything else is finished)
I tried to add an event structure but don't seem to have it in my edition of LabVIEW....
What I just added now is a shift register to save the value of the switch between loop iterations, and I compare it to the current value, then feed it into a true/false case structure, only calling my "set" VI when the two values are different...
Is there a better way, or is that all I can do unless my company buys a fancier LabVIEW version?
Thanks.
B.
04-27-2011 08:11 AM
You may be able to simplify your code by changing the mechanical action on your switch to "latch when pressed" or "latch when released" so the switch automatically resets itself and you don't have to remember the last state. Other than that, there is not much more you can do that you are not already doing. Without a state machine / command processor, you pretty much have to do everything every loop iteration.
04-27-2011 08:30 AM
What Version of LabVIEW are you running?
04-27-2011 09:08 AM
@BPerlman wrote:
Thank you.
An event structure sounds like just what I need. I am not going to get into the design patterns you mentioned here .... this is a demo program and there is a switch on the front panel that demos a feature in our library, turning it on and off. I just want to call a library function to turn the feature on or off at user request....the rest of the demo works (right now, I call the VI to turn on/off the feature every time through my main loop -- this is ugly and causes us timing problems... but everything else is finished)
I tried to add an event structure but don't seem to have it in my edition of LabVIEW....
What I just added now is a shift register to save the value of the switch between loop iterations, and I compare it to the current value, then feed it into a true/false case structure, only calling my "set" VI when the two values are different...
Is there a better way, or is that all I can do unless my company buys a fancier LabVIEW version?
Thanks.
B.
Shameless plug for an idea in the idea exchange. Since this would have helped you resolve your issue you might want to consider giving a kudo to this idea.
The way you are doing it now with the shift register is pretty much the only way you can do this without using an event structure.
04-28-2011 12:53 AM
You may be able to simplify your code by changing the mechanical action on your switch to "latch when pressed" or "latch when released" so the switch automatically resets itself and you don't have to remember the last state.
I played around with these options and I'm not sure how it helps me here. It would look strange to the user if the switch always popped back to "off", say,
when he turned it on. Also, how would he actually switch the thing off, if the switch were always off? I don't get it.
What Version of LabVIEW are you running?
8.6, base development version.
Shameless plug for an idea in the idea exchange. Since this would have helped you resolve your issue you might want to consider giving a kudo to this idea.
Done. I like the idea, of course!
The way you are doing it now with the shift register is pretty much the only way you can do this without using an event structure.
OK, thanks....
B.