Hello all,
In this mini project, I would like to read data coming from an Arduino Leonardo board into my Matlab program and display them in the command window continuously as long as the Arduino board does not turn off. The data is an analog voltage from a potentiometer which connects to the Arduino board. The code in Arduino is just a simple code for Analog read.( The code is from Arduino code library).
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
My problem is that my matlab code can display the data in real-time manner but it has a limit which is defined as number of points (50000) in the code. After the limit is reached, the code stops running. Is there any way to change this code to make the readout in really continuous manner. For example, the code will read and display the data from Arduino as soon as the Arduino board is on. Thank you.
My matlab code is posted below.
function arduinoMat1
%make sure no old serial ports are open
clear
delete(instrfindall);
%connect to serial port and set baud rate
s1 = serial('COM8','BaudRate',9600);
% open connection and send identification (to initalize it or something???)
fopen(s1);
fprintf(s1, '*IDN?');
get(s1)
%number of points to be plotted
numberOfPoints = 50000;
%initialize with zeros, to hold yvalues
yVal(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) = str2double(fgets(s1));
yVal(index);
%every 100 points, plot the entire graph
% this might be a bad way to do it, but at least it's "real-time"
if mod(index,100) == 0
set(thePlot,'YData',yVal, 'XData', xVal);
drawnow
end
end
hold off
% %close the port and delete it
fclose(s1);
delete(s1)
clear('s1')
delete(instrfindall)