Hello there, I want to read 4 sensor values into arrays and do DSP on them in MATLAB. I've already read the related issues in this platform.
- https://www.mathworks.com/matlabcentral/answers/121378-how-to-read-multiple-sensors-through-the-serial
- https://www.mathworks.com/matlabcentral/answers/197121-how-to-read-multiple-sensors-through-the-serial
- https://www.mathworks.com/matlabcentral/answers/141392-serial-communication-with-arduino-error-index-exceeds-matrix-dimensions?s_tid=answers_rc1-1_p1_BOTH
I suppose my arduino code runs perfectly, but MATLAB throws an error saying "Index in position 2 exceeds array bounds (must not exceed 1)." at the second sensor reading line. Could you please help ? Thx 😉
% MATLAB code is as follows
clear allclcdelete(instrfind)arduino=serial('COM9');set(arduino,'BaudRate',38400);fopen(arduino);%Handshake
fprintf(arduino,'R'); % Send Read Request to Arduino
x=linspace(1,500); % 500 samples are recorded
y = {}; % all data is thrown in y.
for i=1:length(x) data =fscanf(arduino,'%d'); % '%d' get ints
y = char(data); % The incoming data is converted to characters
y = regexp(y, '\s*', 'split'); % The data is split up into spaces and in columns.
% Data format: data1-tab-data2-tab-data3-tab-data4
reading1(i,1)= y(1,1); % reads in column 1
reading2(i,1)= y(1,3); % reads in column 2
reading3(i,1)= y(1,5); % reads in column 3
reading4(i,1)= y(1,7); % reads in column 4
end% Done reading !
fclose(arduino);delete(instrfind); reading1plot = str2double(reading1); % Converts data that is on cell array to double data
figure(1)plot(reading1plot);title('sensor 1'); xlabel('Sample Index'); ylabel('Analog Reading1');reading2plot = str2double(reading2); % Converts data that is on cell array to double datafigure(2)plot(reading2plot);title('sensor 2'); xlabel('Sample Index'); ylabel('Analog Reading2');reading3plot = str2double(reading3); % Converts data that is on cell array to double datafigure(3)plot(reading3plot);title('sensor 3'); xlabel('Sample Index'); ylabel('Analog Reading3');reading4plot = str2double(reading4); % Converts data that is on cell array to double datafigure(4)plot(reading4plot);title('sensor 4'); xlabel('Sample Index'); ylabel('Analog Reading4');end
Arduino Code:
int flag= 0;int ctr = 0;int ReadReq=0;int reading1;int reading2;int reading3;int reading4;void setup() { Serial.begin(38400); cli();//stop interrupts// Sampling rate is 1000 Hz.//set timer1 interrupt at 1000 HzTCCR1A = 0;// set entire TCCR1A register to 0TCCR1B = 0;// same for TCCR1BTCNT1 = 0;//initialize counter value to 0// set timer count for 100Hz incrementsOCR1A = 1999;// = (16*10^6) / (1000*8) - 1//had to use 16 bit timer1 for this bc 1999>255, but could switch to timers 0 or 2 with larger prescaler// turn on CTC modeTCCR1B |= (1 << WGM12);// Set CS11 bit for 8 prescalerTCCR1B |= (1 << CS11); // enable timer compare interruptTIMSK1 |= (1 << OCIE1A); sei();//allow interrupts }void loop(){ if(Serial.available()>0) { // Check if any data has been sent by the PC ReadReq = Serial.read(); if((ReadReq=='R')&&(flag == 0)){ ctr = 0; flag= 1; } }}ISR(TIMER1_COMPA_vect) { // FREQ 1000 Hz. if(flag == 1) { // takes 500 samples and send it to the MATLAB reading1 = analogRead(A0); reading2 = analogRead(A1); reading3 = analogRead(A2); reading4 = analogRead(A3); Serial.print(reading1); Serial.print("\t"); Serial.print(reading2); Serial.print("\t"); Serial.print(reading3); Serial.print("\t"); Serial.print(reading4); Serial.print("\n"); ctr = ctr + 1; if(ctr == 499) { flag = 0; } }};
Best Answer