02-02-2011 01:53 PM
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
Solved! Go to Solution.
02-02-2011 02:15 PM
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.
02-02-2011 04:05 PM
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
02-02-2011 04:50 PM
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.
02-02-2011 05:02 PM - edited 02-02-2011 05:03 PM
After reading your post again the last part is not very clear. I think this might be what you want.
02-02-2011 11:16 PM
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.
02-03-2011 06:24 AM
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 🙂
02-03-2011 09:35 AM
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.
02-03-2011 10:14 AM - edited 02-03-2011 10:20 AM
Kudos from me too. Labnoob, you should mark lucithers post as accepted solution for future readers.
02-03-2011 10:38 AM
bollocks! i mistakenly accepted my response as the solution!! woops! im sure others will get the idea!