Skip to content
Snippets Groups Projects

Data Logging

Open kamako requested to merge patch-1 into master
1 file
+ 64
0
Compare changes
  • Side-by-side
  • Inline
Starx.m 0 → 100644
+ 64
0
%% Acquire and analyze data from a temperature sensor
%% Connect to Arduino
% Use the arduino command to connect to an Arduino device.
clear();
a = arduino;
filename=input('Please enter a filename \n','s')
figure
h = animatedline;
h.Color = 'red';
i=animatedline;
i.Color = 'blue';
ax = gca;
ax.YGrid = 'on';
ax.YLim = [0 5];
stop = false;
startTime = datetime('now');
while ~stop
stop = readDigitalPin(a,'D12');
end
writeDigitalPin(a,'D13',1)
pause(0.5)
stop=false;
while ~stop
% Read current voltage value
v0 = readVoltage(a,'A0');
v1 = readVoltage(a,'A1');
% Calculate temperature from voltage (based on data sheet)
% Get current time
t = datetime('now') - startTime;
% Add points to animation
addpoints(h,datenum(t),v0)
addpoints(i,datenum(t),v1)
% Update axes
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
% Check stop condition
stop = readDigitalPin(a,'D12');
end
writeDigitalPin(a,'D13',0)
%% Plot the recorded data
[timeLogs,EKG] = getpoints(h);
[throwaway, Pot] = getpoints(i);
timeSecs = (timeLogs-timeLogs(1))*24*3600;
% figure
% plot(timeSecs,tempLogs)
% xlabel('Elapsed time (sec)')
% ylabel('Temperature (\circF)')
%% Save results to a file
T = table(timeSecs',EKG',Pot','VariableNames',{'Time_sec','EKG','Pot'});
filename = strcat(filename,'.xlsx');
% Write table to file
writetable(T,filename)
% Print confirmation to command line
fprintf('Results table with %g temperature measurements saved to file %s\n',...
length(timeSecs),filename)
Loading