Pages: [1]   Go Down
 Author Topic: Map() analogRead() Problem  (Read 128 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 2
 « on: January 12, 2013, 04:24:02 am » Bigger Smaller 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);

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
Offline
Shannon Member
Karma: 118
Posts: 10164
 « Reply #1 on: January 12, 2013, 04:32:18 am » Bigger Smaller Reset

Try this...

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

Sweden
Offline
Full Member
Karma: 5
Posts: 115
 « Reply #2 on: January 12, 2013, 04:34:15 am » Bigger Smaller 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);

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);

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

//Basel
 Logged

Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
 « Reply #3 on: January 12, 2013, 06:19:35 am » Bigger Smaller 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 -

Offline
Newbie
Karma: 0
Posts: 2
 « Reply #4 on: January 12, 2013, 08:07:57 am » Bigger Smaller Reset

Thanks

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

 Pages: [1]   Go Up