Map() analogRead() Problem

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

Try this...

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

Cees:
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:

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:

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

//Basel

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

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.

Thanks :slight_smile:

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