multiple floats/int with serial connection

hello,

i cant figure out how to collect and process multiple floats recieved from a serial connection.
i have got this:

union u_tag
{
    byte b[4];
    float ival;
} u;

float value;


void loop()
{
  if(Serial.available() > 3)
  {
    u.b[0] = Serial.read();
    u.b[1] = Serial.read();
    u.b[2] = Serial.read();
    u.b[3] = Serial.read();
    
    value = u.ival;
  }
}

with my serial connection i can pass a float with:
arduino.writeFloat( 512 );
arduino.flush();

but: how do i pass multiple floats or int?!
arduino.writeFloat( 512 );
arduino.writeFloat( 256 );
arduino.writeFloat( 99 );
arduino.writeFloat( 0 );
arduino.flush();

i hope someone can help me on.
thanks!
flo

but: how do i pass multiple floats or int?!

Just like you show there would seem to be right.

What is the problem with that code?

well i am not receiving a single float at once (eg 512) but splited into single bytes. thats why i have got that union-tag to merge it (one float).
i dont know how to handle multiple floats, event not sure if the incoming bytes are always pairs of four…
unfortuately i cant use the serial monitor along with that serial connection with flash. it crashes :frowning:

I had a similar problem with writing to an LCD and not being able to use the Serial monitor. I moved the LCD to a software serial port so I could use the serial monitor to troubleshoot. It was an eye opener!

hey sacman, thanks for the info. i will give it a try.

edit:
sorry guys forget about this :confused:
there was a mistake somewhere else.
so it works like this:
u.b[0] = Serial.read();
u.b[1] = Serial.read();
u.b[2] = Serial.read();
u.b[3] = Serial.read();
value1 = u.ival;

u.b[0] = Serial.read();
u.b[1] = Serial.read();
u.b[2] = Serial.read();
u.b[3] = Serial.read();
value2 = u.ival;

...and so on...

i will place it into a loop in order to get rid of that bunch of code... looks ugly :slight_smile:

well, thanks for your help!
flo