Pages: [1]   Go Down
Author Topic: mapping floating point numbers  (Read 940 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int value = analogRead(5);
int newval = map(value, 0, 1023, 0, 1600);
float result = newval/100;
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you, that worked..
Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try casting newval to a float and making the literal a float too.
Code:
int value = analogRead(5);
int newval = map(value, 0, 1023, 0, 1600);
float result = (float)newval/100.0;
Whoops, forgot the cast smiley-red
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 223
Posts: 13910
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

can be done in one line as the mapping contains the point (0,0); 

try this
Code:
// 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
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: