Arduino Matlab serial communication issue

Hi guys....here I'm, needing your support!

I have some issues in a simple Arduino - Matlab (both 2014 and 2016) serial communication. I have a simple Arduino sketch that collects values from a sensor and send them via serial. Arduino waits for a character 'r' for starting the reading/sending procedure

void loop()
{    
  if(Serial.available()) 
  {
    cmd = Serial.read();
    if(cmd == 'r') 
    {
      while(1)
      {
        accelgyro.read();
        //acc
        raw_values[0] = accelgyro.a.x;
        raw_values[1] = accelgyro.a.y;
        raw_values[2] = accelgyro.a.z;    

        //gyro
        raw_values[3] = accelgyro.g.x;
        raw_values[4] = accelgyro.g.y;
        raw_values[5] = accelgyro.g.z;

        for (j=0; j<6; j++)
        {
            Serial.write (highByte(raw_values[j]));
            Serial.write (lowByte(raw_values[j]));
        }     
       delay(2);
      }
    }
  }
}

And the correspondent Matlab code:

Arduino = serial('COM6','BaudRate',115200);
fopen(Arduino);
flushinput(Arduino)

acqSize = 1000;

pause(2)

'start'
fwrite(Arduino,'r');
tStart = tic;

while( i <=acqSize)

    if(Arduino.BytesAvailable>packetSize-1)

        lastData = fread(Arduino,packetSize) ;
        raw_matrix(:,i) =  byteToInt(lastData);
        raw_matrix(7,i) = toc(tStart);
        tStart = tic;
        i=i+1
    end
    pause(0.001);
end

where packetsize is number of bytes sent per cycle from Arduino, i.e., 12

The problem is that the speed is really low, I checked the time between two reading and what I obtain is depicted in the attached pic enter image description here

I have a good speed except for these spikes that periodically occur. In these cases the interval between two readings is greater than 0.1 s.

I don't understand why you are using delay() in the Arduino code, and pause() in the matlab code, and complaining about slow speeds. Quit using delay/pause, or quit complaining.

I tried to put a delay in arduino code to slow down the speed to 500 hz (approx.). 2 milliseconds worth nothing with respect to 0.1 seconds of delay in the communication. The same for 0.001 seconds in Matlab. It is good practice to put a sleep in an infinte while loop.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

You should also post your complete Arduino program if you want useful advice.

...R

here the entire code

//Chips libraries
#include <LSM6.h>

//I2C libraries
#include <Wire.h>

int raw_values[7];
char cmd;
long int now, lastUpdate;

LSM6 accelgyro;

void setup() { 
  Serial.begin(115200);
  Wire.begin();
}


void loop()
{    
  if(Serial.available()) 
  {
    cmd = Serial.read();
    if(cmd == 'r') 
    {
      while(1)
      {
        accelgyro.read();
        //acc
        raw_values[0] = accelgyro.a.x;
        raw_values[1] = accelgyro.a.y;
        raw_values[2] = accelgyro.a.z;    

        //gyro
        raw_values[3] = accelgyro.g.x;
        raw_values[4] = accelgyro.g.y;
        raw_values[5] = accelgyro.g.z;

        for (j=0; j<6; j++)
        {
            Serial.write (highByte(raw_values[j]));
            Serial.write (lowByte(raw_values[j]));
        }     
       delay(2);
      }
    }
  }
}

It is good practice to put a sleep in an infinte while loop.

Nonsense.

Pete

liso:
It is good practice to put a sleep in an infinte while loop.

I had not noticed that.

It is also nonsense to have an infinite WHILE loop. That is what the loop() function is for.

...R

It is good practice to put a sleep in an infinte while loop.

referred to the Matlab side! In the arduino code the while starts after a 'r' character is received

referred to the Matlab side

Still nonsense unless you qualify your "good practice" by restricting it to a specific set of circumstances.

Pete