04-12-2017 01:11 PM
Hello everyone.
I have two case structures which are carrying two simple tasks. The first one is adding 0+1 and the second one is adding 1+1. I have put the two tasks in separate case structures and I have designed a simple code to lunch the case structures when I push the button Z on keyboard.
The problem is when I push the Z button, two tasks will be lunched together at the same time. That really depended on how strong I push the Z button. (I have attached my code to this post). If I had a Boolean OK button, I could go to properties > Operation > and choose the option Latch when pressed. But it seems that in my code I can't change that. Does anybody have any suggestions?
p.s. I know that if I choose two different buttons from keyboard to lunch the two case structures, the problem would be solved. But the thing is I HAVE to use only one button.
Sina
Solved! Go to Solution.
04-12-2017 02:36 PM - edited 04-12-2017 02:39 PM
I don't understand what you are trying to do here...
I have two case structures which are carrying two simple tasks. The first one is adding 0+1 and the second one is adding 1+1. I have put the two tasks in separate case structures and I have designed a simple code to lunch the case structures when I push the button Z on keyboard.
You are using a flat sequence, (never a good idea) that means your code is going to do this:
On start up it is going to be spinning in the (gready) for-loop in the first frame until a "Z" is received.
Then it will move on to the second frame and do the same thing.
If you are still holding the "Z" key it will complete the second frame instantly.
Do you want the two tasks to be completed with one "Z" button press?
If so are the two tasks supposed to complete simultaneously or sequentially?
04-12-2017 02:42 PM
Your two tasks are not launched at the same time, first 0+1 is run, after that 1+1 is run.
Incidentally, your two innermost while loops serve no function; each of them always iterates exactly once.
It's not at all clear what you are trying to accomplish.
04-12-2017 02:48 PM
In addition to what RTSLVU asked, you might want to look into using an event structure to capture the Key Down event. It will fire only one time when a key is pressed so you don't have to worry about the user hitting the key for the perfect amount of time and having that change depending on what computer they are using. The Key Repeat event can also be used if you want to capture someone holding down the key as well.
04-13-2017 09:46 AM
Hello and thank you for your comment. Your scenario is right. I want two tasks to be completed with separate "Z" button press.
I am not holding the Z button. I try to push it once but apparently I am holding it for some fractions of seconds.
So the question that I have asked here is a much simplified version of what I am trying to code. I am doing spectroscopy on tumors of mice. I want to get the first spectra by pushing the "Z" button(first task- i.e. 0+1). Move the probe to a new location (second task - i.e. 1+1) and get the new spectrum. But I don't want these tasks to be started unless I order that with "Z" button.
04-13-2017 09:58 AM - edited 04-13-2017 10:02 AM
Like Matt J said, you should use an event structure to capture the 'Key Down' event, and then you can compare the output to 'z' (or another key, if this is just an example).
The Key Down event can be found under 'This VI' > 'Key' in the Event Structure. You can map a key code to it's ASCII string representation using a 'To U8' and 'Type Cast' node pair. A simple example snippet is shown below (this only runs once and then exits, but you can wire indicators to the other outputs or place the structure in a loop if you want to try multiple key presses.
Edit: To be clear, if you want to compare to 'z', compare the Char value with the numeric representation (i.e. 44). Don't cast it to a string and then check it. You can use a case structure with multiple valid results for the same case if you want to allow both 'z' and 'Z' for example.
04-13-2017 10:02 AM
You are right that they don't lunch at the same time. However two tasks needs to be lunched with separate orders of "Z" button.
04-13-2017 10:05 AM
You can place two event structures with a dataflow dependency, or you can use a loop and check the iteration indicator to see which iteration you're running. Be careful with the iteration indicator if you have other (non-exit) cases - if you press 'z', the first case triggers, then you press 'a' and the event structure detects a 'Key Down' event, then ignores it because the key wasn't 'z', the iteration value if you then press 'z' won't be 1, it will be 2.
If you only have two cases, and you know you don't want to expand it in future, then you could consider a boolean on a shift register or similar (or, a state machine!)
04-13-2017 10:14 AM
04-13-2017 11:20 AM - edited 04-13-2017 11:29 AM
@cbutcher wrote:
Here's a simple example using an enum to track the state of the VI, and 1-button dialogues to indicate what's happening.
Unfortunately I can't open your file since my LV is 2014 version 😞