I am trying to send a voltage read by an Arduino from a Waveform generator into matlab.
My trouble is sending a signal that is > 1 Hz. At 1 Hz or lower, I can send data from arduino using Serial.println() and read the data in Matlab using fgets, but this fails at higher frequencies.
I believe I need to modify my code to use Serial.write() in arduino and fread() in Matlab, but I am unsure how to do this to send a full unsigned int (2 bytes at a time).
My Arduino code is:
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(57600);
//establishContact(); // send a byte to establish contact until receiver responds
}
// 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):
byte voltage = sensorValue; //*(5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
//Serial.write(voltage);
}
My matlab code is:
%make sure no old serial ports are open
clear
delete(instrfind({'Port'},{'/dev/tty.usbmodem411'}));
%connect to serial port and set baud rate
%s1 = serial('/dev/tty.usbmodem441','BaudRate', 9600);
s1 = serial('/dev/tty.usbmodem411','BaudRate',57600,'Terminator','CR/LF');
% open connection and send identification
fopen(s1);
fprintf(s1, '*IDN?');
%number of points to be plotted
numberOfPoints = 10000;
%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)); %*1.1/1023.;
%yVal(index) = fread(s1,1,'int8');
%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)
How can I modify my code to send data each voltage readout as bytes and read the bytes correctly in Matlab?
I was able to use the following in Arduino:
hiByte = highByte(sensorValue); //*(5.0 / 1023.0);
loByte = lowByte(sensorValue);
Serial.write(hiByte);
Serial.write(loByte);
But I am having trouble getting Matlab to read the two corresponding bytes in the right order and frequency, and to reconstruct the actual sensorValue from the two bytes.
Currently I cannot read a 10Hz signal correctly. Eventually I would like to read in a 300Hz signal. Any ideas?
Thank you.