LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Initialize a numeric variable and change it based on push button commands

Solved!
Go to solution

Hello,

I'm trying to create a variable and initialize it to 0. Then, the variable is fetched to an event structure that has three cases: button 1, button 2, and button 3. If button 1 is pushed the variable changes to 1, and if button 2 is pushed, its value becomes 2, and so on. All of this will be in a while-loop and the program stops when the stop button is pushed. Only one button can be pushed at a time (radio button).

 

Attempt:

I create a 1D array with element's value 0 using the Initialize Array function. Then, I add in the Index Array function to read the value. This is where I'm stuck. Following NI examples, the next step would be adding the Replace Array Subset to replace the element but to a new array. Is it possible to link the updated array back to the original array? ***I feel like this is the worst way to approach the problem but I don't know LabView's functionality well enough to approach it differently.

 Please help!

 

Thanks and Happy New Year 😄

 

 

 

0 Kudos
Message 1 of 17
(5,134 Views)

What is a "variable"? Are you talking about an indicator or wire value to be used by a function?

You don't need an array, all you need is a scalar. Show us what you tried so far.

Are you using a radio button control?

0 Kudos
Message 2 of 17
(5,111 Views)

It definitely sounds like you are overcomplicating this.  Just store the value in a shift register and update it when the buttons are pressed.


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
0 Kudos
Message 3 of 17
(5,095 Views)

Hi altenbach,

 

Basically, I'm looking to initialize a number x = 0. Then, x is wired to an event structure. When an event occurs, the value of x: 1) will be used to do stuff, and 2) will be changed. After the event execution is complete, the new value of x will be fetched to the beginning of the program so the event structure can used this new value for the event cases.

 

I've attached a snip of what I'm trying to achieve in broken C++ language. My only attempt so far is the array approach but I haven't been able to feed the new value back to the event structure. I'm going to try to use local variable next; though everyone seems to frown upon it.

 

I hope my attempt in clarifying doesn't confuse you more.

 

 

Thanks, Knight 😉

 

0 Kudos
Message 4 of 17
(5,080 Views)

Hi crossrulz,

 

I've looked at tutorials on shift register and it seems that shift register only work in loops? I suppose I could add a loop around the event structure but is there a way around this?

 

Thanks Knight!

0 Kudos
Message 5 of 17
(5,073 Views)

SegFault1119 wrote:

I've looked at tutorials on shift register and it seems that shift register only work in loops? I suppose I could add a loop around the event structure


Why wouldn't you have a loop around the event structure?  It is there in the C++ code.


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
0 Kudos
Message 6 of 17
(5,062 Views)

You are right! I was confused with how event structures behave inside a while loop. I will try this out and I might come back with more questions.

 

Thanks!

0 Kudos
Message 7 of 17
(5,057 Views)
Solution
Accepted by topic author SegFault1119

@SegFault1119 wrote:

 

I've attached a snip of what I'm trying to achieve in broken C++ language. My only attempt so far is the array approach but I haven't been able to feed the new value back to the event structure. I'm going to try to use local variable next; though everyone seems to frown upon it.

 


I would simply use a radio-button control. This one flashes a certain LED based on the radio button. (see attached).

 

(To do "something" for each possible state, wire it to a case structure and create a case for each value. The cases will be named automatically after the button labels. If the case should execute regularly, put in in the timeout case and the radio value in a shift register. Not shown).

 

I have no idea why you would want to use a local variable next. That makes no sense.

Message 8 of 17
(5,056 Views)

Thank you for your VI example. I've played around with it and it's exactly what I'm trying to achieve. Though, I still need to spend more time to understand it.

 

Also, could you take a quick look at my attachment? I'm trying to work with shift register. The shift register function in my program works but the stop button for the loop doesn't work. The stop button registers when it's being pressed but the loop doesn't stop executing. 

0 Kudos
Message 9 of 17
(5,041 Views)

It was the time out case. I added 200 as you did in your example VI and it's working now.

 

Thank you very much!

0 Kudos
Message 10 of 17
(5,040 Views)