How can I save serial monirtor values from arduino to multiple vectors?

7 visualizaciones (últimos 30 días)
Hi !
I have a project that uses Arduino Uno and Matlab to calculate Pulse Transit Time (PTT) between 2 sensors. I am trying to work things out with one sensor first. The idea is to use split function to save the data into two vectors, one for the time and the other for the voltage. but I keep getting this error:
"Error using strsplit (line 80)
First input must be either a character vector or a string scalar."
Also, I want to store the time value as 'long' type in the vector since I need the maximum accuracy for this project. Is there a way to do so? In my code I used str2double and it doesn't work.
My Arduino code:
int SwitchState=0;
int prevSwitchState=0;
long time;
void setup() {
Serial.begin(9600);
//pinMode(2,INPUT);
}
void loop() {
SwitchState = digitalRead(2);
if (SwitchState == HIGH ){
time = micros();
int sensorval = analogRead(A0);
Serial.print(sensorval);
Serial.print("\t");
Serial.print(time);
Serial.println();
}}
And this is my Matlab code:
clear all;
clc;
delete(instrfindall);
a = serial('COM3','BaudRate',9600);
fopen(a);
for i=1:1000 %while if constant acquisition is needed.
sSerialData = fscanf(a,'%d');
flushinput(a);
t = strsplit(sSerialData,'\t');
y(i) = str2double(t(1));
time(i) = str2double(t(2));
end
fclose(a);
plot(time,y);
  3 comentarios
Maysamf
Maysamf el 17 de Abr. de 2018
Thank you for your comment.
I have resolved this issue as you said by changing the data type.
To answer your question on why I am not using the package, I want to store the data in two arrays not only plot it. I want to process the data using some mathematical equations then send it back to the arduino as one value.
Thank you again.
Madhu Govindarajan
Madhu Govindarajan el 18 de Abr. de 2018
Gotcha, I am glad your issue is resolved :)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by