LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Running for loop on a button press

Solved!
Go to solution

HI everyone, I am trying to save some values in an array which is being formed by auto indexing of the for loop .However I would like to run the for loop only on the button press named "next" and  append this  value to the same array . Could anyone help me with implementing this logic. Right now when I press the next button , nothing happens and I only get one value in the array.I am attaching my code in the picture and the VI in the file.

Thanks 🙂Screenshot 2021-09-16 094129.png

0 Kudos
Message 1 of 8
(3,351 Views)

It only returns one value because you set it that way...

 

You made your event to trigger when you click "Start" button, so you'd have to simultaneously click the Start and the Next button to do what you want, otherwise the loops already executes when you click the next button.

Why don't you just put the loop into another event, a next button value change event?

 

I'm not really sure what you want to do here to give better advice, other than creating another event.

0 Kudos
Message 2 of 8
(3,345 Views)
Solution
Accepted by topic author shahshery

Hi shahshery,

 


@shahshery wrote:

I am trying to save some values in an array which is being formed by auto indexing of the for loop .However I would like to run the for loop only on the button press named "next" and  append this  value to the same array . Could anyone help me with implementing this logic. Right now when I press the next button , nothing happens and I only get one value in the array.


Your VI design is highly flawed! Do you learned the basics of event structures?

  • Inside an event case the code should finish rather quickly (within milliseconds). Placing a loop with a wait of 500ms surely contradicts that recommendation…
  • Your event case is set to react on value change of "start button". It will not react on value changes of "next button"…
  • Your event case is set to lock UI until the event case has finished. Your UI simply cannot react on "next button"…
  • What's the point of that 100ms wait in parallel to the event structure? The event structure will wait for any of those two events you have configured…
  • Why did you maximize both frontpanel and blockdiagram window to fullscreen? That's just annoying!

I suggest to implement it this way:

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 3 of 8
(3,330 Views)

Thanks for your response:) With the start button and the outer event loop I'm just trying to run the program when the start button is pressed and stop it when the stop button is pressed inside the VI rather than using the abort execution button.With the next button I just want to run the for loop for 1 iteration then stop until i press the next button again . This should happen each time when the next button is pressed until the loop has run N times which is predefined in the control named "number of devices".After all the loop has run N times , all the values that were generated in each iteration should be saved in an array.If you could help me with implementing this logic I'd be really grateful.

0 Kudos
Message 4 of 8
(3,324 Views)

You don't need start button to do that, the stop button was correct tho.

 

GerdW's code does exactly that, with the exception of checking if Next was pressed N times already.

His code executes the event each time Next button is pressed, so you get # of data points for each "device".

0 Kudos
Message 5 of 8
(3,316 Views)

Hi shahshery,

 


@shahshery wrote:

With the next button I just want to run the for loop for 1 iteration then stop until i press the next button again .


My code does exactly this: running the code inside your FOR loop exactly once and then wait until you press "next" the next time…

 


@shahshery wrote:

This should happen each time when the next button is pressed until the loop has run N times which is predefined in the control named "number of devices".


Then you should disable the "next" button once the array in the shift register contains the number of required elements: check for ">= number of devices"…

 


@shahshery wrote:

 all the values that were generated in each iteration should be saved in an array.


The values are "saved in an array" which is hold in this shift register. So this requirement also is fulfilled.

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 8
(3,311 Views)

Dear GerdW,

Thanks for your help.I am not actually a programmer but just a student trying to solve a basic problem that explains my mistakes 😄 I'll try your program and get back to you if I need further help 🙂

0 Kudos
Message 7 of 8
(3,309 Views)

Thanks a lot GerdW , for now your code works for me 🙂 

0 Kudos
Message 8 of 8
(3,297 Views)