As a veteran C programmer, but a LabVIEW newbie, I initially had some difficulty figuring out how to do something once when a button was pressed. In C I had used the following code a million times (where prevButton is a static variable)
I initially implemented the code exactly as written, using a feedback node to save the previous button value, but it seemed messy to me, and I made a few stupid errors in the process. Since I used it all over the place, I made a simple VI to do the same thing. To make it less error prone, I re-implemented it as a very simple state machine.Code:if (button != prevButton && button == 1) { //do something } prevButton = button;
I've attached my implementation of the state machine so that other new LabVIEW programmers can learn from my experiences.
It takes a boolean input (True/False) and outputs not only True/False, but on a transition it outputs either TrueToFalse or FalseToTrue. You could then take that output into a case statement to implement your own code on the transition that you want.
The VI execution style is reentrant. In LabVIEW, if it wasn't set as reentrant, the feedback node would act as a global variable and all instances would use that value. Since it is reentrant, the feedback node acts as a static local variable, and each instance of the VI has it's own value. This means you can use this same VI multiple times to decode multiple buttons.
I've attached both the LabVIEW code and the HTML documentation. Hope this helps someone.


Reply With Quote

