Pages: [1]   Go Down
Author Topic: Map() analogRead() Problem  (Read 326 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Map value problem;

Although the value read is 1023, result is sometimes 11 en sometimes 12 ????
How can than be?

Basic Code (not actual program!):

ValMap = map(analogRead(A0), 0, 1023, 0 , 12);
Serial.print(analogRead(A0), DEC);Serial.println(ValMap, DEC);

Result on screen
1023 - 11
1023 - 11
1023 - 12
1023 - 12
1023 - 11

========================
Making analogRead() first a Value, then putting it into map(), solved the problem!!!, Thank you...
« Last Edit: January 12, 2013, 08:11:52 am by Cees » Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 197
Posts: 12741
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Try this...

Code:
int ara0;
ara0 = analogRead(A0);
ValMap = map(ara0, 0, 1023, 0 , 12);
Serial.print(ara0, DEC);Serial.println(ValMap, DEC);
Logged

Sweden
Offline Offline
Sr. Member
****
Karma: 13
Posts: 257
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Map value problem;

Although the value read is 1023, result is sometimes 11 en sometimes 12 ????
How can than be?

Basic Code (not actual program!):

ValMap = map(analogRead(A0), 0, 1023, 0 , 12);
Serial.print(analogRead(A0), DEC);Serial.println(ValMap, DEC);

Result on screen
1023 - 11
1023 - 11
1023 - 12
1023 - 12
1023 - 11

Look at the first two lines of the code, let me quote:
Code:
ValMap = map(analogRead(A0), 0, 1023, 0 , 12);
Serial.print(analogRead(A0), DEC);

You're making two different measurements on these lines. Try changing it to the following:
Code:
int val1=analogRead(A0);
ValMap = map(val1, 0, 1023, 0 , 12);
Serial.print(val1, DEC)

//Basel
Logged

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


Be aware that the map function has serious rounding troubles around the high end.

Code:
for (int i=0; i<1024; i++)
{
  int x = map(i, 0,1023, 0,12) ;
  Serial.print(i);
  Serial.print("\t");
  Serial.println(x);
}

you might want to use map(i, 0,1024, 0,13) ; to get the results you want.
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks  smiley

Making analogRead() first a Value, then putting it into map(), solved the problem!!!
Logged

Pages: [1]   Go Up
Jump to: