From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help making a simple math loop

Solved!
Go to solution

Hey there,I'm an idiot who spent the past 2 hours trying to do something really simple but failed to do it.  

Basically i have a number(lets call it X) this number needs to be higher than Y(another constant), and if it its not , i want to keep multiplying X by 2 until X is bigger than Y, what i'm failing to do is the loop for some reason, i keep getting " wire is a member of a cycle".  

Thank you all

0 Kudos
Message 1 of 7
(2,746 Views)

Hi Hamza,

 

some pseudocode:

while x < y
  x := 2*x
wend

All you need is a simple while loop with a shift register, a comparison and the "*2" part…

 

Maybe you should post your VI so we can tell what you made wrong?

(There also is a loopless solution possible using the binary logarithm…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 7
(2,743 Views)

Thanks a lot Im off to a new problem now, the while loop keeps looping without giving value to out of the loop.  

the only part you need to pay attention to is the top right part, the inside of the True part of the case structure.  

Thanks!

Edit:Update the vi a little, simple problem

Download All
0 Kudos
Message 3 of 7
(2,725 Views)
Solution
Accepted by HamzaR

Hi Hamza,

 

the while loop keeps looping without giving value to out of the loop.

THINK DATAFLOW!

The loop keeps spinning because the loop condition is calculated before the loop and will NEVER changes inside the loop!

You need to do the comparison INSIDE the loop!

 

the only part you need to pay attention to is the top right part,

Well, no… 😄

- use shift registers instead of feedback nodes, they are easier to understand for LabVIEW beginners (IMHO)…

- use the Select node instead of a case structure when you just want to switch between two values…

- NEVER hide or delete the label of terminals in the block diagram!

- use AutoCleanup from time to time!

- put a wait in your loop to prevent it from spinning as fast as possible! (That's not needed for UI handling…)

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 4 of 7
(2,698 Views)

the only part you need to pay attention to is the top right part,

Well, no… 😄

 


I meant the whole vi not the case.  

 

As you can tell, i'm still learning labview ,So thank you so much for the tips, super helpful, i didnt even know there was an auto cleanup!.  

Great job! thanks!

 


 

- use the Select node instead of a case structure when you just want to switch between two values…

Could you please explain whats the advantages besides being cleaner? i just find case structures a lot easier to read and understand,and its easier to modify 

0 Kudos
Message 5 of 7
(2,684 Views)

Hi Hamza,

 

Could you please explain whats the advantages besides being cleaner?

Isn't it enough justification to provide cleaner code? 😄

 

In your case you just select one of two wires, no other operations involved: in this case I prefer the Select node as I immediatly see what is happening in the code for both TRUE/FALSE cases.

When there are lots of other code for each of both cases I put them inside a case structure to have the CPU only do the needed stuff…

Best regards,
GerdW


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

@HamzaR wrote:

Could you please explain whats the advantages besides being cleaner? i just find case structures a lot easier to read and understand,and its easier to modify 


The Select node does not hide code like the Case Structure.  So it is usually better to just use the Select node for simple choices on a single value (easier to see the full flow of data).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 7 of 7
(2,663 Views)