How to read multiple sensors through the serial.
조회 수: 26 (최근 30일)
이전 댓글 표시
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
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
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);
추가 답변 (2개)
Kasper
2014년 3월 15일
편집: Walter Roberson
2014년 3월 16일
댓글 수: 2
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
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!