Go Down

Topic: Serial Decimal Reading? (Read 2058 times) previous topic - next topic


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.


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);
 delay(10);                  // 1 sec delay to keep things in control



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.


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

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


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


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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131