i try some solution of my own to be able to print float with sprintf
but no success so far , maybe you can have a look at the code and explain what i did wrong
i consider myself an advanced beginner but definitly not an expert
Thanks
edit : the only thing i am sure about is that on mega or uno , sprintf do not deal with float (%f)
float f1 = 1.23456;
float f2 = 2.34567;
float f3 = 3.45678;
float f4 = 4.56789;
float f5 = 5.67891;
char buffer1[200];
static char buffer2[10]; // if static and global , should always be at the same address ?? or maybe not ?
// also don t need to use free(); , am i wrong ?
char* myftostr(float data, byte width, byte precision) {
//static char chararray[15];
dtostrf(data, width, precision, buffer2);
return buffer2; // return pointer of buffer2
}
void setup() {
Serial.begin(115200);
}
// trying to "fix" sprintf "%f" disabled on arduino
void loop() {
Serial.println("BEGIN PRINT \"NORMAL\"");
Serial.print("float 1 val : ");
dtostrf(f1, 5, 1, buffer2);
Serial.print(buffer2);
Serial.print(" float 2 val : ");
dtostrf(f2, 10, 2, buffer2);
Serial.print(buffer2);
Serial.print(" float 3 val : ");
dtostrf(f3, 4, 4, buffer2);
Serial.print(buffer2);
Serial.print(" float 4 val : ");
dtostrf(f4, 1, 5, buffer2);
Serial.print(buffer2);
Serial.print(" float 5 val : ");
dtostrf(f5, 1, 0, buffer2);
Serial.println(buffer2);
Serial.println("BEGIN PRINT with function");
Serial.print("float 1 val : ");
Serial.print(myftostr(f1, 5, 1)); // CALL myftostr and print return pointer positioned at buffer2
Serial.print(" float 2 val : ");
Serial.print(myftostr(f2, 10, 2));
Serial.print(" float 3 val : ");
Serial.print(myftostr(f3, 4, 4));
Serial.print(" float 4 val : ");
Serial.print(myftostr(f4, 1, 5));
Serial.print(" float 5 val : ");
Serial.println(myftostr(f5, 1, 0));
Serial.println("BEGIN SPRINTF with function");
sprintf(buffer1, "float 1 val : %s float 2 val : %s float 3 val : %s float 4 val : %s float 5 val : %s",
myftostr(f1, 5, 1), myftostr(f2, 10, 2), myftostr(f3, 4, 4), myftostr(f4, 1, 5), myftostr(f5, 1, 0));
// line splitted for visibility , change nothing
Serial.println(buffer1);
Serial.println("BEGIN SPRINTF with function and sprintfstring separated");
static const char strSprnt[] = { "float 1 val : %s float 2 val : %s float 3 val : %s float 4 val : %s float 5 val : %s" };
sprintf(buffer1, strSprnt, myftostr(f1, 5, 1), myftostr(f2, 10, 2), myftostr(f3, 4, 4), myftostr(f4, 1, 5), myftostr(f5, 1, 0));
Serial.println(buffer1);
delay(2500);
}
// output on serial monitor
//
//BEGIN PRINT "NORMAL"
//float 1 val : 1.2 float 2 val : 2.35 float 3 val : 3.4568 float 4 val : 4.56789 float 5 val : 6
//BEGIN PRINT with function
//float 1 val : 1.2 float 2 val : 2.35 float 3 val : 3.4568 float 4 val : 4.56789 float 5 val : 6
//BEGIN SPRINTF with function
//float 1 val : 1.2 float 2 val : 1.2 float 3 val : 1.2 float 4 val : 1.2 float 5 val : 1.2
//BEGIN SPRINTF with function and sprintfstring separated
//float 1 val : 1.2 float 2 val : 1.2 float 3 val : 1.2 float 4 val : 1.2 float 5 val : 1.2