I have a problem with plotting the data read from the serial port from an Arduino uno board.
Show older comments
Hi Everyone, I am trying to read the data printed from an Arduino board with MATLAB serial read and I have problem when I plot it real time. After some time, the data becomes delayed and it has lagged response to my input. Actually, there is a motor and its current is being read. Please take a look at my code and see if you can advise on why this happens. Also, I would like to know how to plot the data versus time. Thank you. Sina.
delete(instrfind({'Port'},{'COM4'}));
clear all;
s1=serial('COM4','Baudrate',115200);
set(s1,'DataBits',8);
set(s1,'StopBits',1);
set(s1,'Parity','none');
fopen(s1);
x=[0];
SS=[1];
str='';
j=1;
tic;
time=0;
while(j<10000);
str=fgetl(s1);
ss=str2num(str);
while isempty(ss);
sss=fgetl(s1);
ss=str2num(sss);
end
if j<=1;
SS(j)=ss;
x(1)=1;
elseif j>1;
SS(j)=mean([SS(j-1);ss]);
x(j)=toc;
end
if isreal(ss);
% ts = timeseries(SS(1:j));
if (exist('last'))
plot([x(j),get(last,'xdata')],[SS(j),get(last,'ydata')]);
end
last=plot(x(j), SS(j), '.-');
% plot(time, SS(j)/1000, '.-')
hold on
% siz=size(x);
% plot(x(j), siz(2), '.-r')
axis([toc-10 toc+10 0 4000]);
end
drawnow;
hold on
grid on
j=j+1;
pause(0.00001);
end
ElapsedTime=toc;
delete(instrfind({'Port'},{'COM4'}));
% fclose(s)
% delete(s)
clear s1
Accepted Answer
More Answers (0)
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!