Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumProgramming Questions & HelpPrograms › sending a float to the serial

Page Index Toggle Pages: 1
sending a float to the serial (Read 7480 times)
04/23/07 at 17:12:13

barmak   Offline
YaBB Newbies
I Love YaBB 2!

Posts: 5
*
 
How is possible to send a float number to the serial without converting it to integer?

Thanks, Again!
Rafael.
 
IP Logged
 
Reply #1 - 04/24/07 at 17:05:25

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
hi rafael, floating point is not supported by the Serial, one way could be to multiply the number if it is very small or do a conversion before sending it through the serial.
 
IP Logged
 
Reply #2 - 11/16/07 at 18:24:57

pcunix   Offline
YaBB Newbies
I Love YaBB 2!
Middleboro MA

Posts: 6
*
 
barmak wrote on 04/23/07 at 17:12:13:
How is possible to send a float number to the serial without converting it to integer?

Thanks, Again!
Rafael.


I just had to do that this morning.   I'll give you the subroutine I used here, and the full write-up of the program I was using is at http://aplawrence.com/Electronics/usb_io.html

Basically you multiply your float by 10 * some scale, and then pass it as a long to this.   Of course if your floats are very large you would need to break them up even more.

// subroutine for floating point
// takes a long
void myprint(float number, int scale) {

//  a lot of extra work in here because of rounding and overflows
// probably could consolidate some of this but it's clear this way and
// no need for super speed

  float mult=pow(10,scale);
  float rounded= floor(number /mult);
  float biground=rounded * mult;
  float remainder=(number - biground);
  remainder = remainder / mult;
  Serial.print(long(rounded));
  Serial.print(".");
 
  while (scale--) {

     float toPrint=floor(remainder * 10);
     Serial.print(int(toPrint));
     remainder -= (toPrint/10);
     remainder *=10;
  }

  Serial.println(" ");
}
 
IP Logged
 
Reply #3 - 06/02/08 at 22:31:06

N201LJ   Offline
YaBB Newbies
darwin is my copilot
USA

Posts: 13
*
 
Your other option is to do the conversion in hardware, I suppose.  I have absolutely no experience using it, but the Micromega uM-FPU v3.1 can do floating point to ASCII conversions. 

It uses an I2C or SPI interface.
 

alan duncan&&to email me, use: lastname dot firstname @ mac dot com
IP Logged
 
Reply #4 - 10/28/08 at 15:51:21

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
From 0017 it is possible, and it works as with any other type.

float a = 0.5;

Serial.print(a);
 
IP Logged
 
Page Index Toggle Pages: 1