LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Increment once per key press

Solved!
Go to solution

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.

 

 

Increment Once Per Keypress.png

0 Kudos
Message 1 of 11
(2,101 Views)
Solution
Accepted by topic author martinv

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.

Message 2 of 11
(2,084 Views)

increment.png


aputman
LabVIEW 2017
LabVIEW Programming
0 Kudos
Message 3 of 11
(2,060 Views)

If you want it to increment really fast when you hold down the key, you would need to add a Key Repeat event.

increment1.png


aputman
LabVIEW 2017
LabVIEW Programming
0 Kudos
Message 4 of 11
(2,056 Views)

(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. 

 

Increment Once Per Keypress_Event.png

0 Kudos
Message 5 of 11
(2,048 Views)

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. 


aputman
LabVIEW 2017
LabVIEW Programming
Message 6 of 11
(2,039 Views)

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,

Tom L.
Message 7 of 11
(2,036 Views)

@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. 

 

Increment Once Per Keypress_Event.png


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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 8 of 11
(2,034 Views)

Correct.  Thanks for the help!

 

Shift regsiter version seems to work: 

 

Increment Once Per Keypress_Event_Shift Reg1.png

 

Stop Event Case:

Increment Once Per Keypress_Event_Shift Reg_stop case.png

0 Kudos
Message 9 of 11
(2,027 Views)
  • You should initialize the shift register with the start number (e.g. zero)..
  • Three is a +1 primitive (increment), use it.
  • Shift register and indicators should be I32 (blue), not orange. You are dealing with integers here.
  • hardwiring a -1 to the timeout seems useless, just delete the timeout case instead.
  • You don't need to wire a FALSE in the other cases going to the conditional terminal. The tunnel is set to "use default if unwired", so you can leave the tunnel in all other event cases unwired.
  • Wire the indicator terminal before the event structure so it correctly resets when the program starts even if no events got fired yet.
  • ..

LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
Message 10 of 11
(1,998 Views)