Go Down

Topic: mapping floating point numbers (Read 1 time) previous topic - next topic

ars

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() {

float valp = fscale(0,1023,0,16,analogRead(A0),0);


  Serial.print(analogRead(A0));Serial.print("\t");Serial.print(analogRead(A1));Serial.print("\t");Serial.print(analogRead(A2));Serial.println("");
}


Please help..

Arrch

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

Hey,
No luck, now I am getting values from 0.00 ,1.00,......15.00,16.00. Not the correct values.

dxw00d

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


Arrch


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 :smiley-red:

robtillaart

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
 


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