MATLAB-Arduino real time communication read/write interference
Show older comments
I'm playing around with matlab-arduino (in this case a teensy) serial communication, and I'm tryinng to get the arduino to read in a stream of potentiometer values, have it send the value to matlab, have matlab echo the value back to the arduino, and have the arduino set the brightness of an led accordingly. The main goal of this is check the speed of serial communication and how well they can continuously communicate with each other.
I ran into an interesting bug where I'm expecting incoming serial data, but calling fprintf every loop allows the serial data to be detected for a few initial loops but go undetected persistently afterwards. However, the serial data manages to be detected (as expected) when fprintf is not called.
Here is my code for matlab:
close all;
a1 = animatedline('Color',[0 .7 .7]);
% to interface with "teensytest" adruino code
arduino=serial('COM21','BaudRate',57600); % create serial communication object
%arduino.Timeout=60;
fopen(arduino); % initiate arduino communication
arduino.ReadAsyncMode = 'continuous';
tic;
out = 0;
x = 0;
y = 0;
it = 0;
while (1)
% Read arduino data and plot
if (arduino.BytesAvailable())
out = str2num(fscanf(arduino));
if (length(out))
out = out(1);
if (out>255)
out = 255;
end
x = toc;
y = out;
axis([x-5, x, -5, 255]);
addpoints(a1, x, y);
end
else
% no serial data detected, plots error value
x = toc;
y = -5;
axis([x-5, x, -5, 255]);
addpoints(a1, x, y);
end
drawnow limitrate
% Echo values back
if (strcmp(arduino.transferStatus, 'idle'))
fprintf(arduino, '%i\n', out, 'async');
end
% fprintf(arduino, '%i\n', out);
it = it + 1;
end
fclose(arduino);
and for arduino:
// To interface with matlab "matlab_teensy_demo
int led_pin = 3;
int read_pin = A0;
int on = 0;
int val;
void setup()
{
Serial.begin(57600);
pinMode(led_pin, OUTPUT);
time = millis();
}
void loop()
{
// read potentiometer, send to matlab
val = map(analogRead(read_pin), 0, 1023, 0, 255);
Serial.println(val);
delay(10);
// Read serial from matlab
if (Serial.available() > 0)
{
on = (int) Serial.readStringUntil('\n').toInt();
}
// Set LED lights
analogWrite(led_pin, on);
}
Here are the cases that I've tried with some confusing results
- Removing the fprintf statement in matlab allows matlab to read the potentiometer values as expected, so I don't believe the error is on the Arduino side
- Uncommenting the it = it+1 (or otherwise having something print directly to the matlab terminal) allows the read and write to work as expected, but now there is a stream of values being printed that shouldn't need to occue
- Moving the fprintf within the <if (arduino.BytesAvailable())> block allows the read and write to work as expected, but prevents me from writing values to serial when there is not incoming serial data. This writing is not necessary in this demo, but I would like to have this option in future projects
- Changing read/write modes betwen synchronous/asynchonous does not seem to help
Can someone explain what is going on?
Answers (0)
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!