Go Down

### Topic: Map() analogRead() Problem (Read 745 times)previous topic - next topic

#### Cees

##### Jan 12, 2013, 10:24 amLast Edit: Jan 12, 2013, 02:11 pm by Cees Reason: 1
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...

#### Coding Badly

#1
##### Jan 12, 2013, 10:32 am

Try this...

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

#### baselsw

#2
##### Jan 12, 2013, 10:34 am

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: [Select]
`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: [Select]
`int val1=analogRead(A0);ValMap = map(val1, 0, 1023, 0 , 12);Serial.print(val1, DEC)`

//Basel

#### robtillaart

#3
##### Jan 12, 2013, 12:19 pm

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

Code: [Select]
`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.
Rob Tillaart

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

#### Cees

#4
##### Jan 12, 2013, 02:07 pm
Thanks

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

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