Pages: [1]   Go Down
Author Topic: reading voltages with decimal value and serial printing it  (Read 219 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i want to read a voltage say 4.5 and print it in serial print as 4.5.Iam using the code below but it is printing only the integer 4.
The code is ..

int volt_in_an=A0;    // select the input pin for measuring analog i/p voltage
double volt_in=0;
void setup()
{
   //pinMode(pwm_out, OUTPUT);   //sets the pin as output
   Serial.begin(9600);
}
void loop() {
   volt_in=analogRead(volt_in_an);
   double volt=map(volt_in, 0, 1023, 0, 5);
   Serial.println(volt,DEC);   // displays analog input voltage  value 0 to 5
}


suggestions are welcome....
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but it is printing only the integer 4.
No. It is printing the float value 4.0000000, but, since the fractional part is 0, it is not printed.

The analogRead() function returns an int. Storing that in a float won't improve its accuracy.

The map function takes a collection of integer values and returns an integer result. Storing an int in a float won't restore the truncated portion.
Logged

Pages: [1]   Go Up
Jump to: