Go Down

Topic: Serial Decimal Reading? (Read 1 time) previous topic - next topic

12thspy

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

}

halley

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.

12thspy

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

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

halley

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

12thspy


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!

Arduino
via Egeo 16
Torino, 10131
Italy