LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to do an event only once inside a while loop?

I have a boolean control (Start Button) inside a while loop that needs to initiate TWO processes when it is activated. For the FIRST of these processes, it is imperative that once the button is pressed, the boolean control must remain in its switched state (switch when pressed) for the remaining duration of the VI.

However, for the SECOND process, the same boolean control must trigger a process (some code inside a Case Structure) only ONCE, and never again. Due to various constraints, this Case Structure code is also in the same While Loop that contains the boolean control.

I am trying to figure out a way to do this, but have not been successful so far.

If the explanation of the problem I have provided above is no
t clear, here's another way of looking at the problem:

I need an effect similar to the "latch when pressed" mechanical action of controls, but achieved programatically. The Case Structure containing my code receives a default "False" status until the button is pressed. After the button is pressed (and for the remainder of the program), the status is changed to "True". This Case Structure is inside a While Loop, which runs throughout the length of the program. How do I make the code in the Case Structure only happen once, and not with each iteration of the While Loop?

Any suggestions are appreciated.
0 Kudos
Message 1 of 5
(5,348 Views)
Use a shift register and wire ....

... maybe it's easier to just make an example. See attached (LV7).

Is that what you need?
0 Kudos
Message 2 of 5
(5,348 Views)
Hi,


You can use the "value changed" event, and in the event disable the button
(so it cannot be pressed, and can thus never activate the event again).

You could also register a dynamic event "value changed" for the button, and
in the event you could unregister the dynamic event, so it will not be
called again.

Regards,

Wiebe.

"JNR" wrote in message
news:5065000000080000001FD40000-1079395200000@exchange.ni.com...
> I have a boolean control (Start Button) inside a while loop that needs
> to initiate TWO processes when it is activated. For the FIRST of these
> processes, it is imperative that once the button is pressed, the
> boolean control must remain in its switched state (switch when
> pressed) for the remaining duration of the VI.
>
> However,
for the SECOND process, the same boolean control must trigger
> a process (some code inside a Case Structure) only ONCE, and never
> again. Due to various constraints, this Case Structure code is also in
> the same While Loop that contains the boolean control.
>
> I am trying to figure out a way to do this, but have not been
> successful so far.
>
> If the explanation of the problem I have provided above is not clear,
> here's another way of looking at the problem:
>
> I need an effect similar to the "latch when pressed" mechanical action
> of controls, but achieved programatically. The Case Structure
> containing my code receives a default "False" status until the button
> is pressed. After the button is pressed (and for the remainder of the
> program), the status is changed to "True". This Case Structure is
> inside a While Loop, which runs throughout the length of the program.
> How do I make the code in the Case Structure only happen once, and not
> with each iteration of the Whil
e Loop?
>
> Any suggestions are appreciated.
0 Kudos
Message 3 of 5
(5,348 Views)
> I am trying to figure out a way to do this, but have not been
> successful so far.
>

To do this without events, you need another piece of state information.
Add a shift register to your while loop and initialize it to False.
This will indicate whether the initialization has run in this session.
Combine this information with the button information to control the
execution of the case. Obviously, the right shift register needs to be
sent True when the initialization happens, and Current value otherwise.
Note that this is a very common approach, and when you find that you
have several shift registers for various data, you might decide to
combine them into a cluster and use fewer shift registers. The choice
is yours.

Greg McKaskle
0 Kudos
Message 4 of 5
(5,348 Views)
JNR wrote in message news:<5065000000080000001FD40000-1079395200000@exchange.ni.com>...
> I have a boolean control (Start Button) inside a while loop that needs
> to initiate TWO processes when it is activated. For the FIRST of these
> processes, it is imperative that once the button is pressed, the
> boolean control must remain in its switched state (switch when
> pressed) for the remaining duration of the VI.
>
> However, for the SECOND process, the same boolean control must trigger
> a process (some code inside a Case Structure) only ONCE, and never
> again. Due to various constraints, this Case Structure code is also in
> the same While Loop that contains the boolean control.
>
> I am trying to figure out a way to do this, but have not been
>
successful so far.
>
> If the explanation of the problem I have provided above is not clear,
> here's another way of looking at the problem:
>
> I need an effect similar to the "latch when pressed" mechanical action
> of controls, but achieved programatically. The Case Structure
> containing my code receives a default "False" status until the button
> is pressed. After the button is pressed (and for the remainder of the
> program), the status is changed to "True". This Case Structure is
> inside a While Loop, which runs throughout the length of the program.
> How do I make the code in the Case Structure only happen once, and not
> with each iteration of the While Loop?
>
> Any suggestions are appreciated.

Add a shift register to the while loop. Connect the start button to
the right-hand shift-register terminal. Connect a False constant to
the left-hand shift-register terminal outside the while loop. Feed
the selector of the case statement with the following logic:
Start_Button
AND (NOT left_hand_shift_register_terminal). This logic
while cause the TRUE case to execute only on a transition of the Start
button from F to T.

Paul Cardinale
0 Kudos
Message 5 of 5
(5,348 Views)