Reputation: 2802
I've got a servo which turns opposite to the number I get from a program. The numbers I get from the program are between 37...113. I need to convert the 37 to its opposite side. So 37 becomes 113, 38 becomes 112, and so on. 75 stays at 75 because that's the mid point.
Do any of you know of a way to calculate this? This sounds like simple math, but I can't figure it out. I don't want to use a look-up table because the range may change.
Upvotes: 16
Views: 8888
Reputation: 14524
public int reverseNumber(int num, int min, int max) {
return (max + min) - num;
}
reverseNumber(37, 37, 113); // returns 113
Upvotes: 43
Reputation: 5903
public int calculate(int min, int max, int input) {
return max-(min-input);
}
Upvotes: 2