Pages: [1]   Go Down
Author Topic: Serial Decimal Reading?  (Read 1216 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I have  project where i am wanting to read a decimal value through the serial monitor.  But all my data truncates to an integer it seems.  Below is my code which should do a simple count at 1.1, 2.1, 3.1 etc.  but all i get 1 2 3 4 etc.

Thoughts?

float count = 1.1 ;



void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

}

void loop()                      
{
  count = count + 1.0;
  Serial.print(count, DEC);
  Serial.println();
  delay(10);                  // 1 sec delay to keep things in control

}
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In Arduino libraries up to 0012, there is no built-in way to print a float variable to the serial port.  What you're using is casting the value to an integer.  There are a number of home-grown functions here to fill that gap.  I've heard 0013 will have a float-to-string or float-to-print capability but I don't know the details.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hmmm, then what is the point of the DEC option?

Serial.print(b, DEC) prints b as a decimal number in an ASCII string
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As opposed to printing it in HEX, or as a BYTE value.  In computing, the word "decimal" means "in base ten, digits 0 to 9," not particularly "with a decimal point and fractional parts afterward."
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Ah, that clears things up for me.  Good to know, guess i will just have to manually break down the values as i need.  

Thank you much for you prompt info.
Logged

Pages: [1]   Go Up
Jump to: