You are right, I'm looking for a way to convert the 4 bytes into a float format. The cast (float)incomingvalue won't work. Try the code following. I've tested putting some bytes into the array, and it gives the same value as a float.
Edit: I've looked at page http://babbage.cs.qc.edu/IEEE-754/32bit.html I don't know which order your device will send the bytes, you may have to tinker, by reversing the order the 4 bytes are read into the array. I've posted the sketch I was using to try and convert bytes to and from floats. It gives the same value as the calculated values from the page, e.g. 100 is hex 42c80000, which is loaded as the values 0 0 200 66.
union t_long_fl { // this assign the byte array on the same memory as the float
byte b_arr[4];
float f_var;
} ;
t_long_fl t1;
int j;
int int_part_of_float;
int frac_part_of_float;
// for each block of data, as you coded it
j=0;
for (i=9; i<13; i++)
{
t1.b_arr[j] = Serial.read(); // add byte into array
j += 1;
}
int_part_of_float = (int)t1.f_var;
frac_part_of_float = 100 * (t1.f_var - (float)int_part_of_float);
Serial.print("Acceleration in Z: ");
Serial.print(int_part_of_float);
Serial.print('.');
Serial.println(frac_part_of_float);
Serial.print("\n");
long readnumber()
{
char c;
long res= 0;
int count = 0;
boolean pos = true;
while (true) {
if (Serial.available())
{
c = Serial.read();
count ++;
if (c >= '0' && c <= '9')
{
res *= 10;
res += (c - '0');
}
else if (c = '-' && count == 1)
{
pos = false;
}
else
else
{
break;
}
}
}
return pos ? res : -res;
}
union t_long_fl {
byte b_arr[4];
float f_var;
} t1 ;
void print_ba()
{
for (int i = 0; i < 4; i++)
{
Serial.print(" ");
Serial.print((int)t1.b_arr[i]);
}
}
void print_float(float f)
{
long i_i = (long)f;
int i_f = 100 * (f - (float)i_i);
Serial.print(i_i);
Serial.print('.');
Serial.print(i_f);
}
void setup()
{
Serial.begin(9600);
Serial.print("enter [Fii ff ] or [Imm mm mm mm ] or [P]\nii ff integer,fractional part for float\nmm values for each byte (dec)\n");
}
void loop()
{
long int_part;
int int_frac;
if (Serial.available())
{
char c = Serial.read();
Serial.print(c);
switch (c)
{
case 'I':
for (int i = 0; i < 4 ; i++)
{
t1.b_arr[i] = readnumber();
}
print_ba();
Serial.println();
break;
case 'F':
int_part = readnumber();
Serial.print("int part:");
Serial.print(int_part);
int_frac = readnumber();
Serial.print(" frac part:");
Serial.println(int_frac);
t1.f_var = int_part + float(int_frac)/100.0;
break;
case 'P':
print_float(t1.f_var);
print_ba();
Serial.println();
break;
default:
Serial.println(" bad char ");
break;
} // end switch
}
} // end loop