How to read multiple sensors through the serial.

30 ビュー (過去 30 日間)
Kasper
Kasper 2014 年 3 月 13 日
コメント済み: Rehan Qasim 2020 年 8 月 20 日
Hi guys
I hope you can help me.
I have connected 4 force sensors to an Arduino, and I´m sending the data to the serial port simultanious – no problems so far.
Now I would like Matlab to read the serial and plot 4 graphs, 1 from each sensor.
The data in the serial are organized in 8 coloums with a description and the value for each sensor.
My problem is, that I dont know how to seperate the serial so each of the four dataset is placed in its own variable so I afterwards can make the plots.
This is my code:
clear all
clc
arduino=serial('COM4','BaudRate',9600);
fopen(arduino);
x=linspace(1,100);
for i=1:length(x);
y(i)=fscanf(arduino,'%f');
end
fclose(arduino)
This very basic code only reads the first "coloum" in the serial - how do I add the other coloum, so I can get that data as well?
I appreciate any help :)
Best regards Kasper
  1 件のコメント
Nazrin Zaini
Nazrin Zaini 2014 年 11 月 16 日
have you found the solution for this problem?i really like to know what is the solution.do you mind to share with me? really glad if you can share it :)

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2014 年 3 月 13 日
numcols = 8;
y = zeros(length(x), numcols);
for k = 1 : numcols
for i=1:length(x);
y(i, k)=fscanf(arduino,'%f');
end
end
plot(y);
Note: this depends upon all of the data for one sensor being consecutive on the serial port, but the way you describe it you have one data value for each sensor before proceeding to the next data value for the first sensor, and so on. I think it should probably be more like,
numcols = 8;
y = zeros(length(x), numcols);
fmt = repmat('%f', 1, numcols);
for i=1:length(x);
y(i, :) = fscanf(arduino, fmt);
end
plot(y);
  1 件のコメント
Kasper
Kasper 2014 年 3 月 13 日
編集済み: Walter Roberson 2014 年 3 月 16 日
Hi Walter and thanks for your quick reply.
I have tried both. And now I get 8 coloums, but only the first coloum is being filled with sensor data from the first sensor, so I need the remaining 3 sensors.
When I look at the serial in my Arduino, it looks like this:
Heel 0.0000 Metatars 1 0.5258 Svang 0.4290 Calcanus 0.0000
Heel 0.0258 Metatars 1 1.3258 Svang 0.8871 Calcanus 0.0774
Heel 0.1032 Metatars 1 1.5677 Svang 1.2226 Calcanus 0.0677
Heel 0.1710 Metatars 1 1.4452 Svang 1.0355 Calcanus 0.0871
Heel 0.6484 Metatars 1 1.3161 Svang 1.0677 Calcanus 0.1452
Heel 1.2548 Metatars 1 1.1677 Svang 1.0839 Calcanus 0.0968
I would like to read coloum 2,4,6 and 8, and put them in four variables so I later can plot them with time x.
Any ideas?
Best regards Kasper

サインインしてコメントする。

その他の回答 (2 件)

Kasper
Kasper 2014 年 3 月 15 日
編集済み: Walter Roberson 2014 年 3 月 16 日
Finally I have figured out the code. If anybody should find this thread, and have the same problem, here is the code which worked:
clear all
clc
arduino=serial('COM4','BaudRate',9600);
fopen(arduino);
x=linspace(1,100); % der optages 100 samples
numcols = 1; %der laves en kollonne med data
y = {}; %alt data smides i y
for i=1:length(x);
data =fscanf(arduino,'%f'); % '%f' gør at det er decimaler
IncomingString = char(data); % den indkomne data laves om til karaterer
IncomingString = regexp(IncomingString, '\s*', 'split'); %dataen splittes op med mellemrummer og i kolonner.
Heel(i,1)= IncomingString(1,3); %#ok<SAGROW> % jeg har suppressed en fejl %læser i kolonne 3
Metatars(i,1)= IncomingString(1,5); %læser i kolonne 5
Svang(i,1)= IncomingString(1,7);
Calcaneus(i,1)= IncomingString(1,9);
end
if true
% code
end
fclose(arduino);
PlotHeel = str2double(Heel); %Omdanner data som er på cell array til double data
figure(1)
plot(PlotHeel);
title('Heel'); xlabel('Samples'); ylabel('Volt');
PlotMetatars = str2double(Metatars); %Omdanner data som er på cell array til double data
figure(2)
plot(PlotMetatars);
title('Metatars'); xlabel('Samples'); ylabel('Volt');
PlotSvang = str2double(Svang); %Omdanner data som er på cell array til double data
figure(3)
plot(PlotSvang);
title('Svang'); xlabel('Samples'); ylabel('Volt');
PlotCalcaneus = str2double(Calcaneus); %Omdanner data som er på cell array til double data
figure(4)
plot(PlotCalcaneus);
title('Calcaneus'); xlabel('Samples'); ylabel('Volt');
%Written by Kasper Stentz
  2 件のコメント
Nazrin Zaini
Nazrin Zaini 2014 年 11 月 16 日
how about the arduino code, i am working out using your matlab code and try to figure out how to make columns for each sensor data in arduino, but not succesfull.
chfakht chfakht
chfakht chfakht 2015 年 2 月 6 日
i'm getting this error : Warning: Unsuccessful read: Matching failure in format. Index exceeds matrix dimensions.
Error in Serial2 (line 12) Heel(i,1)= IncomingString(1,3); %#ok<SAGROW> % jeg har suppressed en fejl %læser i kolonne 3

サインインしてコメントする。


Alvaro Anillo
Alvaro Anillo 2016 年 4 月 11 日
編集済み: Alvaro Anillo 2016 年 4 月 11 日
It would be great to see how works the arduino code because I don't see how it is sended the data, with the original code I can handle the inferface between serial port and matlab.
I need to read just two variables from my arduino code to plot time vs voltage so must be very easy but I'm just stopped
void loop(){
//read
Serial.println(analogRead(A0));
Serial.println(micros());
}
Thanks in advance
  2 件のコメント
Andhika Nagami
Andhika Nagami 2020 年 8 月 3 日
did you find the arduino code?
Rehan Qasim
Rehan Qasim 2020 年 8 月 20 日
??

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB Support Package for Arduino Hardware についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by