Plotting force sensor data from Arduino in matlab
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi,
I'm trying to plot live data from a force sensor resistor in matlab using an arduino. I'm using the code I found on mathworks, but I cannot figure out how to adapt it correctly for it to read voltage data.
I need the command a=arduino; b = readVoltage(a,'A0');
But than I'm struggling with the fread function, since the fread function should than read the voltage, but this gives me the error 'In an assignment A(:) = B, the number of elements in A and B must be the same'
when I put data(i) = fread(b);
s = serial('COM3');
s.InputBufferSize = 1; % read only one byte every time
try
fopen(s);
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Arduino is connected.');
end
%% Create a figure window to monitor the live data
Tmax = 1000; % Total time for data collection (s) figure,
grid on,
xlabel ('Time (s)'), ylabel('Voltage'),
axis([0 Tmax+1 0 200]),
%% Read and plot the data from Arduino
Ts = 1; % Sampling time (s)
i = 0;
data = 0;
t = 0;
tic % Start timer
while toc <= Tmax
i = i + 1;
%%Read buffer data
data(i) = fread(s);
%%Read time stamp
% If reading faster than sampling rate, force sampling time.
% If reading slower than sampling rate, nothing can be done. Consider decreasing the set sampling time Ts
t(i) = toc;
if i > 1
T = toc - t(i-1);
while T < Ts
T = toc - t(i-1);
end
end
t(i) = toc;
%%Plot live data
if i > 1
line([t(i-1) t(i)],[data(i-1) data(i)])
drawnow
end
end
fclose(s);
I hope my question is clear. Thanks in advance
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!