LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

state machine - wait in user input before going to next state

Solved!
Go to solution

Hi there,

 

I have built upon the standard state machine example and would like some advice on how i can prevent labview from entering the next state until a user hits a button.

 

e.g. the current state is 1, it runs through its code, and its about to come to the end hence, enter the next state based on the defined "next state" constant. there shoudl only be two choice on how the machine should proceed, eithe rto state 4 or to state 5.  however, if i have used a "select function" where by the next state is determined by the boolean value of a switch, how do set up my code so that labview doesnt just assumne the current of the switch (probably a "low" is the boolena value to use. 

 

Im probably not being very clear but hope you understand my issue.

 

Thanks in advance

 

0 Kudos
Message 1 of 15
(8,207 Views)

Hi,

Just a quick thought, you could put your boolean in an event structure and set the event to value change, then the value of the control will only be read when the user hits the button.  Be careful though, it is not good practice to have a state machine 'hang' waiting for a user input when there is no other way of exiting the code.  I sometimes use an event case as one state "wait on event", this will have various exit states.  If the SM should run even when no input is detected, wire a timeout value to the event structure.  This is easier to code than the producer-consumer (events) design pattern and will often suffice.

Hope this helps,

Michael.

0 Kudos
Message 2 of 15
(8,195 Views)

Thanks for your reply.

 

So should i be using the event structure rather than the standard stame machine exmaple, or are you saying i should implement this event structure into my existing SM.

 

Thanks

0 Kudos
Message 3 of 15
(8,177 Views)

Just add a state to wait for the button press. If the button is not pressed go to the wait for button press. If it is pressed go to the next state.

 

See the attached.

 

=====================
LabVIEW 2012


Message 4 of 15
(8,163 Views)

After reading your post again the last part is not very clear. I think this might be what you want.

 

 

=====================
LabVIEW 2012


0 Kudos
Message 5 of 15
(8,156 Views)
Solution
Accepted by labnoob

Hi Rivers,

 

Simply add a case to handle the 2 buttons after the action case has finished. You can use a 'Wait on user interface' function if you don't want to poll the buttons. You may run into problems if you use an event structure to implement this. If you use more then 1 event structure in your code you can get a problem with the user interface indefinitely hanging. This happens if a button is pressed that is handled by an event structure in another part of the loop. The user interface locks until it gets actioned but it cannot be actioned as it is in another case. You should only use 1 event structure to handle all your inputs but if you do this then you will have the problem of only enabling certain buttons at certain points. For example you will only want buttons for action 2 & 3 enabled after action 1, and buttons that are used to go to other actions disabled at this point. This will become messy. Better just to add an extra case after each action case that handles the user interaction to the next case. In the example i have attached i have placed the action buttons in a cluster that is passed to all cases allowing you to get the buttons you need when you need them. You of course could just put the buttons in the cases where needed also but i prefer the first method.

 

Rgs,

 

Lucither.

------------------------------------------------------------------------------------------------------
"Everything should be made as simple as possible but no simpler"
Message 6 of 15
(8,128 Views)

Thanks to all for your contributions.

 

Lucither’s example was exactly what was required. I made slight modification to suit, but all in all a perefect solution, so thanks Lucither

 

Kudos where kudos is due 🙂

0 Kudos
Message 7 of 15
(8,099 Views)

Kudos from me too, I have never used the wait for activity VI before, I could never see its use over an event structure, but here is a good example.

Michael.

0 Kudos
Message 8 of 15
(8,084 Views)

Kudos from me too. Labnoob, you should mark lucithers post as accepted solution for future readers.

=====================
LabVIEW 2012


0 Kudos
Message 9 of 15
(8,074 Views)

bollocks! i mistakenly accepted my response as the solution!! woops! im sure others will get the idea!

0 Kudos
Message 10 of 15
(8,063 Views)