Fix/improve map() example (10bit to 8bit value)

In map() - Arduino Reference you have the following code example:

  val = map(val, 0, 1023, 0, 255);

This produces a bad result. For example, 0-4 all map to 0 (5 values), while only 1023 maps to 255 (1 value). This despite 0-1023 being precisely 4x as many values as 0-255.

I'd suggest to change the example to this:

  val = map(val, 0, 1024, 0, 256);

This requires that no value above 1023 actually occurs in the input. Under this condition, it'll map 0-1023 perfectly to 0-255, that is, 0-3 map to 0, 4-7 to 1, and so on up to 1020-1023 mapping to 255 (i.e. there's always 4 values from the input mapping to 1 value in the output).

good point (proposed more often)

for this particular example one could use val = val / 4;

@robtillaart sure, or just val >>= 2, but given that they want to demo the map() function, that'd kinda defeat the point :wink: