function [data] = diodedelta(Curmax,Curmin,Curinc,fname)
%[data] = supercond(tempFollow,tParam,sampleCurr,dvmRange,ptInterval)
%
%Collects and logs data for the superconductivity experiment.
%

%
%set parameters
%NOTE: do not change these or some data might be lost in transit
numDeltaPoints =25; %number of individual delta mode measurements to be averaged for each data point

deltaDelay = 0.1; %delay

%initialize some things
pause on;
data = [0,0];




%create and open instrument objects, and echo their names
lakeshore = visa('ni','gpib0::12::INSTR');
fopen(lakeshore);
fprintf(lakeshore,'*IDN?');
lakeshoreName = fscanf(lakeshore);

keithley = visa('ni','gpib0::13::INSTR');
fopen(keithley);
fprintf(keithley,'*IDN?');
keithleyName = fscanf(keithley);

disp('Successfully connected to:')
disp(lakeshoreName(1:end-2)); %eliminates trailing whitespace
disp(keithleyName(1:end-2));  %eliminates trailing whitespace
disp(' ');

%check keithley internal setup
fprintf(keithley,'SOUR:DELT:NVPR?');
is2182aConnected = fscanf(keithley);
if is2182aConnected ==1,
    disp('WARNING: Voltmeter is not connected.')
    disp(' ')
end

%set up voltmeter
fprintf(keithley,'SYST:COMM:SER:SEND "VOLT:RANGE 10.0"');
fprintf(keithley,'SYST:COMM:SER:SEND "VOLT:NPLC 1"');

%set up keithley for delta mode
%as in ref manual, p. 5-29
disp('Strike any key to arm the Keithley...')
pause
fprintf(keithley,'*RST');
fprintf(keithley,'SOUR:DELT:HIGH 0e-3');
fprintf(keithley,'SOUR:DELT:DEL %e',deltaDelay);
fprintf(keithley,'SOUR:DELT:COUN %s',num2str(numDeltaPoints));
fprintf(keithley,'TRAC:POIN %s',num2str(numDeltaPoints));
fprintf(keithley,'SOUR:CURR:COMP 105'); 
%initialize values to be collected
Cur = Curmax;
Volts = 0;
newData = [0,0];
if ishold==0;
    hold
end
%repeat for all points
	while Cur>Curmin  
		fprintf(keithley,'TRAC:CLE');
		fprintf(keithley,'SOUR:DELT:LOW %s', num2str(-1.0*Cur));
		fprintf(keithley,'SOUR:DELT:ARM');
		pause(max([0.01 numDeltaPoints*(8/10000)])); %it takes 8 seconds to arm the keithley with a 10,000 point buffer
		pause(2);
		disp('...Keithley is armed.');
		disp(' ');
        %keithley 
        fprintf(keithley,'INIT:IMM');
        pause(numDeltaPoints*deltaDelay + 0.1);
		fprintf(keithley,'TRAC:DATA?');
        fprintf(keithley,'CALC2:FORM MEAN');
        fprintf(keithley,'CALC2:STAT ON');
        fprintf(keithley,'CALC2:IMM');
        fprintf(keithley,'CALC2:DATA?');
        Volts = fscanf(keithley,'%e');
        
        newData = [Cur,Volts];
        data = [data; newData];
        plot(Cur,Volts);
		fprintf(keithley,'SOURC:SWE:ABOR');
		pause(1);
        pause(numDeltaPoints*deltaDelay + 0.1);
		Cur=Cur-Curinc;
	end
%eliminate leading zero row in data matrix
data = data(2:end,:);

%plot resistance data over temperature
plot(data(:,2),data(:,1),'*');

%save data file to folder as tabs-delimited text file
cd 'C:\Documents and Settings\Student\My Documents\MATLAB\supercond\data';
%fname = ['sc' datestr(now,30) '.txt'];
%fname = [fname '.txt'];
%dlmwrite (fname,data,'-append','delimiter','\t','precision',10,'newline','pc');

%close instrument objects and delete them
fclose(lakeshore);
fclose(keithley);
delete(instrfind);

disp('supercond.m was successfully closed.')

end