04-18-2014 10:29 AM - edited 04-18-2014 10:36 AM
I'm trying to find the 'proper' method to do this basic concept of incrementing a value (in this example) once per key press:
Is there a more efficient (using less cpu and/or less code) way to do this?
I am also curious why you can't add shift registers to the case structure.
Solved! Go to Solution.
04-18-2014 11:08 AM
It doesn't get much more efficient than that. The is an increment '+1' primitive in the numeric palette which makes the code look a bit smaller but I suspect the compiler is smart enough to spit out the same thing either way.
It is considered better coding style to use an event structure to react to controls rather than polling. That will probably save a little CPU drain, although I see you have a 50ms wait in your polling loop to curtail it being too greedy.
Shift registers only make sense on structures that implement a loop-a case does not. You can create shift registers on your outer while loop and wire to that.
04-18-2014 12:00 PM
04-18-2014 12:03 PM
If you want it to increment really fast when you hold down the key, you would need to add a Key Repeat event.
04-18-2014 12:13 PM - edited 04-18-2014 12:15 PM
(Note I posted this before seeing the 2 replys that snuck in before this post)
I think I get it. Is this what you described (Pic Below)?
I first thought the loop would be running continuously, but after putting on an indicator I see it does nothing until the case inside is finished.
04-18-2014 12:19 PM
You need to put the Stop Button in an event (or set the event structure to timeout). By setting the loop timeout to -1 (never timeout), clicking stop will only stop the VI after you click the Run Once button.
04-18-2014 12:20 PM
Hello martinv,
Aputman's suggestions are actually VI Snippets and you can use them directly in your code as long as you have a compatible version of LabVIEW.
It looks like you've recreated most of what he's done, but a few observations:
-You've configured an infinite timeout, and it's not clear if you still have a timeout case. As is, the user will not be able to quit your application until they press the "Run Once" button. Add a timeout case or a "close" event to allow the user to shut the application without having to re-run the sequence.
-It's not clear if you are looking to track a GUI button press or a keyboard key press. The event structure can work with either.
-Be aware of how many times the value of your Boolean control changes- you may end up incrementing twice if the button returns to the original state after being interacted with.
Regards,
04-18-2014 12:21 PM
@martinv wrote:
(Note I posted this before seeing the 2 replys that snuck in before this post)
I think I get it. Is this what you described (Pic Below)?
I first thought the loop would be running continuously, but after putting on an indicator I see it does nothing until the case inside is finished.
Glad to see you figured it out. Slight edits I recommend:
1) Make a case in your event structure for the Stop:Value Change and put the stop terminal inside of that case. You will then wire the value of the stop button (or a true constant) from that event case to the loop conditional terminal. This will allow you to stop your loop immediately instead of being forced to wait for another event (or possibly 2) before actually stopping.
2) I would use a shift register for the value. This way other cases can affect/read the value without the use of a local variable. Be sure to wire the value through all of the other cases.
04-18-2014 12:22 PM - edited 04-18-2014 12:48 PM
Correct. Thanks for the help!
Shift regsiter version seems to work:
Stop Event Case:
04-18-2014 05:52 PM