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?
PaulS:
A lot of what you need to do on the matlab side depends on what you do on the Arduino side - that is something you failed to disclose.
Thank you Paul for your reply. This is my Arduino code:
void setup()
{
// Start serial at 9600 baud
Serial.begin(9600);
}
void loop()
{
// Read the input on analog pin 0:
int sensorValue = analogRead(A0);
int sensorValue2 = analogRead(A1);
int sensorValue3 = analogRead(A2);
int sensorValue4 = analogRead(A3);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (3.3 / 1023.0);
float voltage2 = sensorValue2 * (3.3 / 1023.0);
float voltage3 = sensorValue3 * (3.3 / 1023.0);
float voltage4 = sensorValue4 * (3.3 / 1023.0);
Thanks for your reply.
The data i 3.3v/1023 values, depended on how much pressure their is applied to the sensor.
So data is like 0.02, 1.08, 0.00, and so on.
So it is related to matlab , not arduino ? At the arduino site , you have everything quite ok . BTW - when posting code , use code tags ( # icon just above the smileys )
Theoretically, You'd need to give each sensor data stream a unique id for matlab or any app that reads it. Like serial print something like
Sensor1=12.332&sensor2=575.988
Or use a special character for each sensor like
L12.3332b3443.788k333.33
And so on. So then from another app, you'd need to parse this.
waski:
So it is related to matlab , not arduino ? At the arduino site , you have everything quite ok . BTW - when posting code , use code tags ( # icon just above the smileys )
Thank you for your reply
I´ll remember that for next time.
No I dont think my Arduino code is wrong. When I check serial in Arduino the numbers are like, depending on how much pressure I put on the 4 sensors:
My problem is, that I cant read each coloum in Matlab. I would like to put coloum 2,4,6 and 8 in their individual variable, so that I can make four different graph to time x.
mistergreen:
Theoretically, You'd need to give each sensor data stream a unique id for matlab or any app that reads it. Like serial print something like
Sensor1=12.332&sensor2=575.988
Or use a special character for each sensor like
L12.3332b3443.788k333.33
And so on. So then from another app, you'd need to parse this.
Thanks you for your reply.
That makes sense.
But I dont understand if you just cant read the individual coloums like I showed in my previous answer?
Or is it just not possible?
you format the data for the computer to read easily, not necessarily you (human).
It's definitely possible. I can do it in various languages but have no idea how matlab works.
Oh, and 'listen' for a carriage return CR, for an end of serial.println. This will tell whatever app to stop listening and start parsing the serial string.
Thanks for the replies guys - I have figured the code out now, and if anybody should have the same problem, here is the code:
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
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, Denmark