Go Down

### Topic: mapping floating point numbers (Read 2349 times)previous topic - next topic

#### ars

##### May 08, 2012, 08:01 pm
Hello,
I urgently require to map the analog readings from my pot, in range 0-1023 , to a floating point range like: 0.001,0.002,......,15.99,16.00. For this the ordinary map function doesn't serve the purpose, i tried using fscale , but it says that "fscale was not declared in this scope". This is my code:

void setup() {
Serial.begin(38400);
}

void loop() {

}

#### Arrch

#1
##### May 08, 2012, 08:11 pm
Map the integer to within the limits of the float time a power of ten. Divide the result by said power of ten.

Say for example, you need a value between 0.00 and 16.00. Example:

Code: [Select]
`int value = analogRead(5);int newval = map(value, 0, 1023, 0, 1600);float result = newval/100;`

#### ars

#2
##### May 08, 2012, 08:33 pm
Hey,
No luck, now I am getting values from 0.00 ,1.00,......15.00,16.00. Not the correct values.

#### dxw00d

#3
##### May 08, 2012, 08:37 pm
Try casting newval to a float and making the literal a float too.
Code: [Select]
`int value = analogRead(5);int newval = map(value, 0, 1023, 0, 1600);float result = (float)newval/100.0;`

#### ars

#4
##### May 08, 2012, 08:39 pm
Thank you, that worked..

#### Arrch

#5
##### May 08, 2012, 08:45 pm

Try casting newval to a float and making the literal a float too.
Code: [Select]
`int value = analogRead(5);int newval = map(value, 0, 1023, 0, 1600);float result = (float)newval/100.0;`

Whoops, forgot the cast

#### robtillaart

#6
##### May 08, 2012, 09:25 pm
can be done in one line as the mapping contains the point (0,0);

try this
Code: [Select]
`// 0.015640274 == 16.0 / 1023.0#define FACTOR (0.015640274)void setup() {  Serial.begin(38400);}void loop() {  float result = analogRead(A0) * FACTOR ;  // * 16.0 / 1023.0  Serial.print(result, 3);  Serial.print("\t");  float result = analogRead(A1) * FACTOR;   Serial.print(result, 3);  Serial.print("\t");  float result = analogRead(A2) * FACTOR;   Serial.print(result, 3);  Serial.println();}`

using CTRL-T in the IDE cleans up the layout of your code, and code tags makes it better readable
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up