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 numbers directly. 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.
Another "issue" connected with using Serial.print is explained after showing you how to print a floating point number.
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 ran the following program, you might expect "65 66" to turn up on the serial monitor... but you wouldn't get that. You'd get "A 66". (65 is the ASCII code for the character "A"; 66 is "B"'s code.)
byte bTmp; int iTmp; bTmp=65; iTmp=66; Serial.print(bTmp); Serial.print(iTmp);
Serial.print is an overloaded function. Don't worry, you don't have to extend sympathy. You just have to understand the details of the overloading.
When Serial.print is asked to pass a byte-type datum to the output stream, it assumes you want the number treated as a code for a character. 65 yields "A", 97 yields "a". There are lots more, not all of them giving rise to "printable" characters. One, for instance, causes some output devices to start a new line. You can even get a "1"... but not by filling bTmp with 1. If you put 49 in bTmp, the program above will deliver a "1".
Why didn't the program above yield a "B" in response to the Serial.print(iTmp) when iTmp was holding 66? Because of Serial.print's "cleverness". Pass it an int-type datum and it sends "66" to the output stream if you have passed it 66; it prints the number as a series of digit characters.
If Serial.print's built-in cleverness suits your needs, you're done. For those times when you want to do something different, there are ways....
The following....
byte bTmp; int iTmp; bTmp=65; iTmp=66; Serial.print(bTmp,DEC); Serial.print(iTmp,DEC);
... will give rise to "65 66". Well, "6566", actually, but you can imagine the space between the numbers, can't you? And...
byte bTmp; int iTmp; bTmp=65; iTmp=66; Serial.print(char(bTmp)); Serial.print(char(iTmp));
... would give rise to "AB".
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?
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 .....