sprintf()? Can't get it to work. What am I missing?

temp is a global char buffer to this function. When I pour in numbers to this method all I get from my debugging is val : ? What in the world am I missing here?

void label::setValue(double val) {

  Serial.print(val); Serial.print(" : ");
  
  int num = sprintf(temp,"%f",val);
  
  Serial.println(temp);
  
  setValue(temp);
  setRefresh();
}

Thanks!

-jim lee

Have you enabled the patch to use floating point format specifiers?

Hint int num = sprintf(temp,"%f",val); if you don't want to use the returned length, you don't have to. If you want to make it clear, (void)sprintf(temp,"%f",val); would be sufficient.

Umm, no? 'Cause I never heard about this patch thing..

What is this you speak of?

-jim lee

By default, %f doesn't work for Arduino, it adds about 1500 bytes to your sketch. You need a patch - search the forum.

You're already using Serial.print - why not just use that?

Another common fix is to use dtostrf.

After reading that there was some sort of patch. I did a search and found where people are rolling their own. I think I'll just do that.

The Serial.print() is only for debugging. Its the string buffer I really want to fill.

Thanks! I never would have guessed it was turned off by default.

-jim lee

I found this, and it works wonderful.

http://dereenigne.org/arduino/arduino-float-to-string

-jim lee

the sprintf() function does not work for floating point for Arduino.

If you want to use it, there are several work-arounds.