LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Execute a piece of code when user click on a button

hi everyone,

 

 

I have Vi composed of a while loop with different task to do inside.

 

I tried to put one of the task (a piece of code) Into a event structure "Button1 value change" to execute this part only when the user click on a button,

but as soon as I put a event structure inside the while loop, it stops everything, the other tar task does not work anymore !

 

It should be very simple but I'm getting crazy about why it does not work, and why everytime I drag even an empty event structure, It stop everyting. (the while loop iterate one time and stop)

 

I can post a piece of code to explain my problem more in details if needed.

 

Thank you very much,

Guillaume.

0 Kudos
Message 1 of 11
(8,148 Views)
The while loop is waiting for your action (value cange) in EVERY cycle. If you can, move that part of the code with the event structure outside of the while loop. Then the code in the event structure will be implemented when the event will occur and the while loop will run continuously.
---

While evaluating my code please have in mind that I am a LV novice. Therefore sometimes my code might violate some coding rules that I have to learn about myself. But how else could I do that... 🙂

Chart zoom with "Mouse Over" effect
0 Kudos
Message 2 of 11
(8,139 Views)

Ok I will try, indeed, it doesn't stop the while loop anymore (thanks).

 

But the piece of code in the event structure will have to access data inside the while loop, data updated every iteration of the while loop.

 

In an other way, the code inside the event structure take in argument the data inside the while loop.

 

For example, the code inside the event structure start on click and wait until a float in the while loop reach 3 for instance, send a signal, wait for this same float to go down to 1 and stop until the user click again. (I hope I am clear)

 

 

Will it be possible ? (I need to use local or global variable to pass data dynamicly from one structure to anoher ?)

 

thanks,

Guillaume.

Message Edited by gdlbb on 02-07-2010 02:04 PM
Message Edited by gdlbb on 02-07-2010 02:07 PM
0 Kudos
Message 3 of 11
(8,130 Views)

gdlbb wrote: 

But the piece of code in the event structure will have to access data inside the while loop, data updated every iteration of the while loop.

 

In an other way, the code inside the event structure take in argument the data inside the while loop.


 

 

 

It would be much easier of you could show us a simplified version of your code.

 

There are many possible solutions to do all this.

 


gdlbb wrote:

For example, the code inside the event structure start on click and wait until a float in the while loop reach 3 for instance, send a signal, wait for this same float to go down to 1 and stop until the user click again. (I hope I am clear)


 

 

  I think you are approaching this way too complicated. All you probably need is a simple state machine.

0 Kudos
Message 4 of 11
(8,121 Views)

gdlbb wrote:

Will it be possible ? (I need to use local or global variable to pass data dynamicly from one structure to anoher ?)

 

thanks,

Guillaume.


You can use a local variable. But put a while loop in the event structure to check whether the local variable has reached the desired value (it is 3 in this case) or not continuously. If you will not put this into a while loop, your event structure will check it only once.

 

It could look something like this (this example is not written for your case, it is a very abstract example):

event.jpg

---

While evaluating my code please have in mind that I am a LV novice. Therefore sometimes my code might violate some coding rules that I have to learn about myself. But how else could I do that... 🙂

Chart zoom with "Mouse Over" effect
0 Kudos
Message 5 of 11
(8,117 Views)

Giedrius.S wrote: 
It could look something like this (this example is not written for your case, it is a very abstract example):

 

 NOOOO!!!!!!

 

Sorry, but this is horrible code that breaks quite a few rules. 😞

 

 

  1. An event structure belongs in a while loop (with rare exceptions, such as in dialogs).
  2. Event structures should not contain while loops that could take a while or might never complete.
  3. Unless the event has "lock FP until complete" disabled (enabled by default), the stop button would not work while the event is still running.
  4. The FOR loop inside the event structure uses 100% of the CPU doing basically nothing (Unless there is a small wait in the FALSE case that we cannot see ;)).
  5. The value property node should be replaced by a local variable. Less overhead.
  6. Why are your controls disconnected? The "Start to decrement" terminal belongs in the lower loop, replacing the local.
  7. The "activate event tracker" terminal belongs inside the event frame if it is set to latch action. Is it?
  8. It seems silly to increment a value just to compare it with a constant. Why not compare with "2" instead?
  9. You could simplify the code by wiring the value directly to the case and make one case "2" and the other the default.
