STOP ACQUISITION

Hello everyone,
with igor, my tutor and I, we have created a procedure to aquire the signal by the AFM.
With a while loop, we can make acquisitions at different intervals of time, and through the script of save, we can save the "Wave".
Now we would like to put a stop function: we would like to create a button "STOP" in the graphical interface, which allows to stop the acquisition, and once stopped, automatically save the file until that instant of time.
On the forum I found one scritp "pseudo stop button" but adapting it to our procedure, does not work very well.
you help us?
It would be helpful if you gave more specific details of your problem, including code snippets (properly formatted for Igor Exchange). In the mean time, I am guessing that you have the common problem that Igor does not look at controls while your function is running. Stopping using the "Abort" button method will leave your data acquisition instrumentation in an uncertain state, and not allow you to save results.

I suggest you look at the Manual, Chapter IV-6 "Interacting with the User" (p. IV-139) for a code example of how to detect a key press (Escape), control the exit from your function, and perform whatever data save function you create.
Here is a 'Escape' code example that I used in data acquisition. Data was continuously being acquired in large waves, processed, and displayed in an indefinite Do...While(1) loop. The following method ensured that an acquisition/process/display cycle was completed after the Escape key was pressed.

Function PressEscapeToAbort(phase, title, message)
	Variable phase 	// 0: Display control panel with message.
				// 1: Test if Escape key is pressed.
				// 2: Close control panel.
	String title 	// Title for control panel.
	String message 	// Tells user what you are doing.
	if (phase == 0) 	// Create panel
	  DoWindow/F PressEscapePanel
	  if (V_flag == 0)
		NewPanel/K=1 /W=(350,800,500,900)
		DoWindow/C PressEscapePanel
		DoWindow/T PressEscapePanel, title
	  endif
	  String abortStr = "Press escape to abort"
	  TitleBox Press,pos={16,40},size={106,20},title=abortStr
	  DoUpdate
	endif
	if (phase == 1) // Test for Escape key
	  Variable doAbort = 0
	  if (GetKeyState(0) & 32) // Is Escape key pressed now?
	    doAbort = 1
	  else
	    if (strlen(message) != 0) // Want to change message?
		TitleBox Message,title=message
		DoUpdate
	    endif
	  endif
	  return doAbort
	endif
	if (phase == 2) // Kill panel
	  DoWindow/K PressEscapePanel
	endif
	return 0
End

In your code, before the acquisition begins, put the following command to open a Panel saying "Press escape to abort"

PressEscapeToAbort(0,"","")

Then later in the code after all necessary operations have been done for that cycle, before While(1), put something like

  if (PressEscapeToAbort(1, "", "")) 
// 1: tests for Esc key press; exit point from Do-While loop    
    Print "Test aborted by Escape key."
//..your clean-up, data save, and acquisition termination commands go here...
    break  // exits from Do..While
  endif
While(1)

Then just before the end of your function, put

PressEscapeToAbort(2, "", "") //  2: Kill information panel for 'Escape'	

You may have to press the Escape key more than once, or make sure it is held down sufficiently long.
HI s.r.chinn, nice to meet you!
This is my procedure.
So, I need to "abort" my acquisition when I want to, so you save all that acquired until then.

If I understand it, I should write down your first script in the DO-while loop, then, before the function acuisizione insert the second script, then?
correct me if I'm wrong.

 
function master(experimenttime, recordingtime, duration, rate, savedrate, plotresolution,savepath)
variable experimenttime, recordingtime, duration, rate, savedrate, plotresolution
string savepath
variable psdflag=1 // first time, make the periodogram
NewPath/O Path1,savepath//defines where to save the files

