Map Function

Please add any feature requests for Flowcode version 7 here

Moderator: Benj

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Map Function

Post 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
Martin

Post Reply