function synchronize_daq_dwn

daqreset
close all

recording_status=0;
sampling_rate=10000;
samples_per_trigger=10000;

tic

d1output=analogoutput('nidaq',1);
addchannel(d1output,0);       
addchannel(d1output,1);            
% addchannel(d1output,2);           %add this channel for the picospritzer
% addchannel(d1output,3);           %add this channel if the camera needs to be gated
% set(d1output,'TransferMode','SingleDMA') %DMA can only be used by one of the objects, so it is used by d1input
set(d1output,'TransferMode','Interrupts') 
%set(d1output,'TriggerType','HwDigital')
set(d1output,'TriggerType','Manual')
set(d1output,'SampleRate',sampling_rate)
set(d1output,'ClockSource','External');
%get(d1output)

outputt=repmat(0,samples_per_trigger,2);
outputt(3000:4999,1:2)=1;
    	
d1input=analoginput('nidaq',1);
addchannel(d1input,0);
addchannel(d1input,1);              %channels 0 and 1 carry the information recorded from neurons
addchannel(d1input,2);              %this channel is for the temperature gauge -- added feb 11 2004
% addchannel(d1pinput,3);           %spritzer
% addchannel(d1input,4);            %camera
% addchannel(d1input,5);            %third electrode
% addchannel(d1input,6);            %fourth electrode
set(d1input,'SampleRate',sampling_rate)       
set(d1input,'SamplesPerTrigger',samples_per_trigger)
set(d1input,'ChannelSkewMode','Minimum')   
set(d1input,'TransferMode','SingleDMA')    
set(d1input,'TriggerType','Manual')
if recording_status                             %this is part of matlab DAQ logging, the files created for which will be used for the .hdf patch log
    set(d1input,'LoggingMode','Disk&Memory')
else
    set(d1input,'LoggingMode','Memory')
end
% 	actualin=setverify(d1input,'SampleRate',sampling_rate)       %these two lines are for debugging/checks on d1input
% 	get(d1input)
    
putdata (d1output,outputt)
start(d1input)
figure(1), plot(outputt(:,1))

for j=1:1
    %start(d1input)
	trigger(d1input)
    [dataa time]=getdata(d1input);
    %stop(d1input)
 	figure(2)
    plot(dataa)
    %stop(d1input)

	aitime=d1input.InitialTriggerTime;    %these four lines find the difference in the start time of the input object and that of the output object
	aotime=d1output.InitialTriggerTime;   %with 10kHz sampling, it looks like the average value is about eleven ms, and increases if the output obj. sampling rate is increased (output starting *before* input)
	delta=abs(aitime-aotime);             %one consequence is that setting windows based on where pulses occur in the output train cannot work
	sprintf('%d',delta(6))                %instead must find windows based on detection of stim pulses in the recorded trains -- as dwnrawreader does 
	
	figure(3)
	plot(j,delta(end),'o','markerfacecolor','g','markeredgecolor','g','markersize',8),hold on
    pause(10)
end

% stop(d1output)

%     delete(dig_object)
% 	clear dig_object
delete(d1output)
clear d1output
delete(d1input)
clear d1input

toc

% 	dig_object=digitalio('nidaq',1);
% 	%disp('a')
% 	%whos dig_object
% 	%b=get(dig_object,{'name' 'type'})
% 	hwlines=addline(dig_object,1:2,'out','trigger_line');
% 	%c=get(hwlines,{'HwLine', 'Index', 'Parent','Type'})
% 	hwinfo=daqhwinfo(dig_object);
% 	hwinfo.Port(1);
% 	dig_data=1;
% 	putvalue(dig_object.Line(1:2),dig_data)%<--when the trigger is actually executed