11-30-2021 02:36 PM
Hello all,
I am trying to write a program that increments by 20, goes up to 40, waits for Boolean to be activated before continuing, then goes down to zero.
I currently am using a shift register within just one of the case structures (which is working as intended) but when I get to the 3rd case structure, the shift register resets back to zero and starts passing -20. So instead of starting at 40 and reducing to 20, it instead starts at 0 and reduces to -20, breaking the code. How can I change my code to pass the number 40 all the way to the start of the 3rd case structure?
Thank you very much,
Josh
Solved! Go to Solution.
11-30-2021 02:53 PM
Hi Josh,
as a first step please learn about typedefining datatypes, like enums - and using the typedefinition for all instances of that enum! Especially as you try to build up a statemachine…
To improve your statemachine: Don't use while loops within the states: you already have a while loop around the whole statemachine!
@Josh_Music wrote:
when I get to the 3rd case structure, the shift register resets back to zero and starts passing -20. So instead of starting at 40 and reducing to 20, it instead starts at 0 and reduces to -20, breaking the code. How can I change my code to pass the number 40 all the way to the start of the 3rd case structure?
When you want to avoid "default" values then you should not use "default if unwired" tunnels in your case structure!
11-30-2021 03:32 PM
@GerdW wrote:
Don't use while loops within the states: you already have a while loop around the whole statemachine!
I can imagine a VI without the outermost while loop, but in order to pass information through iterations using the shift register, the while loop is the most direct method. I cant think of any problems with putting a while loop nested in a case structure. Could you please explain that?
Wiring the shift register through each of the cases solved my problem-thank you!
11-30-2021 03:42 PM
@GerdW wrote:
Hi Josh,
as a first step please learn about typedefining datatypes, like enums - and using the typedefinition for all instances of that enum!
Agreed, especially since in your case you are connecting different enums in different cases, thus turning you cases into numbers instead of enum items.
Of course if the enum items are arranged in typical order You can go to the next case with a +1, thus you only need one single instance of the constant on the diagram and making it a typedef is no longer really needed (but still highly recommended!)
To elaborate on that and what Gerd already mentioned, here's a quick rewrite. Note that I set the increment to 1 to make it look a little bit more interesting. Unless you can guarantee that the upper and lower limits are reached exactly, you should never do equal comparisons. For example if somebody changes the step to e.g. 19, a value of 40 never occurs and your instrument will blow up way before the value reaches a million volts. 😄
11-30-2021 03:43 PM
@Josh_Music wrote:
@GerdW wrote:
Don't use while loops within the states: you already have a while loop around the whole statemachine!
I can imagine a VI without the outermost while loop,!
Gerd told you to eliminate the inner loops. Not the toplevel loop! Right?