# problem using the map() function..

Basically if I do this:

``````  pot1 = analogRead(5);                       //check to see if another wave has been selected
waveSelect = map(pot1, 0, 1023, 0, 3);      //waveform pot, divided into 4
``````

Then so should it go like this: waveSelect=1, pot1=0-255 waveSelect=2, pot1=255-511 waveSelect=3, pot1=512-767 waveSelect=4 pot1=768-1023

right? Becasue it's not working for me like that. Maybe I'm misunderstanding what map does?

Your map range is 0 to 3. But you are expecting values in range 1 to 4.

waveSelect = map(pot1, 0, 1023, 0, 3); //waveform pot, divided into 4

Your map range is 0 to 3. But you are expecting values in range 1 to 4.

Not only that, but the only value in the from range that will map to the upper limit of the to range is the last value.

As specified,
pot1 = 0 - 341 ==> 0
pot1 = 342 - 682 ==> 1
pot1 = 683 - 1022 ==> 2
pot1 = 1023 ==> 3

To get the values you are expecting, you need a map call like:
waveSelect = map(pot1, 0, 1024, 1, 4);

Your map range is 0 to 3. But you are expecting values in range 1 to 4.

waveSelect = map(pot1, 0, 1023, 0, 3); //waveform pot, divided into 4

oops that was just me not thinking when i started this topic, in the actual code I have it right.

PaulS:
Not only that, but the only value in the from range that will map to the upper limit of the to range is the last value.

As specified,
pot1 = 0 - 341 ==> 0
pot1 = 342 - 682 ==> 1
pot1 = 683 - 1022 ==> 2
pot1 = 1023 ==> 3

To get the values you are expecting, you need a map call like:
waveSelect = map(pot1, 0, 1024, 1, 4);

This, is my problem. It kept changing from waveSelect 3 to 2 as soon as pot1 hit 1022. How does setting the upper bound to 1024 change things, as pot1=max 1023??

Why are you using "map" at all?

AWOL: Why are you using "map" at all?

That way it's simpler if I want to change my code and want to divide by 5,6 or whatever, it's easy. I can just change that one line. For other ways(as far as I know), I'd have to change a lot more code..

and want to divide by 5,6 or whatever, it's easy.

Can't disagree there. ;)

How does setting the upper bound to 1024 change things, as pot1=max 1023??

Look at the map function:

``````long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
``````

in_min and out_min are both 0 in your initial map call, so map looks like: return (x) * (out_max) / (in_max); With out_max equal 3 and in_max = 1023, the map function looks like: return x * 3 / 1023; The only value for x that results in returning 3 is 1023. If you change in_max and out_max to 4 and 1024, the function looks like return x * 4 / 1024; Now, any value of x over 767 will return 3. The maximum input value, 1023, will return 3.

So, if you want values from 0 to 3 to be returned, you need to call map: waveSelect = map(pot1, 0, 1024, 0, 4);

If you want values from 1 to 4: waveSelect = map(pot1, 0, 1024, 1, 5);

(Yes, I had the wrong to range).

Thanks for that, all makes sense now. :) I was scratching my head there for a while.

WoW.. Learned something new.. :P