You are on page 1of 2

clc; clear all; close all;

%clean everything up

a=arduino('com3') % start communication with the daq board


ai_pin=0;
% the pin from which we'll be reading
tic
% start the clock
i=0;
% counter for our data
setpoint = 30;
Kc = -50;% -34.8;
taui = 75; %286.65;
DC = 0;
a.analogWrite(3,DC);
tic
i = 1;
T = 0;
v = 0;
time = 0;
data = zeros(1,4);
err(1) = 7;
data(1,2) = temp((a.analogRead(ai_pin)).*(5/1023));
T(1) = data(1,2);
while toc<400
% take data for 300 seconds

i=i+1;
%increase our array index
time(i)=toc;
%measure elapsed time
v(i)=a.analogRead(ai_pin);
%measure voltage from the right pin
T(i) = temp(v(i).*(5/1023)); %Convert to celcius
err(i) = T(i) - setpoint;
data(i,1) = time(i);
data(i,2) = T(i);
data(i,3) = DC;
data(i,4) = err(i);
data(i,5) = setpoint;
deltat = data(i,1)-data((i-1),1);
DeltaDC = Kc.*(err(i)-err(i-1)+((deltat)/taui)*err(i));
DC = DC + DeltaDC;
DC = round(DC);
if DC > 255
DC = 255;
end
if DC < 0
DC = 0;
end
a.analogWrite(3,DC);
current(i) = DC;
%logs current
%figure(1);
%plot to figure
subplot(2,1,1);
plot(time,T,'-r');
%plot voltage as a function of time
grid on
xlabel('Time(s)');
ylabel('Temperature (deg C)');
figure(2);
subplot(2,1,2);
grid on

plot(time,data(:,3),'-r');
xlabel('Time (s)');
ylabel('PWM Value');
%subplot(3,1,3);
%plot(time,data(:,5),'-r');
%grid on
%xlabel('Time (s)');
%ylabel('Setpoint Temperature (deg C)');

pause(.25);
end

a.analogWrite(3,0);

%wait 1/4 of a sec and loop again

You might also like