The serial port on my Arduino Mega is receiving 3 floats as bytes (total of 12 bytes, 4 per float) over serial. Unfortunately I cannot assemble the 4 Bytes in a float, as the serial monitor only displays
0.00
0.00
0.00
etc.
Here is the code:
void serialEvent2() {
if (Serial2.available() > 11) {
byte yawData[3];
yawData[0] = Serial2.read();
yawData[1] = Serial2.read();
yawData[2] = Serial2.read();
yawData[3] = Serial2.read();
float yawAngle = *((float*)(yawData));
Serial.println(yawAngle);
byte pitchData[3];
pitchData[0] = Serial2.read();
pitchData[1] = Serial2.read();
pitchData[2] = Serial2.read();
pitchData[3] = Serial2.read();
float pitchAngle = *((float*)(pitchData));
byte rollData[3];
rollData[0] = Serial2.read();
rollData[1] = Serial2.read();
rollData[2] = Serial2.read();
rollData[3] = Serial2.read();
float rollAngle = *((float*)(rollData));
}
}
I made sure the data coming to Serial2 is not 0
.
UPDATE: The initial mistake was my byte array not being sized correctly. I fixed it. The values I print are fine until after about a second, they get totally weird. Dropped bits/Bytes maybe? Seems odd because a sample sketch written in Processing works perfectly while taking the exact same input.