sprintf command used with floats

 sprintf(MessageBodyWarning, "WARNING!!! Your Temperature is at an unstable level:%.02f \nTime of Occurance: %02d:%02d:%02d \n ", LeftFloatTemp, hour(), minute(), second());

This is the command i'm trying to run. The time works properly but I am very unsure how to make my LeftFloatTemp work properly for it to be displayed as a proper float.

The only results i ever seem to get when using 'f' is a '?'. Regardless of what other details i add to the f format. I have researched this and I'm not sure if other ever even came to a conclusion most of which consist of major conversion, if theres a way around all this I'd like to know if anyone else has a solution. All input is appreciative. I am Running an RTC, an arduino Mega with Ethernet shield. with a typical TMP temp sensor plugged in.

If you need any more details please let me know.

Thanks in advance.

Trevor

By default, floating point output is not supported for sprintf.

There are versions which do support floating point. Perhaps someone else knows how to specify those versions for Arduino, but I don't.

The usual workaround is to convert the float to a string first with dtostrf, and then use that in the sprintf line.

Go in /hardware/arduino/avr .
Create a file platform.local.txt and add this line in it:

compiler.c.elf.extra_flags= -Wl,-u,vfprintf -lprintf_flt -lm   -Wl,-u,vfscanf -lscanf_flt -lm

Save file, and restart Arduino IDE. Now you should be able to use sprintf and sscanf with floats :wink:
But be aware that this will use around 2KB of memory (around 1% of the available memory of an Arduino Mega 2560).

There's also dtostrf()
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42

This comes up fairly often. Here's another thread:
http://forum.arduino.cc/index.php?topic=103935.0

guix:
Go in /hardware/arduino/avr .
Create a file platform.local.txt and add this line in it:

Just to clarify, is this the same place where I find the boards.txt, programmers.txt and platform.txt? If you want me to make a new txtfile as platform.local.txt I also have the issue of not being able to create nothing but folders.

Yes, in the same place where you can find boards.txt etc. Too bad we cannot put this file in Documents/Arduino or something...

I think your problem is because you don't have write permission to the Program Files folder. Google will help you... You have to be an administrator on your computer so that you can modify permissions for the Program Files/Arduino folder.

If you are administrator already and don't understand how to setup permissions, you can create the file elsewhere (desktop) and then you can move it in the folder. Yes, it's stupid -_-.

Well first off thanks for the windows tutorial haha. I have properly added that file. I still have the same result with my float it gives me "?" as a result. Here's my code is possible I'm just not doing it right at this point.

sprintf(MessageBodyWarning, "WARNING!!! Your Temperature is at an unstable level:%02f \nTime of Occurance: %02d:%02d:%02d \n ", LeftFloatTemp[x], hour(), minute(), second());

I don't think it matters but I am running an array as my float it shouldn't matter unless you think otherwise.

