My thanks to "mem" at the Arduino forum for the following code, and for permission to post it here. He did say it was put together quickly, so do please get in touch (with me) if you discover any "features" which need attention!
Serial.print does not print floating point. Here is a sketch with a function you can use to display a "double" type value, giving whatever number of decimal places you need. The function is followed by a simple "setup" and "loop" exercising the function.
Remember: You don't need extra hardware to see what is being produced by Serial.println.... just click on the Serial Monitor icon on the Arduino development environment's toolbar. I have a How To explaining that, if you are interested.
/*Program to provide and demonstrate a way
to send floating point numbers to the serial
stream.*/
double x;
double y;
double z;
void printDouble( double val, unsigned int precision){
/* prints val with number of decimal places determine by precision
NOTE: precision is 1 followed by the number of zeros for the
desired number of decimal places
Example:
printDouble( 3.1415, 100); // prints 3.14 (two decimal places)
*/
Serial.print (int(val)); //prints the int part
Serial.print("."); // print the decimal point
unsigned int frac;
if(val >= 0)
frac = (val - int(val)) * precision;
else
frac = (int(val)- val ) * precision;
Serial.println(frac,DEC) ;
}
void setup(){
Serial.begin(9600);
Serial.println("Print floating point example");
printDouble( 3.1415, 100);
/* Previous line is an example of call to
printDouble to print pi to two decimal places*/
x = 10;
y = 3.1;
z = x / y;
delay(3000);// Give reader a chance to see the output.
}
void loop(){
printDouble(z,10); // one decimal place
printDouble(z,100); // two decimal places
printDouble(z,1000); // three decimal places
z = z + .1;
delay(100);
}
If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service.
Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.
Click here to visit the homepage of my biggest site.
Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?FrmAht" I added to that link causes your browser problems. Please let me know, if so?
Link to editor's oldest homepage.
Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org
....... P a g e . . . E n d s .....