Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino: printf/fprintf prints question mark instead of float

Tags:

I have the following code for an Arduino sketch:

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
static FILE lcdout = {0} ;

static int lcd_putchar(char ch, FILE* stream)
{
    lcd.write(ch) ;
    return (0) ;
}

void setup() {
  lcd.begin(16, 2);
  fdev_setup_stream (&lcdout, lcd_putchar, NULL, _FDEV_SETUP_WRITE);
}

void loop() 
{
  stdout = &lcdout;
  printf("%.2f Volts", 2.0);
}

The problem comes at the last line of the code. This should print out "2.00 Volts" but instead, it prints "? Volts" (a question mark instead of the actual float value). If I try to format an integer, this works great.

So basically, if I replace the printf line with the following, it will work properly:

printf("%d Volts", 2); //prints correctly "2 Volts"

Any idea what's the problem ?