LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Want to change Event cycle variable in real time as well

Solved!
Go to solution

Hi... A beginner here. I have a vi that writes value in real time in "VALUE" variable.

1. When I press auto cycle "value" should be updated with random number in every 10 sec span.(6 times)

2. I want to modify my code such that if I give input to "SET VALUE" anytime it should be able to update variable "Value".

3. A value can only have input of set value during particular 10 sec span and after that again value takes random variable for next cycle of 10 sec.  

0 Kudos
Message 1 of 17
(382 Views)

You're mixing (at least) 3 paradigms. 

 

You're

+ polling (control-> indicator)

+ using events (Boolean trigger)

+ looping with timer (time out and wait for multiple).

 

This won't work well. You can of course mix event driven design and timed loops, but to do it properly (growable, maintainable) you'll have to step up architecture a bit (for instance queued message handler).

 

Event Structures work best when no event takes significant time to execute. The loop with timing is a mistake. It will ruin event driven design. It can work, if the event should block user interaction until it is done, but that's not what you want.

 

For now, for learning, you can keep it simple.

 

I intentionally do not hand you a complete solution.

 

So, you catch events:

+ The Boolean should trigger automatic updates of the value. 

+ The Set Value should update the value manually.

+ On time out, you can do things.

 

A note on the TO. Note that TO does not trigger when events keep coming in. Add for instance a mouse move event, and no TOs will trigger until you stop moving the mouse. In you're project, this is no problem at all. Set Value should probably interrupt the timed updates. The Boolean should too. 

 

+ In the Boolean value change event, set the random numbers, and times they should trigger. So, 2 arrays, one with numbers, one with timestamps. Optionally (not sure what the design is), add to the arrays that are there. 

+ Put the arrays in two shift registers.

+ In the time out case, compare the time with the 1st timestamp. If it's time to trigger, remove the value and the timestamp. If the array is empty, don't do anything.

+ on the Set Value event, update the value. Optionally (not sure what the design is) empty the array of values and timestamp.

 

Here's a quick mockup. Just a small peace of the puzzle, what is shown is all that's implemented:

 

Random Values.png

Message 2 of 17
(347 Views)

Thanks for the suggestion. I understood the events. I created the code I wanted and it is working fine. I am attaching the code here. Is it optimized? or can I create better version of it?

0 Kudos
Message 3 of 17
(313 Views)

@doc_d wrote:

Thanks for the suggestion. I understood the events. I created the code I wanted and it is working fine. I am attaching the code here. 


Looks like you ignored (or didn't understand) all\any advice, and made the original worse. 

 

The solution doesn't require more screen space that the VI I posted.

 

You don't need locals.

You don't need a sequence structure.

You don't need any structure except the while loop and the event structure.

 


@doc_d wrote:

 Is it optimized?


No.

 


@doc_d wrote:

 or can I create better version of it?


Yes.

 

Consider what I posted. Add a case for the Time Out event. Add a case for Add Value event. Put the code for the events in there.

Message 4 of 17
(305 Views)

@doc_d wrote:

Thanks for the suggestion. I understood the events. I created the code I wanted and it is working fine. I am attaching the code here. Is it optimized? or can I create better version of it?


This is certainly not optimized, and while it may "appear" to work I don't think that it works the way that you think that it works. This lack of proper architecture will come back and bite you in more complicated applications. Do you understand the concept of dataflow? You are using a lot of local variables. Front panel controls and indicators are NOT variables, they are controls and indicators. The wires are the variables. When you use local variables you break dataflow and create race conditions. You have your Event structure in the same loop as parallel code that takes longer to run - you might as well put the code in the event structure (I don't recommend this, by the way, unless you properly use the timeout event as your timer). 

Message 5 of 17
(301 Views)

"In the time out case, compare the time with the 1st timestamp. If it's time to trigger, remove the value and the timestamp. If the array is empty, don't do anything."

Lost track here. 

 

And I have other events which may execute as well during the cycle will this affect my variables? 

0 Kudos
Message 6 of 17
(297 Views)

@doc_d wrote:

"In the time out case, compare the time with the 1st timestamp. If it's time to trigger, remove the value and the timestamp. If the array is empty, don't do anything."

Lost track here. 


Random Values.png

 


@doc_d wrote:

And I have other events which may execute as well during the cycle will this affect my variables? 


Yes, if you want this. No, if you don't.

 

It's your program, you can make it do what you want. If you know how.

Message 7 of 17
(277 Views)

@doc_d wrote:

 

And I have other events which may execute as well during the cycle will this affect my variables? 


Since you have everything running inside the loop your other events will not occur until after everything is completed (including your wait). If you have a lot of events occurring this could lead to getting your events queued up. You need to take the advice that has been given. 

 


@doc_d wrote:

"In the time out case, compare the time with the 1st timestamp. If it's time to trigger, remove the value and the timestamp. If the array is empty, don't do anything."

Lost track here. 

 


This seems pretty self explanatory to me, although I wouldn't worry with an array. Just keep the starting time in a shift register or feedback node and update it when a new timer is started. 

Message 8 of 17
(263 Views)
Solution
Accepted by topic author doc_d

@doc_d wrote:

Hi... A beginner here. I have a vi that writes value in real time in "VALUE" variable.

1. When I press auto cycle "value" should be updated with random number in every 10 sec span.(6 times)

2. I want to modify my code such that if I give input to "SET VALUE" anytime it should be able to update variable "Value".

3. A value can only have input of set value during particular 10 sec span and after that again value takes random variable for next cycle of 10 sec.  


A good program should always start with a good set of requirements. Here we have huge holes:

 

  • Define "real time"?
  • LabVIEW does not have "variables", just wires, terminals and connectors. You are talking about an indicator here.
  • What should happen after 6 random values have been generated? (e.g. if the set value is changed, should the value persist forever or again change to random after 10 seconds?)
  • Does a manual change count as one of the six updates or not?
  • When pressing "auto cycle", should the first random number be generated immediately or only after 10 seconds?
  • What should be displayed in the indicator before the user interacts with the front panel? (zero? a random number? the value of the set value control?)

 

In any case, see if this can give you some ideas. Make sure you fully understand it, then modify according to your specific requirements.

 

altenbach_0-1627054241848.png

 


LabVIEW Champion. It all comes together in GCentral GCentral
What does "Engineering Redefined" mean??
Message 9 of 17
(261 Views)

Thank you all. I got the solution and more importantly learned new stuff as well. As a beginner I am yet to learn the software in deep. Trying my best. 

0 Kudos
Message 10 of 17
(255 Views)