-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapRange_Arduino.js
More file actions
34 lines (29 loc) · 1015 Bytes
/
MapRange_Arduino.js
File metadata and controls
34 lines (29 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* This function is inspired by the map function of Arduino (C++)
* It transfers a value from the current range
* to a new range of values specified by parameters
* @param {Number} x - value to convert
* @param {Number} inMin
* @param {Number} inMax
* @param {Number} outMin
* @param {Number} outMax
* @param {Object} options - convert number return in integer
* @returns
*/
const map_range = (x, inMin, inMax, outMin, outMax, options) => {
let value = outMin;
if (x < inMin || x > inMax) {
x < inMin && (value = outMin);
x > inMax && (value = outMax);
return value;
}
value = ((x - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
if (options) {
options.convertInt && (value = parseInt(value));
}
return value;
};
console.log(map_range(10, 0.002, 1, 0, 100)); // 100
console.log(map_range(0.000002, 0.002, 1, 0, 100)); // 0
console.log(map_range(25, 0, 50, 0, 100)); // 50
console.log(map_range(0.0111, 0.00001, 1, 0, 100, { convertInt: true })); // 1