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.
