MATLAB Answers

Darryl
0

fscanf question with Arduino

Asked by Darryl
on 24 Feb 2013
Latest activity Answered by Kristian Cvetkov on 12 Nov 2017
I need to know how I can control the amount of Serial.println(variable_1) methods that get read during the fscanf() execution.
This is the code:
arduino = serial('COM4');
fopen(arduino);
x = 1:1:60;
for i=1:length(x)
y(i) = fscanf(arduino, '%d');
end
fclose(arduino);
% Plot data from arduino
plot(x,y)
If my Arduino sketch has multiple Serial.println() it seems to combine the variables within the Serial statements in the same vector. Is it possible for them to be separated?

  0 Comments

Sign in to comment.

2 Answers

Answer by Walter Roberson
on 24 Feb 2013
 Accepted Answer

fscanf(arduino, '%d', 1);

  3 Comments

Darryl
on 25 Feb 2013
Thank you for your help.
Say if I did have 2 serial print statements in my arduino sketch, would it be possible to skip the first one that I asked about prior and read a second output. Or even better be able to choose between the two statements as I could write some code like this:
for i=1:length(x)
y(i) = fscanf(arduino, '%d', % read serial print 1);
z(i) = fscanf(arduino, '%d', % read serial print 2 );
end
Then I could plot a 3d graph for my dht11 temperature and humidity sensor, and it would save me uploading separated sketches for the arduino.
y(i) = fscanf(arduino, '%d', 1);
z(i) = fscanf(arduino, '%d', 1);
if you want to read both of them. Or, more efficiently,
t = fscanf(arduino '%d%d', 1);
y(i) = t(1);
z(i) = t(2);
Darryl
on 25 Feb 2013
Thank you very much indeed. Much appreciated, will try that tomorrow.

Sign in to comment.


Answer by Kristian Cvetkov on 12 Nov 2017

How I can write data on my Arduino, after I read them in Matlab?

  0 Comments

Sign in to comment.