On matlab i have incoming serial data from the Arduino. The arduino reads data from one sensor and prints it onto the serial monitor and MATLAB plots a graph with it. When i print data from 2 sensors (separated by a tab) matlab doesnt plot a graph. How am i supposed to parse them and plot them both on the graph in real time?
arduino code:
#include <ColorLCDShield.h>
#include <math.h>
#include <float.h>
char data;
int counter = 0;
const double ARDUINO_VOLTAGE = 5.0;
int tempPin= A3;
int voltPin = A2;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int temp= analogRead(tempPin);
int voltage = analogRead(voltPin);
Serial.print(temp);
Serial.print("\t");
Serial.println(voltage);
delay(500);
}
so data is shown like this on the serial monitor:
temp voltage
temp voltage
temp voltage
.......
matlab code (only plots the temperature and not the voltage)
delete(instrfindall);
clear all; clc; close all;
% Initialize serial port
s = serial('COM5','BaudRate',9600);
set(s,'BaudRate', 9600);
set(s,'DataBits', 8);
set(s,'StopBits', 1);
fopen(s);
s.ReadAsyncMode = 'continuous';
% variables
numberOfData = 500;
data = zeros(1, numberOfData);
i = 1;
% Main graph figure
figure(1);
hold on;
grid on;
title('Incoming Data from External Device');
xlabel('Time');
ylabel('temp');
% Start asynchronous reading
readasync(s);
while(i<=numberOfData)
% Get the data from the serial object
data(i,:) = fscanf(s, '%f %f', 2);
voltage = data(i,1);
temperature = data(i,1);
% Plot the data
plot(i, temperature, '*r', i, voltage, '--g');
% Draw and flush
drawnow;
%Increment the counter
i=i+1;
end
% Give the external device some time…
pause(3000);
return;
catch
% Some of these crash the program – it depends. The serial port is left
% open, which is not good.
stopasync(s);
fclose(s); % bad
delete(s);
clear s;
fprintf(1, 'Sorry, you"re going to have to close out of Matlab to close the serial port\n');
return
end