Pausing/restarting a while loop, or sequence in general?


Recommended Posts

Hi, I am running a logging and control script on a LabJack U3-LV. Here is the issue I'm trying to solve. I am running a scripted sequence to toggle between two solenoids. I set the number of cycles and time period on a page, then run the sequence. I want the ability to pause the sequence, instead of having to stop and then restart the sequence.

Here is a video showing the problem. I set the number of cycles to 10, then press "RUN" to start the sequence. I stop it after 4 cycles. When I restart, it starts the sequence again at 0 cycles. I would like to script it so it continues at 5 cycles, and then add another button to stop the program. Is this reasonable?

Here's my code:

global N_cycles //declare variable
global period_half //declare variable
global N_cycles_executed=0 //empty variable to use in loop

while (N_cycles_executed < N_cycles)
   //Extension
   Output_contract=5 //turn off contraction solenoid
   Output_extend=0 // turn on extension solenoid
   delay(period_half) // extend for time period
   //Contraction
   Output_contract=0 //turn on contraction solenoid
   Output_extend=5 //turn off extension solenoid
   delay(period_half) //contract for time period
   N_cycles_executed++ //Index number of cycles by 1
   endwhile

 

Link to comment
Share on other sites

Certainly.  Just don't set N_Cycles_executed to 0 in the sequence.  Declare the variable (as I recommend all globals) in a separate sequence marked Auto-Start.  I usually call this sequence Startup.  Then, create three buttons, with Quick Sequence actions:

Start:
N_cycles_executed = 0
beginseq(mysequence)

Continue:
beginseq(mysequence)

Stop:
endseq(mysequence)

This, of course, does not deal with the state of the solenoid when stop is clicked, but it does keep it from resetting the counter.  To do a true Pause you would have to use a flag and replace the delay() statements in your script with a while loop that watches the flag as well as the time.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share