hello everyone, maybe a very easy question for you. I want to output multiple variables through println function, how can I do this?
for example, if I want to output variables a, b, c, how can I output them using println in one sentence? Thanks a lot, I am looking forward to your reply.
"sprintf" is valid, but its use with "float" type is limited, though it can be patched.
Can I ask why it is so important that you output everything in a single expression?
Serial.printf((int) f); //print a floater's integer portion
Serial.printf('.'); //print a decimal point
Serial.printf(abs(f * 100) % 100); //print the floater's two decimal point
You can change it to print out as many decimal points as you wish. Or to make a generic function out of it.
Serial.printf((int) f); //print a floater's integer portion
Serial.printf('.'); //print a decimal point
Serial.printf(abs(f * 100) % 100); //print the floater's two decimal point
Or, more succinctly and correctly Serial.println(f, 2);
@dhenry, please try not to mislead quite so much and occasionally RTFM
for example, in my program, there are four float variables outputs, float a, b, c, d. I want the outputs at one line which are all with 2 bits after the decimal points, how should I do? thanks a lot!
You know that the four float variables will be the input for the host computer, and the host computer has to read the serial port, every read it should read 4 variables at a time.
xianpeng:
The reason I want to output it at one line is to be the input for the host computer. It is the form demand.
No I think you totally misunderstand what the format is saying. The host needs the four values and then a line feed / carriage return. It doesn't care, or even know, if this happened through three Serial.print statements and one Serial.println rather than just one print statement.