Showing results for 
Search instead for 
Did you mean: 

Nested Events and Timed Loops

Ok, I'm pretty new at this, so forgive any incompetance. I've got a VI with a bunch of kinda complex loops in it. I got some help on these boards to get this far, but I'm afraid I've hit a few more snags. Here's what I'm trying to do: The user hits a "go" button to begin the loop sequence. It runs one loop for a given time (this works) then proceeds to run another loop for a second given time (this works as well). Here is where the snags come in. Inside that second loop I wanted to have an event structure that was based on a user controlled variable. The idea is basically that during the 30 seconds or so that the second loop is running, if they change a variable, then a motor will run for a time proportional to the change in the variable, and different directions based on if it was a positive or negative change. I thought I had this down. I put in an event structure that would go whenever the variable changed, did the logic, and created two while loops that would run the motor for a given time based on the logic. It doesn't work, and I'm not sure why. I think it's something to do with how I calcualted time, but it really could be anything. The third loop in the sequence has a timing that I'm really not sure how to approach, seeing as how I don't quite get the way that LabView handles variables. I want it to run for a time proportional to the value of the variable at the end of the previous loop, but I don't know how to get that value.

Also, if you could mention quickly how to make my LEDs turn on only while certain loops are running, and turn off afterwards, that would be great. I use version 7.0 and I'll include a copy of my code if you need to see what I'm trying to say. I don't think it matters, but I'm using Labjack to control the motors.
0 Kudos
Message 1 of 6
I'm having some trouble following your code, but an obvious problem is the fact that your event structure doesn't have a timeout. This will actually cause the second loop to iterate only when the event occurs because the program will stop and wait whenever it reaches the event structure unless the event occurs. In general, user interaction through events is best confined to one place.
To pass the value to the last loop, you probably don't need a variable, but just to wire the value from the second loop to the third loop. If you do want a variable, you can use the LV equivalent of a variable, which is a local variable. A local allows you to read and write to a control's value from several places. You should use them only when necessary because when used wrongly, they can easily cause race conditions. You can use a local to turn your LED on and off. Simply right click it and select Create>>Local Variable. Then, place the local anywhere you like and wire T or F into it. Copy the local by using ctrl+drag. This will show you how easily a race condition can occur. Since you're writing to the local from several places, you have to make sure they can't occur in parallel

I hope these thoughts helped you along. If not, don't be afraid to post more questions.

To learn more, I suggest you read the LabVIEW user manual. Also, try searching this site and google for LabVIEW tutorials. Here is one you can start with. You can also contact your local NI office and join one of their courses.
In addition, I suggest you read the LabVIEW style guide.

Try to take over the world!
0 Kudos
Message 2 of 6
Thanks for the help, but I'm still a little shaky on how to accomplish what I need to. The first loop works fine, and I think that I got the LED thing, but that second one is still giving me troubles. I tried to clean up the logical mess by using a formula node, but I'm still not sure if it's working right. I see that there is a problem with the event structure not having a timeout value, but I'm not sure how I should determine that value. I decided to change the way it needs to function, but I'm still not having the best of luck. Here's what I need to happen:

-first loop
Runs for given time (this works)
-seocnd loop
Runs until a value in (currently controled by a knob, but will be an input via the labjack) is lower than a certain level
While this loop is running, if a control is changed, an interior loop must run for a time determined by the difference in the variable.
-third loop
runs for a time based on the final position of the afore mentioned varriable

The problem now seems to be that it won't run the interior loops, despite the variable change. I think it has to do with the timeout value. Is there a way to set it to check once every iteration of the loop? Thanks

Message Edited by Dominj on 04-17-2005 05:31 PM

Message Edited by Dominj on 04-17-2005 05:32 PM

0 Kudos
Message 3 of 6
ok, I think I got most of it to do what I need, the problem now seems to be that I can't get the loop inside the event structure to run for the correct amount of time. It should be a 1 to 1 ratio. One unit on the position slider should equal 1 second of loop time, but I can't get it to time it right. Thanks for any help you can offer.
0 Kudos
Message 4 of 6
Your problem is that you're comparing for a ms counter value you took before the loop started. You should place the ms counter inside the event structure. Then, the time it would take would start when the event is processed. You should be aware that this will stop your outer loop, because until the inner loop is finished running, it will not iterate. Also, be aware of the fact that events are queued - if you have several changes they will all be processed, even if it takes time to process them all. Also, since you say this will come from an outside source, you will need to fire the event. You can do this using the Value (Signaling) property.

2 other notes - since you have the timeout input wired, the 50 ms wait in the loop is unnecessary.
I don't think you need to declare the variables in your code if you have them wired - you can just use their names. Additionally, you can replace the code with a simple case structure. Check if NewVal is bigger than OldVal and inside the case check your other condition. Use Boolean to 0,1 and wire that as X and Y.

Try to take over the world!
0 Kudos
Message 5 of 6
I hope you don't mind my critique. It is not a good idea to have two event structures in one vi. They could cause conflicts. The first event structure does nothing but wait for a Go button to be pressed. Instead of using an event structure, a simple loop will do fine and won't cause any conflicts. What happens if the Go button is pressed a second time while the second loop is being executed? The event will fire again and send a new timer value out. Since the first timer value was already captured by the loop, this may not cause a problem, but maybe you can see why it is not a good idea to use an event structure to simply wait for a button press to start the vi action. See the attached vi for my modification suggestion. One other thing, in the other event structure, you have two inputs at the bottom that come from the same source. Just use one input and tie the two wires inside the event structure like I have done in the attachment. Why use two inputs when one will do.
- tbob

Inventor of the WORM Global
0 Kudos
Message 6 of 6