LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to reset a for loop

I am pretty new to LabVIEW so I am having trouble with what I believe to be a simple issue. Basically the goal of this program it to cycle a relay at determined rate for a determined number of cycles. The program I have written does what I need it to, my issue is I want to activate the cycles with a button. Right now as soon as I press run it will cycle  times (Numeric 3) and not reset. From my understanding a shift register will accomplish my goal, but I appear to be setting it up incorrectly.

 

Thanks in advance

Download All
0 Kudos
Message 1 of 6
(979 Views)

Hello Dfturner,

 

Use Event Based State Machine Architecture to achieve your Requirement.

There is no Use of While Loop and Even if you remove it will behave like the same.

 

Its Always better to share the code than images.

 

----------------------------------------------------------------------------------------------------------------
Palanivel Thiruvenkadam | பழனிவேல் திருவெங்கடம்
LabVIEW™ Champion |Certified LabVIEW™ Architect |Certified TestStand Developer

Kidlin's Law -If you can write the problem down clearly then the matter is half solved.
-----------------------------------------------------------------------------------------------------------------
0 Kudos
Message 2 of 6
(963 Views)

I went back and re-added the VI to the attachments. Thanks for the response,  I will give it a shot

0 Kudos
Message 3 of 6
(945 Views)

The Run button is simply "Run What is on your block diagram."  What is on your block diagram?  A FOR Loop that does stuff.  Remember why it's called a FOR loop.  It's DO FOR 'insert condition.'  Typically the conditions are terminal.  Psuedo: FOR i<x, i++: As in DO (the following code) FOR i (iterations) times until i is greater than or equal to x (or phrased while i is less than x).  In your code you have the FOR loop iterating at whatever number you put into Numeric 3.  And you absolutely should not (and cannot) put INF.

 

You also have a While Loop that runs once due to the True boolean wired to the stop.  What you've written: Do (the following codee) While The Condition is False - or... Do (the following) Until the Condition is True.

 

It total it's:

Psuedo:

Do the Following For 'Numeric 3' Iterations {

 - Initialize Digital Output

 - Start

 Do the Following Until Stop is True {

 - Set Dig Bool

 - Wait for (X * 1000) ms

 - Set Dig Bool

 - Wait for (X2 * 1000) ms

 -Check Condition is True (It is True)

 Stop While

 }

Close and Stop Dig Output

}

 

I recommend CTRL-H in your block diagram, hover over stuff and read about it - read the detailed descriptions too.  You should be able to figure this out with the hints I've given you.  And also ask yourself, why is there a While Loop inside your For Loop.

0 Kudos
Message 4 of 6
(932 Views)
  • For incrementing the "i" value, there is a +1 primitive.
  • If you are only using one line on DIO port, change the DAQmx write to Single Channel, Single Sample (1 line)
  • The sequence structure is not required.  Use Stall Data Flow timing VI on the error line. 
  • Ideally, a state machine architecture would work better for this.  I like using JKI state machine but I'm sure there are other state machine layouts you could use that are maybe simpler to learn and understand as a beginner.  But this would give you functionality for button start and better organization of your code and a stronger foundation to build upon. 
  • With a proper state machine architecture, the for loop and while loop are not even necessary and generally not desired because your code execution gets stuck in those loops, and UI becomes unresponsive.  Rather, with a state machine, the same state can be called over and over.  Even the wait between relay flips can be made to not interfere with UI responsiveness, meaning you could stop the whole process with a button press even if the execution is 'waiting' for next relay flip. 
aputman
------------------
Heads up! NI has moved LabVIEW to a mandatory SaaS subscription policy, along with a big price increase. Make your voice heard.
0 Kudos
Message 5 of 6
(897 Views)

@aputman wrote:
  •  <many good points>

Also, you equal comparison wired to the stop condition will never do anything useful, because it is always FALSE. The FOR loop will run for as many times you wire to N and then end. At that points, i=(n-1),so i=N can never be true. You don't even need to show the conditional terminal (as in 99% of all LabVIEW FOR loops in the world).

Your while loop does absolutely nothing because it stops after one condition. It is basically a glorified (and completely unnecessary!) sequence frame.

Why do you think you need to configure a task from scratch and destroy it with every iteration of the FOR loop? That typically only needs to be done once.

0 Kudos
Message 6 of 6
(863 Views)