Matlab doesn t want to receive negative values from Arduino

Hello everyone,
I have a project in Arduino in which I have to send the MPU pitch and roll data to Matlab through serial communication.
The issue that I have is that Matlab does not read negative values, I ve tried changing my code in Arduino but still didn t work.
Arduino:
void GyroPacket(int p,int r,int t, int tempp){
Serial.write(250);
Serial.write(4);
Serial.write(0);
Serial.write(15);
Serial.write(1);

Serial.write(highByte(p)); // Transmit high byte of pitch
Serial.write(lowByte(p)); // Transmit low byte of pitch
Serial.write(highByte(r)); // Transmit high byte of roll
Serial.write(lowByte(r)); // Transmit low byte of roll
// Serial.write(p);
// Serial.write(r);
Serial.write(t);
Serial.write(tempp);
}
Could anyone guide me?

Please post a schematic showing which controller You use and how it's connected.

I am using arduino mega2560 and its connected through an usb cable if that is want you meant?

Perhaps the Matlab program is incorrect.

this is the matlab code, works fine it just doesn t show the negative values:
serialPort = serialport("COM3",115200);

while true

data = read(serialPort,100,"uint8");

dataLength = length(data);

for j = 1:dataLength

   if data(j) == 250

      if dataLength > j + 9

       pitch = typecast(uint8([data(j+5), data(j+6)]), 'int16');
       roll = typecast(uint8([data(j+7), data(j+8)]), 'int16');
       size = data(j+1);
       destination = data(j+3);
      sensorbyte = data(j+4);
      payload = zeros(size,1);

      for jj = 1:size
          payload(jj) = data(j +4 + jj);
      end
      disp(payload);
      end
   end

end
end

That explanation helps. The Mega has several serial ports. "Serial" is the one used for downloading code as well as printing to the serial monitor in the IDE.
Use the Serial1 for COM equipment.

What does it show instead?

I will try like this, thank you!

it shows values such as 250, 240 when my pitch and roll are about -10 or -30

You need to have communications between Matlab and the Arduino working correctly, and the received data properly interpreted, before any measurements can be made.

Plenty of working examples are posted on line.

Assuming the above code converts two unsigned bytes to a 16 bit signed integer correctly, where are you displaying those converted values? The only bit of code I saw that appeared to be displaying anything was displaying the unconverted unsigned bytes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.