Hello! I'm trying to optimize my code so I'm using functions to manage all the repetitive printing stuff. I want to write a function able to handle strings and double values as well with extra parameters. I already managed to print but in this topic it's said that is nonsense to feed strings and use float to string conversion. So, I want to ask why I shouldn't use dtostrf() and how I should write the function to handle both variable types?
This is my current code:
void setup() {
Serial.begin(9600);
to_serial("Hello world"); //print string
to_serial(42.42); //print number
}
void loop() {
}
void to_serial(double text, bool param1){ //handle numbers print request
char text2[5]; //on the same topic is said to be unnecesary to
//declare this variable
return to_serial(dtostrf(text,4,2,text2), param1);
}
void to_serial(char * text, bool param1){ //handle text print request
Serial.print(text);
Serial.println();
}
And the output, as expected is:
Hello world
42.50
But, if I comment the first void to serial it doesn't compile:
sketch_sep03a:4:24: error: cannot convert 'double' to 'char*' for argument '1' to 'void to_serial(char*, bool)'
to_serial(42.42, NULL);
^
exit status 1
cannot convert 'double' to 'char*' for argument '1' to 'void to_serial(char*, bool)'
Is there any other way to handle this? Thanks for your help and time