// described here as I cannot edit the MAP() reference page
I recently had to map analogRead() to a percentage. Easy one would say, use the map function.
x = analogRead(A0); // x = 0..1023
percent = map(x, 0, 1023, 0, 100); // make percent of it
As the integer math truncates there will be on average about 10.2 input values mapped on every output value
EXCEPT for output value 100, as only the max input value 1023 is mapped upon the max output value 100
0 0
...
1013 99
...
1022 99
1023 100
This mapping was not good as I wanted to have at least 10 values mapped upon 100, just like the other values.
There are 2 solutions, both tweaking the ranges .
x = analogRead(A0); // x = 0..1023
percent = map(x, 0, 1013, 0, 100); // make percent of it
This only changes one input value, but at first sight it might be not clear why it should be 1013.
// 1013 = 1023 - 10, where 10 is # occurrences I want to have the max value of 100; (in fact this is not even true!, see output sketch below)
// note it can't be 1012 or 1011 that will give trouble
So in short the above works - sort of - but can be tricky to understand. Especially next month or so when you review your code
The other (better) way is
x = analogRead(A0); // x = 0..1023
percent = map(x, 0, 1024, 0, 101); // make percent of it
This changes both the input range and the output range by 1.
This works as we saw that the max output value only occurs for the max input value of the map(). As the max input value is one higher that the max value analogRead can provide it will never be reached. Thus the max output value will also never be reached.
A third way to use float math and do proper rounding would work to but is a performance killer.
a small sketch shows the differences
void setup()
{
Serial.begin(115200);
Serial.println("Start ");
for (int i = 0; i < 1024; i++)
{
int x = map(i, 0, 1023, 0, 100);
int y = map(i, 0, 1013, 0, 100);
int z = map(i, 0, 1024, 0, 101);
Serial.print(i);
Serial.print("\t");
Serial.print(x);
Serial.print("\t");
Serial.print(y);
Serial.print("\t");
Serial.println(z);
}
}
void loop() {}
to be continued ...