I found this code on another post and modified it slightly, so I could verify what the bytes should be after my SPI transfer.
Test Code:
void setup()
{
Serial.begin(9600);
// prints title with ending line break
Serial.println("Floats coming out as bytes!!");
// wait for the long string to be sent
delay(3000);
}
float number = 3.14;
union u_tag {
byte b[4];
float fval;
} u;
void loop()
{
u.fval = number;
Serial.print("Byte 1: ");
Serial.println(u.b[0], DEC );
Serial.print("Byte 2: ");
Serial.println(u.b[1], DEC );
Serial.print("Byte 3: ");
Serial.println(u.b[2], DEC );
Serial.print("Byte 4: ");
Serial.println(u.b[3], DEC );
Serial.print("velocity: ");
Serial.println(u.fval);
Serial.println();
delay(3000); // allow some time for the Serial data to be sent
}
Here are the results that I get from running the above code.
Floats coming out as bytes!!
Byte 1: 195
Byte 2: 245
Byte 3: 72
Byte 4: 64
velocity: 3.14
I think my approach should work, but I must be transferring the bytes from the slave incorrectly. I mentioned earlier that I tried changing the bit order, and it didn't seem to correct my issue. The way I tested this wasto change the bit order form this SPI.setBitOrder(MSBFIRST); // MSB first bit order to this SPI.setBitOrder(LSBFIRST); // LSB first bit order. Changing the bit order did not achieve the desired result either.
Please help me understand what I am doing wrong with my SPI code.
Thanks