Which IDE version do you use? You need at least version 1.5.7 beta for this file to be loaded. If you use a version >= 1.5.7, then you did something wrong but no idea what it could be. Turn on detailed compiling (in Arduino IDE's preferences) and check the last few lines of the compile window, you should see something like this:

...

C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-gcc -w -Os -Wl,--gc-sections,--relax -mmcu=atmega2560 -Wl,-u,vfprintf -lprintf_flt -lm -Wl,-u,vfscanf -lscanf_flt -lm -o [...]

C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -j .eeprom [...]

C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avr-objcopy -O ihex -R .eeprom [...]

Sketch uses 9,792 bytes (3%) of program storage space. Maximum is 253,952 bytes.
Global variables use 214 bytes (2%) of dynamic memory, leaving 7,978 bytes for local variables. Maximum is 8,192 bytes.

I'm using IDE 1.6.1 and can't seem to find the setting of detailed compiling.
Take a look at the attachment file to verify that I didn't just miss it. I also looked into the txt file and find nothing like such? Any more leads? Suggestions?

ArdionoIDE_Preferences.PNG

The one you got already, don't muck with the IDE, just use dtostrf().

KeithRB:
The one you got already, don't muck with the IDE, just use dtostrf().

It's actually very simple. for the IDE version 1.0.x, edit Compiler.java and change the compiler flags to be specified via an option in "Preferences.txt":

    Preferences.getBoolean ("build.floating_point") ?
    "-Wl,-u,vfscanf,-lscanf_flt,-u,vfprintf,-lprintf_flt,--gc-sections" + optRelax :
    "-Wl,--gc-sections" + optRelax,

Next, the "Preferences.java" can be edited to provide a user checkbox to set this option to "true" or "false":

        // up top where the objects are defined, add:
        JCheckBox useFloatingPoint;

// then add this where the checkboxes are built:

        // [ ] Enable floating point support
        useFloatingPoint = new JCheckBox (" Enable floating point support");
        pain.add (useFloatingPoint);
        d = useFloatingPoint.getPreferredSize ();
        useFloatingPoint.setBounds (left, top, d.width + 10, d.height);
        right = Math.max (right, left + d.width);
        top += d.height + GUI_BETWEEN;

// in "applyFrame()", add this:

        setBoolean ("build.floating_point", useFloatingPoint.isSelected ());

// in "showFrame()", add this:

        useFloatingPoint.setSelected (getBoolean ("build.floating_point"));

Rebuild the IDE, then do NOTHING except REPLACE your current [b]"arduino-1.0.x/lib/[color=red]pde.jar[/color]"[/b] with the new one that resulted from rebuilding the IDE (save the old one just in case - rename it "pde.old").

If you don't want to bother editing Preferences.txt, then simply do the Compiler.java edit, rebuild, copy pde.jar over and MANUALLY add [b]"build.floating_point=true"[/b](or false) to your "preferences.txt" file and the dreaded "?" goes away.

Note this will cause your sketch to become about 1.5K to 2.0K larger...

Frankly, I hate to be this guy but I truly don't understand that last post about Changing the IDE, you say it's simple but I'm not familiar with messing around with such tools and I don't know what you mean by things like :

edit Compiler.java and change the compiler flags to be specified via an option in "Preferences.txt":

I'd be okay with using the dtostrf() function, but I don't understand the purpose does it replace my %02f completely if i convert my float into a String?

TLuck:
Frankly, I hate to be this guy but I truly don't understand that last post about Changing the IDE, you say it's simple but I'm not familiar with messing around with such tools and I don't know what you mean by things like :
I'd be okay with using the dtostrf() function, but I don't understand the purpose does it replace my %02f completely if i convert my float into a String?

You don't convert it ot a String. Then it wouldn't work at all. You convert it to a string (char array). Then you use %s to put that string into your final output instead of %f.

It really makes more sense to do it that way. Not only is changing the IDE to make it do sprintf with floats complicated, but it also bloats any code that uses sprintf even if it doesn't use it with floats by about 2k. It's not worth it.

char FloatValue[10];
    dtostrf(LeftFloatTemp[x], 5, 2, FloatValue);
    sprintf(MessageBodyWarning, "WARNING!!! Your Temperature is at an unstable level:%02s \nTime of Occurance: %02d:%02d:%02d \n ", FloatValue[x], hour(), minute(), second());

I've read the dtostrf() function help page over and over I don't understand why this Code doesn't send out what i want. Does anyone see the fault in my code?

:%02s

I have no idea what including leading zeroes on a string does.

You're right that slipped out, it's been corrected and still the same issue. I feel like I'm doing something totally wrong.

Why is FloatValue[ x] in the sprintf statement?

sprintf(MessageBodyWarning, "WARNING!!! Your Temperature is at an unstable level:%02s \nTime of Occurance: %02d:%02d:%02d \n ", FloatValue[x], hour(), minute(), second());

You just want FloatValue, not FloatValue[ x ]. You're giving a pointer to a string now, it's not a number in an array anymore.

That is it! That's the solution I have results wow! greatly appreciated everyone, I feel like the tilt of focus is real! Thanks for keeping me on tracks everyone! Like already posted this final functional code is the following :

 char FloatValue[10];
    dtostrf(LeftFloatTemp[x], 5, 2, FloatValue);
    sprintf(MessageBodyWarning, "WARNING!!! Your Temperature is at an unstable level: %s °C \nTime of Occurance: %02d:%02d:%02d \n ", FloatValue, hour(), minute(), second())