HOME - - - - - - - - Table of contents, my Arduino "How To" articles
Other material for programmers Delicious Bookmark this on Delicious   Recommend to StumbleUpon

"Printing" floating point numbers over the serial stream

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);
 }

Serial.print and "overloading"

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".




   Search this site or the web        powered by FreeFind
 
  Site search Web search
Site Map    What's New    Search

The search engine is not intelligent. It merely seeks the words you specify. It will not do anything sensible with "What does the 'could not compile' error mean?" It will just return references to pages with "what", "does", "could", "not".... etc.
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....

Sequenced set of tutorials on Arduino programming and electronics interfacing.
Tutorials about the free database supplied with Open Office version 2. (If you experienced Adabas with Star Office, ooBase is nothing like it!)
Some pages for programmers.
Using the parallel port of a Windows computer.


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.




Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated!

Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.


And if you liked that, or want different things, here are some more pages from the editor of these tutorials....

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?

Click here to visit editor's pages about using computers in Sensing and Control, e.g. weather logging.



To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed!


Valid HTML 4.01 Transitional Page tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....