Matlab not reading data fast enough from arduino.
My code is able to read data from the serial port correctly (it seems), however for some reason my dataIndex variable is not keeping up with the speed that I am trying to read data at (I think). While I should have 10000 data points using this code my dataIndex variable is ending at way short at about 816 instead. Has anybody else ever had an issue like this?
Another but not as important issue I have been having is this occassional error code:
Warning: Matching failure in format. The format specified for conversion, ‘%d’, is incorrect.
Unable to perform assignment because the left and right sides have a different number of elements.
It only comes up sometimes, and it seems like its because my value variable occasionally becomes a char.
Any advice that could be offered on any of this would be greatly appreciated!
%% Matlab code
clc;
clear;
close all;
% % Closes serial ports
fclose(instrfind());
% % Defines serial connection
arduino = serial(‘COM11′,’BaudRate’,9600);
% % Initializes serial connection
fopen(arduino);
% % Duration to record data(in seconds).
duration = 10;
% % Time interval between consecutive data reads.
stepTime = .001;
% % Total number of data samples to be recorded.
samples = duration/stepTime;
% % Initialize arrays for storing data and timestamps.
valueArray = zeros(1,samples);
dataIndex = 1;
tObj = tic;
while(toc(tObj) <= duration)
% % Gets data value from serial port
value = fscanf(arduino,’%d’);
% % Store the read data in the corresponding data array.
valueArray(dataIndex) = value;
dataIndex = dataIndex + 1;
% % Read data from Arduino pins in intervals specified by the variable ‘stepTime’.
pause(stepTime);
end
% % Closes serial ports
fclose(instrfind());
%% Arduino code
const int analogInPin = A0;
int sensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.println(sensorValue);
delay(10);
}My code is able to read data from the serial port correctly (it seems), however for some reason my dataIndex variable is not keeping up with the speed that I am trying to read data at (I think). While I should have 10000 data points using this code my dataIndex variable is ending at way short at about 816 instead. Has anybody else ever had an issue like this?
Another but not as important issue I have been having is this occassional error code:
Warning: Matching failure in format. The format specified for conversion, ‘%d’, is incorrect.
Unable to perform assignment because the left and right sides have a different number of elements.
It only comes up sometimes, and it seems like its because my value variable occasionally becomes a char.
Any advice that could be offered on any of this would be greatly appreciated!
%% Matlab code
clc;
clear;
close all;
% % Closes serial ports
fclose(instrfind());
% % Defines serial connection
arduino = serial(‘COM11′,’BaudRate’,9600);
% % Initializes serial connection
fopen(arduino);
% % Duration to record data(in seconds).
duration = 10;
% % Time interval between consecutive data reads.
stepTime = .001;
% % Total number of data samples to be recorded.
samples = duration/stepTime;
% % Initialize arrays for storing data and timestamps.
valueArray = zeros(1,samples);
dataIndex = 1;
tObj = tic;
while(toc(tObj) <= duration)
% % Gets data value from serial port
value = fscanf(arduino,’%d’);
% % Store the read data in the corresponding data array.
valueArray(dataIndex) = value;
dataIndex = dataIndex + 1;
% % Read data from Arduino pins in intervals specified by the variable ‘stepTime’.
pause(stepTime);
end
% % Closes serial ports
fclose(instrfind());
%% Arduino code
const int analogInPin = A0;
int sensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.println(sensorValue);
delay(10);
} My code is able to read data from the serial port correctly (it seems), however for some reason my dataIndex variable is not keeping up with the speed that I am trying to read data at (I think). While I should have 10000 data points using this code my dataIndex variable is ending at way short at about 816 instead. Has anybody else ever had an issue like this?
Another but not as important issue I have been having is this occassional error code:
Warning: Matching failure in format. The format specified for conversion, ‘%d’, is incorrect.
Unable to perform assignment because the left and right sides have a different number of elements.
It only comes up sometimes, and it seems like its because my value variable occasionally becomes a char.
Any advice that could be offered on any of this would be greatly appreciated!
%% Matlab code
clc;
clear;
close all;
% % Closes serial ports
fclose(instrfind());
% % Defines serial connection
arduino = serial(‘COM11′,’BaudRate’,9600);
% % Initializes serial connection
fopen(arduino);
% % Duration to record data(in seconds).
duration = 10;
% % Time interval between consecutive data reads.
stepTime = .001;
% % Total number of data samples to be recorded.
samples = duration/stepTime;
% % Initialize arrays for storing data and timestamps.
valueArray = zeros(1,samples);
dataIndex = 1;
tObj = tic;
while(toc(tObj) <= duration)
% % Gets data value from serial port
value = fscanf(arduino,’%d’);
% % Store the read data in the corresponding data array.
valueArray(dataIndex) = value;
dataIndex = dataIndex + 1;
% % Read data from Arduino pins in intervals specified by the variable ‘stepTime’.
pause(stepTime);
end
% % Closes serial ports
fclose(instrfind());
%% Arduino code
const int analogInPin = A0;
int sensorValue = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.println(sensorValue);
delay(10);
} arduino, serial, char MATLAB Answers — New Questions