LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Event Structure Help

So I wrote this program not knowing how event structures worked ahead of time, and I did pretty well except for 1 problem.  I set the event to continue on a value change of the "Continue..." button on the bottom right of the screen (block diagram is attached) but when I wrote it, I did so thinking that it would run all the code on the inside first, and then pause for me to pick what I wanted next (that big case structure on the right side) and then click the continue.  Instead the order of operations is backwards, it pauses and waits for me to click "Continue..."  in the very beginning, then runs the code inside. 

 

This would be fine, if it didn't cause the test I was running to be a step behind.

 

Is there a way to change the settings so that it runs the code, and then waits for the "Continue..." button, or do I need to re-write this from psuedo-scratch so that it does everything in the right order?

 

Hope that was all clear, let me know if it's not.

0 Kudos
Message 1 of 9
(3,305 Views)

You could use a "Value (Signaling)" property to write a True to the continue button at the beginning of your VI. That would cause the loop to run once without user interaction.

But actually I'm not sure an event structure is the right architecture for this kind of application. As it looks to me a state machine would be more appropriate. Event structures usually don't do measurement activities or such, they should just respond to user interaction. If the event loop is busy with handling an event the user interface becomes "unresponsive", (since the event loop can't handle another event while being busy in one), which is usually not desired behavior.

 

0 Kudos
Message 2 of 9
(3,292 Views)

The reason I have not used a state machine is because a state machine doesn't pause and wait for me to tell it which state to do next.  I was originally going to make this .vi without an event structure, but I needed some type of .vi to allow me to pause the execution and wait for front panel input, and everyone told me to go with an event structure instead.

0 Kudos
Message 3 of 9
(3,287 Views)

Yes, using an event structure for the continue button is fine. But not for the whole operation. 

 


@LarsUlrich wrote:

The reason I have not used a state machine is because a state machine doesn't pause and wait for me to tell it which state to do next.


Who says that? You as the programmer decide when a state machine advances to the next state. When you need to wait for an event in one state, do that. For that a queued state machine seems like a good option.

 

0 Kudos
Message 4 of 9
(3,281 Views)

That sounds great but how do I do that?

 

I have asked the question "how do I pause a program's application to wait for front panel actuvity" on these forums before, and the only answer I kept getting was "use an event structure."

 

So I did.  And this is what I ended up with.

0 Kudos
Message 5 of 9
(3,276 Views)

As I mentioned, a queued state machine is perfectly suited for this. When the queue is empty, the state machine will pause until there is an element. You can enqueue state(s) from within the state machine or from an event structure (in a separate loop).

 

 

0 Kudos
Message 6 of 9
(3,271 Views)

You can also have an Idle state which does nothing except wait for a short time then check to see if a new command has been received.  If not it returns to the Idle state again.

 

Lynn

0 Kudos
Message 7 of 9
(3,252 Views)

You have misunderstood Events some. An event fires/runs whenever it is triggered, in your case when you press Continue. Since you have your code in this event it'll run when you press the button, just like it's supposed to.

You shouldn't think of it as "wait for button press" but "what should happen when i press". 

 

In your case it can be solved very easily, you simply want it to run once before waiting instead of waiting first? Now, then, how do you make it run? If it runs when you fire a Continue-event, lets fire an Continue event! Drop a Property node of Continue with Value(signalling) and before the main loop and you'll fire it at start.

 

Easy as pie. 

 

/Y 

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 8 of 9
(3,241 Views)

Looks to me like the problem your running into is that the control selecting the case structucture is read as soon as you enter the case structure, i.e. at the same time the rest of your other code is executing.  By the time you reach your 'pause' the conditions for the next iteration are already set, and changes to the case structure control, will be read NEXT time through the loop, making it appear to be one step behind.  Use 'Highlight Execution' to verify.

 

If you don't mind pressing the 'continue' button first, you can get rid of the shift registers, move the case structure to the left and feeding its outputs directly into the various subVIs directly.  The case will be selected by the 'NextUp' control when the 'Continue' button is pressed.

 

-or-

 

Move the code you want to run first outside the event structure.  Leave just the 'Next Up' control, the case structure, and the 'Continue' control inside the event structure.  

 

When you execute, the code outside the eventstructure will execute using the defaults outside the 'while loop',  and pause at the event structure until the 'Continue' button is pressed.  When you press the 'Continue' button, 'NextUp' control is read selecting the case setting the shift registers for the next time through the loop. (you should probably use an error wire to force execution flow)

 

Attached are abbreviated examplesOrder.png

Message 9 of 9
(3,206 Views)