I am using an Arduino Mega 2560 R3. I typically use ARM chips of various flavors, but for this particular one-off prototype I am using the Mega because of the large number of analog inputs and digital I/O. Note that with ARM development, I have no problems with printf() and scanf().
I have modified the platform.txt file to include "-Wl,-u,vfprintf -lprintf_flt -lm" when compiling and linking. This adds support for floating point values in the printf and scanf family.
Here is a simple example.
void setup() {
float f = 6.66;
double d = 9.99;
char msg[100];
snprintf(msg, sizeof(msg), "printf float into f %f", f);
snprintf(msg, sizeof(msg), "printf float into lf %lf", f);
snprintf(msg, sizeof(msg), "printf double into f %f", d);
snprintf(msg, sizeof(msg), "printf double into lf %lf", d);
sscanf("9.99", "%f", &f);
sscanf("9.99", "%lf", &f);
sscanf("9.99", "%f", &d);
sscanf("9.99", "%lf", &d);
}
void loop() {
}
When I compile with "-wall", the output includes:
arduino-cli compile --fqbn arduino:avr:mega --verbose --warnings all Compile
AVR-Compiler-Tests/Compile/Compile.ino:7:59: warning: format '%f' expects argument of type 'double', but argument 4 has type 'float' [-Wformat=]
snprintf(msg, sizeof(msg), "printf float into f %f", f);
^
AVR-Compiler-Tests/Compile/Compile.ino:8:61: warning: format '%lf' expects argument of type 'double', but argument 4 has type 'float' [-Wformat=]
snprintf(msg, sizeof(msg), "printf float into lf %lf", f);
^
AVR-Compiler-Tests/Compile/Compile.ino:13:29: warning: format '%lf' expects argument of type 'double*', but argument 3 has type 'float*' [-Wformat=]
sscanf("9.99", "%lf", &f);
~~^
/Users/jwright/src/nemo.SVN/tray/Development/Compile.ino:14:28: warning: format '%f' expects argument of type 'float*', but argument 3 has type 'double*' [-Wformat=]
sscanf("9.99", "%f", &d);
~~^
The messages for sscanf() are what I expect - %lf should be used with double and %f should be used with float.
The messages for snprintf() seem broken to me. A format of %f expects double, and a format of %lf expects double. Huh?
Thanks,
Jim