When making such an example, it would be much more helpful to attach an actual VI  (or code snippet) instead of a picture. Many details are not visible in a picture (e.g. mechanical action settings, event settings, etc.)

 

 

Message 6 of 11
(8,102 Views)

altenbach wrote:
 
NOOOO!!!!!!

 

Sorry, but this is horrible code that breaks quite a few rules. 😞

 

 

  1. An event structure belongs in a while loop (with rare exceptions, such as in dialogs).
  2. Event structures should not contain while loops that could take a while or might never complete.
  3. Unless the event has "lock FP until complete" disabled (enabled by default), the stop button would not work while the event is still running.
  4. The FOR loop inside the event structure uses 100% of the CPU doing basically nothing (Unless there is a small wait in the FALSE case that we cannot see ;)).
  5. The value property node should be replaced by a local variable. Less overhead.
  6. Why are your controls disconnected? The "Start to decrement" terminal belongs in the lower loop, replacing the local.
  7. The "activate event tracker" terminal belongs inside the event frame if it is set to latch action. Is it?
  8. It seems silly to increment a value just to compare it with a constant. Why not compare with "2" instead?
  9. You could simplify the code by wiring the value directly to the case and make one case "2" and the other the default.
When making such an example, it would be much more helpful to attach an actual VI  (or code snippet) instead of a picture. Many details are not visible in a picture (e.g. mechanical action settings, event settings, etc.)

 

 


Thank you for your comment,  altenbach. As I have mentioned a few times (probably I should make this my signature), I am far from advanced LV programmer. I have still a lot to learn. So such situations when I brake some coding rules might occur from time to time. It is worth participating in the forum, because sometimes I help and sometimes I learn.

 

I have tested this example and it worked to me, so I have put that on a forum adding a note, that this is a very abstract code and should not be copied directly.

 

Will have your notes in mind next time 🙂

---

While evaluating my code please have in mind that I am a LV novice. Therefore sometimes my code might violate some coding rules that I have to learn about myself. But how else could I do that... 🙂

Chart zoom with "Mouse Over" effect
Message 7 of 11
(8,083 Views)

Giedrius.S wrote: 
...It is worth participating in the forum, because sometimes I help and sometimes I learn.

 


Kudos for your learning attitude 😉
PS: I do not know how to precisely translate this "不耻下问".... but that's the attitude! 

 

Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010, 2014
依恩与LabVIEW
LVVILIB.blogspot.com
0 Kudos
Message 8 of 11
(8,079 Views)

You should also look at using queues to pass data from an event structure loop to a consumer loop.  I prefer using queues over Property Nodes and especially Local Variables.

 

An advantage of using queues is that they can deal with multiple events, even when the first one is still being processed.

Message Edited by Ray.R on 02-08-2010 08:37 AM
Message 9 of 11
(8,050 Views)

Hi guys,

 

Thank you very much for your replies, I have learned quite a lot of stuff (about the rules for instance).

 I still don't know very much how to handle event structure, but I did want I want with shift register and case structure.

 

The goal my Vi was :

 

You push inject button (in the "ignite - inject" tab). So, the injection of fuel in my combustion chamber is armed, ready to go, and then, when the gauge (the camshaft angle) is between 180 and 190 ° it launch injection for real (through the sub-vi number 4)

 

 

I know I did not gave enough details, My vi's might be useless for you guys but I post it here anyway because if somebody as the same problem in the future, maybe he will find this post.

 

Thank you very much,

It's been some month I started to learn labVIEW, and I learn a lot, maybe In 5 years I will able to help :).

 

Guillaume.

 

P.S I will give more details of my application in my next post if somebody ask for it.

Message Edited by gdlbb on 02-08-2010 12:03 PM
0 Kudos
Message 10 of 11
(8,028 Views)