Pages: [1]   Go Down
Author Topic: can't print a float value???  (Read 2508 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm getting a problem trying to Serial.print a float value
(I've read http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1169275761, but he is trying to cast it to int which I don't want)
I need to read the floating point numbers

is it possible at all to do Serial.print(myFloatVar) ???

this is the debugger warning

error: call of overloaded 'print(float&)' is ambiguous
C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:42: note: candidates are: void HardwareSerial::print(char)


C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:44: note:                 void HardwareSerial::print(uint8_t)


C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:45: note:                 void HardwareSerial::print(int)


C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:46: note:                 void HardwareSerial::print(unsigned int)


C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:47: note:                 void HardwareSerial::print(long int)


C:/DEV/_ARDUINO/arduino-0007/lib/targets/arduino/HardwareSerial.h:48: note:                 void HardwareSerial::print(long unsigned int)


do I have to modify the Serial lib to accept floating point values on the serial port?
thanks
b.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

At the moment, there's no real way to print a float.  Including the system routine to do so adds a large amount of size to the Arduino core.  It used to be that using floats at all meant that your sketch wouldn't fit on the chip.  Now that floats fit, I imagine that the float printing routine would still take too much space.  Although maybe now with lots of people using ATmega168's, it's worth investigating how to enable float printing.  

May I ask why you need to print a float and why casting it (or multiplying by a 1000 or so and casting it) won't work?
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks mellis,
the reason is I'm playing around with an accelerometer that is giving me very slight values
and I'm checking with the reference voltage that is going into the accelerometer.

I've seen that on some tutorials tom igoe is using digital pins with his pulseIn function to detect the data from the accelerometer...however i've hooked my dual axis to 2 arduino analog pins
and it's voltage reference to another arduino analog pin so I can check what Vin really is.

I know I could do that with a multimeter but I'm doing so that I can have a double reference

now for your solutions in fact does seem mathematically interesting smiley-wink

are you suggesting this?

float Vfloat;
int Vint  
Vfloat = analogRead (VinPin); //reads 665.6
Vint = (int)(Vfloat);
Serial.print(Vint);                   //prints 665

or this?

float Vfloat;
int Vint  
Vfloat = analogRead (VinPin); //reads 665.65
Vint = (int)(Vfloat*1000);
Serial.print(Vint);                   //prints 665650


another doubt is: to convert it to the 5 volt range I will have to multiply that to (5/1024)
is that correct?
ie:

Vint = (int)(Vfloat*1000*(5/1024));
Serial.print(Vint);                   //prints 3250 which means 3,2 V

that would be what my multimeter displays.
thanks as usual smiley
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your conversion (to a 5 volt range) looks right.  But it might be easier to just send the value returned by analogRead() (e.g. 0 to 1023) and just convert it on the computer.  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've seen a the same and similar problems, trying to use sprintf(dest,"%f %s",floatvalue,stringvalue);

Thinking that underneath the Arduino extensions there is C, I thought this would work, but does not?

Am I missing something simple (or being stupid) here?

Simon
« Last Edit: February 05, 2007, 06:06:21 pm by Phooto » Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino is C underneath.

A very common mistake is thinking that functions from the standard libraries on unix (e.g. printf() and friends, or math functions like pow()) are part of the C language.  They are not - they are add-ons that come from libraries or the operating system.

Another hurdle for folks who have programmed on larger systems but are programming a microcontroller for the first time is the size of available resources:  The Arduino has 8k of program space (1k of which is occupied by the bootloader) and 1k of RAM, and it doesn't have things like an operating system, virtual memory, etc.  Compare this to a typical desktop computer that has RAM measured in Gb and a few hundred Gb of disk space (which can be combined to make a virtual memory system limited only by disk space), has an OS with many megabytes of library and kernel functions to call upon, etc.  On a desktop box the printf() function occupies a few k of memory - you don't even notice a chunk of memory that small.  On the ATmega8, however, that "few k" is 50% of your available program space.

Of course, there are also things you can do with the Arduino that you can't do with your desktop computer, or that you can do with a US$32 Arduino instead of using a US$500 desktop computer.  Different tools for different jobs.

-j
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, but this is still a little confusing, so you are saying that printf is not available - although I have used it with integers?

But floating point is not available either? - but it is listed as an available data type.

I would really like to be able to use sprintf with strings at least.  I want to do some base64 encoding in the module to be able to send emails, so some string manipulation will be important!

Is there any documentation of what is and is not available from standard 'C'?
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do floating point, but the ATmega8 doesn't have any floating point hardware so it is all done in software and that eats up the available 8k program space.  For example, I had a program that was under 4k; I added one float variable and performed one multiplication on that variable and the resulting program would not fit on the ATmega8.  The size of the code to get the printf() function is around 4k, so if you want to printf() a float variable you need 4k for the printf() library, 4k for the floating point emulation library, and don't forget 1k for the Arduino bootloader (that's the part that keeps you from needing to buy a programmer).  That's a total of 9k of code to fit on the 8k ATmega8, and we haven't included any of your code yet.

Technically, yes: floating point variables and floating point printf() are available.  Practically, however, their uses are limited.

Work is being done to link against libraries instead of chunks of object code, so future versions of the Arduino environment may be able to make effective use of floats and printf() withing the available limits.

Arduino relies on AVRlib. I'm not sure where the official documentation lives, but you can always go and poke through the code in the directory "tools/avr" under the Arduino install directory and see the source code and man pages.

Again, the standard C language is there, but what's generally referred to as the "standard C library" (or libc on unix) has only portions implemented in Arduino and AVRlib.

-j
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 134
panta rei resist the resistors!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AVR Lib can be found here
http://www.nongnu.org/avr-libc/user-manual/modules.html

as suggested in the playground
http://www.arduino.cc/playground/Learning/Programming

I've also found this other link but it's not official
http://www.sigterm.de/projects/sens-o-nuts/Dokumentation/atmel/libc/
Logged

Pages: [1]   Go Up
Jump to: