LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Case structure / event structure / while loop

Hello everyone

I am writing the goal/problems in our code and in addition, I am attaching the VI file with 3 issues highlighted on the front panel as comments. (Issue# , #= 1,2,3)

Note: if you want to run the code please don't forget to change the path of the text file in the code according to where are you saving it on your PC. The text file contains the variables we are using. 

 

Goal: Two variables (data type is String – “Plant 1” & “Plant 2”) are inputs from the user. If “Plant 1” selected the LED should blink at “X” frequency and if “Plant 2” then at “Y” frequency. Note: The user can select One Plant or Both Plants (at a time). Code and Questions: 1. A code for blinking the LED using while and the shift register is working. 2. Next, we have tried to use Case Structure (selector, using with string data type – not sure, it is feasible: Question: How to use it?) to select “Plant 1” and “Plant 2”. Adding the code (blinking LED) inside the case structure, we are struggling to stop the while of that selected plant. Then we thought of using Event Structure. Still not helping it. Are we thinking of the right way to go ahead with Case Structure / Event Structure to achieve our goal or else there are some different functions in LabView which might help us to achieve it?

 

Thanks for helping 

 

Ali 

0 Kudos
Message 1 of 6
(3,238 Views)

Hi ali,

 

all your "issues" are due to not understanding the basic LabVIEW mantra of "THINK DATAFLOW!"

 

(You really should take the beginner courses offered in the "Training resources" in the LabVIEW board header.)

 

Issue #1: Due to DATAFLOW that selection is only read once when your VI starts…

Issue #2: The stop button is stopping your while loop - but that may take some time depending on your frequency/OnTime settings…

Issue #3: It's the very same problem as in issue #1 - you don't obey to THINK DATAFLOW!

 

Btw. the meaning of "DATAFLOW" is explained in the LabVIEW help…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 6
(3,217 Views)

As Gerd already saind, do some basic tutorials. So far, you are completely misunderstanding the meaning of dataflow, a very simple concept once learn it.

 

Some more comments:

  • There is a primitive for 1/x
  • A multiplication by 1/x is the same as a division by x
  • the "ON x 10/F" belongs after the case structure, because it is the same in both cases. Only code that differs (e.g. ON vs 100-ON) belong inside the case structure.
  • Never maximize the front panel and diagram to the screen. At your stage of learning you need to see both at the same the as well as the help window. Don't get tunnel vision!
  • The ON-time should be limited to the range 0..100. Maybe label it duty cycle instead (0..100%).
  • All controls should have typical default data. What are typical values for the controls? We cannot even guess!
  • Why are there two LEDs connected to the same wire each? Seems redundant.
  • If you want to be able to change the orange controls during run, they belong inside the loop. Same for the cluster.
  • All you probably need is one single loop running at a reasonable rate, one stop button, and some logic that switches the boolean depending on elapsed time and state.
  • Have you tried running your VI with execution highlighting? A good way to learn dataflow principles.
0 Kudos
Message 3 of 6
(3,183 Views)

See if this can give you some ideas....

 

altenbach_0-1623019310589.png

 

Message 4 of 6
(3,171 Views)

Hi altenbach 

 

Thanks for your reply. 

 

I have gone through your code and try to understand the logic behind it but I struggled with that subtract gate (substruct the output from build array from 1-second wait). what exactly mean here?

and why we have built an array of the millisecond timer value? I have googled that and I see how does this tick clockwork but I am not sure what are you trying to use it for in this code. 

are you trying to approach real-time compilation (when the user presses some button to reduce the delay in response on the output LEDs)?!!

 

 2. Why frequency is divided by 1000? so the final form, when it goes to be compared with that output from the subtractor, is ( ( 1000 / frequency) * Duty cycle %). 

 

one more question please,

 

3. Why and how are we using shift registers with an array?  

 

Thanks for helping.

 

Ali 

0 Kudos
Message 5 of 6
(3,081 Views)

The output of the wait is the same as the ms tick count, which is a relative counter that increments every ms.

 

Since the wait is in ms, we need to scale the frequency, which is in Hz, to something that makes sense to a millisecond counter. Thus the factor of 1000.

 

Since the elapsed time is relative to a start tick count, we need one for each LED, because they reset at different times, depending on the frequency. For each LED, we maintain a start tick and the difference to the current tick will give us the elapsed time. If the cycle is complete, we need to update to the new start tick so the difference start over with zero.

 

We need an array to store a starting tick for each LED. You can easily expand the same code to 10 LEDs by just increasing the array sizes. If you only had one LED, you would not need an array.

 

Basically, we calculate how many ticks it takes for a given frequency, and if we account for the duty cycle, we know how many ticks it takes for the LED to turn off. For example if the frequency is 10Hz, we wait for 100 tics to repeat, and if the duty cycle is 30%, the LED is true for 30 ticks. And so on. 

Message 6 of 6
(3,069 Views)