function [data] = diode3(sampleCurrl,sampleCurrh,CurrIncrement,fname)

%Collects and logs data for the monopolar diode experiment.
%
%Input:
%       
%       Npts: See notes for tempFollow.
%       sampleCurr: The magnitude of the current in amperes sent through
%           the sample in delta mode.  This program uses delta mode
%           exclusively, which provides the sample with a square wave 
%           current to eliminate effects of leads with finite temperature.
%       dvmRange: Sets the measurement range of the digital voltmeter in
%           volts.  Care must be taken to keep all data points in range, 
%           otherwise the device will return an error and the data may not 
%           be logged.
%       ptInterval: The time interval in seconds to wait before collecting
%           the next data point.  The time between successive points will 
%           always be slightly longer, as the time for receiving data from
%           the device is not counted in this number.
%
%Suggested input values:
%       sampleCurrl: 0.0
%       sampleCurrh: 0.1
%       CurrIncrement: 0.001
%       dvmRange: 10
%       ptInterval: 0.1
%
%Output:
%       data: A 3-column matrix.  First column is chip temperature, second
%           is the current supplied, third is the voltage read.
%
%If the program exits with errors, call delete(instrfind) at the command
%prompt to reset the hardware connections.
%
%Modified by Ben Nachumi, peripatetic1@yahoo.com, February, 2012

%set parameters  and initialize some things
compliance = 105; %max out compliance--nothing to harm here
ptInterval = 0.1;
dvmrange = 10;
pause on
data = [0 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(' ')

%Initialize the current source and the voltmeter
fprintf(keithley,'*RST');
fprintf(keithley,'SYST:COMM:SER:SEND "VOLT:RANG %s"',num2str(dvmRange));
fprintf(keithley,'SOUR:CURR:COMP %e', compliance);
fprintf(keithley,'SOUR:CURR:RANG:AUTO ON');
disp('Strike any key to start the loop...')
pause


if ishold == 0
hold;
end

%initialize the data
Temp = 0;
Voltage = 0;
newData = [0 0 0];
sampleCurr=sampleCurrl;

while sampleCurr < sampleCurrh

%set the current output
fprintf(keithley,'SOUR:CURR %e',sampleCurr);
%query for and read the temperature
fprintf(lakeshore, 'KRDG?a');
Temp = fscanf(lakeshore,'%f');
%fire the keithley
fprintf(keithley,'OUTP ON');
% query for and read voltage 
fprintf(keithley,'SYST:COMM:SER:SEND "SENS:DATA:FRES?"');
Voltage = fscanf(keithley,'%e');
%turn off the current
fprintf(keithley,'OUTP OFF');
%update the data displays
newData = [Temp, sampleCurr, Voltage];
data = [data; newData];
plot(Voltage,sampleCurr,'*');
%update current
sampleCurr = sampleCurr+CurrIncrement;
pause(pointinterval);
end

%eliminate leading zero row in data matrix
data = data(2:end,:);

%disarm keithley
fprintf(keithley,'SOUR:CLE');

%plot resistance data over temperature
plot(data(:,3), data(:,2),'d');

%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('diode3.m was successfully closed.')

end