Page 1 of 1

Map Function

Posted: Wed Sep 27, 2017 10:28 am
by medelec35
Arduino has a useful Map function.
You just enter in_min, in_max, out_min and out_max.
The the value out will between out_min and out_max.

For example I created a project that uses 10 bit ADC to drive a servo using a microsecond delay.
As the servo I was using works from 700uS to 2400uS I just used:

Code: Select all

pulseWidth = map(analogRead(potPin),0,1023,700,2400);
It saves working out a formula to do the same thing.

The actual function is:

Code: Select all

Result = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
Martin