string timenow=date()+" "+time() // date and time now
string filename
variable t0 = stopMSTimer(-2)/10^6 // consults the microseconds timer, from the moment of PC startup
variable t1=0, dt1=0, dt2=0 // time init
if (plotresolutionduration
make /O/N=0 variancestrip //where to store the variance every plotresolution>duration
setscale/P x, t1, (plotresolution), "s", timestrip, deflstrip, variancestrip // sets the spacing of the waves, just to have "s" as unit
setscale/P y, 0,1,"V" deflstrip, variancestrip 
setscale/P y,0,1,"s",timestrip
//display
display/W=(1,1,800,150) deflstrip vs timestrip
AppendToGraph/L=L2 variancestrip vs timestrip
ModifyGraph axisEnab(left)={0,0.45}
ModifyGraph axisEnab(L2)={0.55,1}
ModifyGraph freePos(L2)=0
ModifyGraph lblPosMode(L2)=2
ModifyGraph rgb(deflstrip)=(0,0,0)


make /O/N=0 storeaverage, storevariance, storetime, waveaverage, wavevariance, wavetime // to store average and variance for the recordingtime
display/W=(8, 310, 400,550) storeaverage vs storetime
variable acq_cycle=0 // counter for acquisition cycle
do //big loop for acquisition and plot
	t1=stopMSTimer(-2)/10^6-t0 // evaluate the experimental time
	//print "t1=",t1
	waveacquire(t1, duration, rate, savedrate, psdflag) // DATA ACQUISITION
	dt1+=stopMSTimer(-2)/10^6-t0-t1 // increment the delta time for plotting
	dt2+=stopMSTimer(-2)/10^6-t0-t1 // increment the delta time for saving
	
	insertpoints numpnts(storeaverage), numpnts(waveaverage), storeaverage // extends storeaverage
	storeaverage[numpnts(storeaverage)-numpnts(waveaverage), numpnts(storeaverage)-1]=waveaverage[p-numpnts(storeaverage)+numpnts(waveaverage)-1] // stores the averaged deflection points
	insertpoints numpnts(storevariance), numpnts(wavevariance), storevariance // extends storevariance
	storevariance[numpnts(storevariance)-numpnts(wavevariance), numpnts(storevariance)-1]=wavevariance[p-numpnts(storeaverage)+numpnts(waveaverage)-1] // stores the variance time points too
	insertpoints numpnts(storetime), numpnts(wavetime), storetime // extends storetime
	storetime[numpnts(storetime)-numpnts(wavetime), numpnts(storetime)-1]=wavetime[p-numpnts(storeaverage)+numpnts(waveaverage)-1] // and the times
	
	acq_cycle=+1 //increment the counter for acquisition cycle
	if (psdflag==1)
		psdflag=0
	endif // switches off psd
	//print dt1," ",dt2
	if (dt1>=plotresolution) // after exiting this loops, it adds a plot point ... 
		// insertpoints numpnts(deflstrip), 1, deflstrip, variancestrip // makes room
		timestrip[numpnts(timestrip)]={stopMSTimer(-2)/10^6-t0} // to store the time too
		deflstrip[numpnts(deflstrip)]={mean(storeaverage, pnt2x(storeaverage, numpnts(storeaverage)-acq_cycle*rate/savedrate), pnt2x(storeaverage, numpnts(storeaverage)))}
		variancestrip[numpnts(variancestrip)]={mean(storevariance, pnt2x(storevariance, numpnts(storevariance)-acq_cycle*rate/savedrate), pnt2x(storevariance, numpnts(storevariance)))/sqrt(acq_cycle*rate/savedrate)}
		doupdate // explicitly ask to update the plots (in hope it has not done it before at insertpoint)
		// print "plotplot"
		dt1=0 //resets delta time for plotting
		acq_cycle=0 // resets acquisition cycle, so it later knows what to use for plotting
	endif
	if  (dt2>=recordingtime) // after exiting this loop, it records on file and resets storeaverage and storevariance
		// saves storeaverage and storevariance on file with automatic names 
		// empty storeaverage and storevariance for next acquisitions
		 timenow=date()+" "+time() // updates time
		 filename="DeflectionStrip "+num2str(savedrate)+"Hz on "+timenow+"-"+num2istr(dt2)+".txt" // for saving
		save /G/P=Path1 storetime storeaverage storevariance as filename		// print "I want to save storeaverage and storevariance"
		redimension/N=0 storetime, storeaverage, storevariance //clean up
		dt2=0 //resets delta time for recording
		psdflag=1 // nextg cycle, record the PSD (if there is no next cycle, it records it at the end)
	endif
	

while (t18192*4)
	segmentsize=8192// segment size for Periodogram calculation
else
	segmentsize=(duration*rate)/5
endif

make/O/N=(rate*duration) wave0
make/O/N=(rate*duration/blocksize) wavevariance // wave variance decimated
make/O/N=(rate*duration/blocksize) waveaverage // wave after decimation
make/O/N=(rate*duration/blocksize) wavetime // to store the time points
make/O/N=0 media //to store the recordings
make/O/N=0 timepoints //to store the time of recordings
make/O/N=(segmentsize/2+1) periodogramdomain
setscale/P x, t1, (1/rate), "s", wave0 // sets the spacing of the wave, so the sampling rate
DAQmx_scan/DEV="dev4" WAVES="wave0, 2;" //acquires from DEV device on channel 2 and puts in wave0

if (psdflag==1)
	DSPPeriodogram/Q/DLSG/NOR=(segmentsize*segmentsize/2)/SEGN={(segmentsize),(segmentsize/2)}/Win=hanning wave0
	timenow=date()+" "+time() // updates time
	filename="PSD "+num2str(savedrate)+"Hz on "+timenow+".txt" // for saving
	periodogramdomain=pnt2x(W_periodogram, p)
	save /G/P=Path1 periodogramdomain W_periodogram as filename		// print "I want to save storeaverage and storevariance"

endif

wavevariance=variance(wave0, pnt2x(wave0, blocksize*p), pnt2x(wave0, (blocksize*(p+1))-1))/blocksize
setscale/P x, t1, blocksize*(1/rate), "s", wavevariance // sets the spacing of the wave, so the sampling rate
waveaverage=sum(wave0, pnt2x(wave0, blocksize*p), pnt2x(wave0, (blocksize*(p+1))-1))/blocksize
setscale/P x, t1, blocksize*(1/rate), "s", waveaverage // sets the spacing of the wave, so the sampling rate
wavetime=pnt2x(wavevariance, p) // timings from the scale (?)
variable stoptimer=stopMSTimer(-2) // stops the microseconds timer
end


Carlo,
Here is an edited copy of your main function code, with the addition of my suggested placement of the previously given Escape code sequences. I have indented your code, eliminated many of your comments, and added artificial linewrap indicators to keep line lengths in the Exchange format to a minimum. My insertions are indicated by
// ***NEW***
labels. You might consider performing your last "waveacquire" within the
PressEscapeToAbort(1,"","")
if-endif loop. You may notice a delay after the Esc key is pressed.

#pragma rtGlobals=3		// Use modern global access method and strict wave access.
function master(experimenttime, recordingtime, duration, rate, savedrate, plotresolution,savepath)
	variable experimenttime, recordingtime, duration, rate, savedrate, plotresolution
	string savepath
	variable psdflag=1 // first time, make the periodogram
	NewPath/O Path1,savepath//defines where to save the files
 
	string timenow=date()+" "+time() // date and time now
	string filename
	variable t0 = stopMSTimer(-2)/10^6 // consults the microseconds timer, from the moment of PC startup
	variable t1=0, dt1=0, dt2=0 // time init
	if (plotresolutionduration
	make /O/N=0 variancestrip //where to store the variance every plotresolution>duration
	setscale/P x, t1, (plotresolution), "s", timestrip, deflstrip, variancestrip 
	setscale/P y, 0,1,"V" deflstrip, variancestrip 
	setscale/P y,0,1,"s",timestrip
	//display
	display/W=(1,1,800,150) deflstrip vs timestrip
	AppendToGraph/L=L2 variancestrip vs timestrip
	ModifyGraph axisEnab(left)={0,0.45}
	ModifyGraph axisEnab(L2)={0.55,1}
	ModifyGraph freePos(L2)=0
	ModifyGraph lblPosMode(L2)=2
	ModifyGraph rgb(deflstrip)=(0,0,0)
 
 
	make /O/N=0 storeaverage, storevariance, storetime, waveaverage, wavevariance, wavetime 
	display/W=(8, 310, 400,550) storeaverage vs storetime
	variable acq_cycle=0 // counter for acquisition cycle
	PressEscapeToAbort(0,"","")   //****************NEW*********************
	do //big loop for acquisition and plot
	   t1=stopMSTimer(-2)/10^6-t0 // evaluate the experimental time
	   //print "t1=",t1
	   waveacquire(t1, duration, rate, savedrate, psdflag) // DATA ACQUISITION
	    dt1+=stopMSTimer(-2)/10^6-t0-t1 // increment the delta time for plotting
	   dt2+=stopMSTimer(-2)/10^6-t0-t1 // increment the delta time for saving
 
	   insertpoints numpnts(storeaverage), numpnts(waveaverage), storeaverage // extends storeaverage
	   storeaverage[numpnts(storeaverage)-numpnts(waveaverage), 
	       &&linewrap numpnts(storeaverage)-1]=waveaverage[p-numpnts(storeaverage)+numpnts(waveaverage)-1] 
	   insertpoints numpnts(storevariance), numpnts(wavevariance), storevariance // extends storevariance
	   storevariance[numpnts(storevariance)-numpnts(wavevariance), 
	       &&linewrap numpnts(storevariance)-1]=wavevariance[p-numpnts(storeaverage)+numpnts(waveaverage)-1] 
	   insertpoints numpnts(storetime), numpnts(wavetime), storetime // extends storetime
	   storetime[numpnts(storetime)-numpnts(wavetime), 
	       &&linewrap numpnts(storetime)-1]=wavetime[p-numpnts(storeaverage)+numpnts(waveaverage)-1] 
 
	   acq_cycle=+1 //increment the counter for acquisition cycle
	   if (psdflag==1)
	     psdflag=0
	   endif // switches off psd
	   //print dt1," ",dt2
	   if (dt1>=plotresolution) // after exiting this loops, it adds a plot point ... 
	     // insertpoints numpnts(deflstrip), 1, deflstrip, variancestrip // makes room
	     timestrip[numpnts(timestrip)]={stopMSTimer(-2)/10^6-t0} // to store the time too
	     deflstrip[numpnts(deflstrip)]={mean(storeaverage, pnt2x(storeaverage, 
	       &&linewrap numpnts(storeaverage)-acq_cycle*rate/savedrate), pnt2x(storeaverage, numpnts(storeaverage)))}
	     variancestrip[numpnts(variancestrip)]={mean(storevariance, pnt2x(storevariance, 
	       &&linewrap numpnts(storevariance)-acq_cycle*rate/savedrate), pnt2x(storevariance, 
	       &&linewrap numpnts(storevariance)))/sqrt(acq_cycle*rate/savedrate)}
	     doupdate // explicitly ask to update the plots (in hope it has not done it before at insertpoint)
	    // print "plotplot"
	     dt1=0 //resets delta time for plotting
	     acq_cycle=0 // resets acquisition cycle, so it later knows what to use for plotting
	   endif
	   if  (dt2>=recordingtime) 
	     // saves storeaverage and storevariance on file with automatic names 
	     // empty storeaverage and storevariance for next acquisitions
	     timenow=date()+" "+time() // updates time
	     filename="DeflectionStrip "+num2str(savedrate)+"Hz on "+timenow+"-"+num2istr(dt2)+".txt" 
	     save /G/P=Path1 storetime storeaverage storevariance as filename	
	     redimension/N=0 storetime, storeaverage, storevariance //clean up
	     dt2=0 //resets delta time for recording
	     psdflag=1
         endif 
	   if(PressEscapeToAbort(1,"","")) //  ******************NEW*****************
	     // perform any commands and functions to save data
	     // or terminate your data acquisition instruments here
	     break  // exit the do-while loop
	   endif
	while (t1
thank you so much, I tried yesterday the script works perfectly !!! Thank you.