How can I send a float variable to Processing?

I'd like send to Processing the float variables calculated with my Arduino Duemilanove, because I need to display the values obtained. How can I do, if function Serial.print works only with int values? I tried with a conversion like this:

float val ; serial.print(int(val),BYTE) ;

but I lose to much significant figure! Does exist a more clever way?!

Can you multiply the values up first, then transmit the int, then step it back down to a float on the other end?


float orrig = 10.123 int transmit = orrig * 1000 // transmit is 10123 now Serial.print(transmit);


then in processing:

int incoming = getIncomingIntSomehow(); float orrig = (float)incoming / 1000.0;

None of this is tested... but the basic idea should work.

The reference material hasn't been updated but Print works with floats albeit only to two decimal places. If that amount of precision is okay for your application, then you are good to go.

Prior to recent ARDUINO versions (version 15 I think) I was using the following subroutine to print floats.

void serialPrintFloat( float f){
// print results properly with float decimal value
 int i;
    i = (f - (int)f) * 100;
   Serial.print( abs(i) ); 

Now lets hope this question thread can me moved to where it belongs?

// print results properly with float decimal value

I don't think so - consider "f = 3.5" and "f=3.05" ;)

In Arduino 16

thanks to all for the suggestions! Pwillard solution seems to be the better for me, printed value in Processing is acceptable, but.... why with 3.5 the reading is correct, with 3.05 I read 3.4 instead? :-?

but.... why with 3.5 the reading is correct, with 3.05 I read 3.4 instead?

Because the sketch doesn't work, because the integer "print" method suppresses leading zeroes. If you notice, it doesn't print "3.5", it prints "3.50", because the fractional part of the float is multiplied by 100, and "50" is the value printed. For 3.05, the actual float will be not be exact, and internally will be something like "3.049..", so 0.04 * 100 = 4, so it prints "3.4", missing out the leading zero.