Real-time graph with multiple data from Arduino
5 views (last 30 days)
Show older comments
Hi,
I am streaming data from an Arduino to the USB and plotting it in Matlab in real time. It works fine with a single stream of numbers but I can't get it to work with 3 sets of data.
I am currently streaming eg
3
6
2
9
6
etc
but when I go to eg
5 4 3
2 4 1
8 3 5
7 6 4
etc and try to call out the 3 columns I can't get it to work. I can configure the Arduino data to stream any way suitable if need be.
Any advise welcome
Philip
current code;
(My code was adapted from http://lungfulcoding.blogspot.co.uk/2013/03/basic-arduino-and-matlab-setup.html)
%
make sure no old serial ports are open
clear
delete(instrfindall);
%connect to serial port and set baud rate
s1 = serial('com7','BaudRate', 9600);
% open connection and send identification (to initalize it or something???)
fopen(s1);
fprintf(s1, '*IDN?');
%number of points to be plotted
numberOfPoints = 200;
%initialize with zeros, to hold yvalues
%yVal0(1:numberOfPoints)= 0;
yVal1(1:numberOfPoints)= 0;
yVal2(1:numberOfPoints)= 0;
yVal3(1:numberOfPoints)= 0;
% x axis points
xVal = (1:numberOfPoints);
%create an empty plot
thePlot = plot(nan);
%delay or pause so serial data doesnt stutter
pause(1);
hold on
for index = (1:numberOfPoints)
%reading value from Arduino
yVal(index) = 50*str2double(fgets(s1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% this section is wrong at the moment, I am trying to split yval into 4
% and then plot n as channel 0, n+1 as channel 1, n+2 etc
n = 4;
yVal0 = yVal(1 : n : end); % => 1 5 9
yVal1 = yVal(2 : n : end); % => 2 6 10
yVal2 = yVal(3 : n : end); % => 3 7 11
yVal3 = yVal(4 : n : end); % => 4 8 12
%every 100 points, plot the entire graph
if mod(index,10) == 0
set(thePlot,'YData',yVal0, 'XData', xVal);
set(thePlot,'YData',yVal1, 'XData', xVal);
set(thePlot,'YData',yVal2, 'XData', xVal);
set(thePlot,'YData',yVal3, 'XData', xVal);
drawnow
end
end
hold off
%close the port and delete it
fclose(s1);
delete(s1)
clear('s1')
delete(instrfindall)
0 Comments
Answers (0)
See Also
Categories
Find more on MATLAB Support Package for Arduino Hardware 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!