I've got a number between 0 and 1024, and I need to convert it to a range of 0 to 180.
I've found this answer on the maths for it here however I cant seem to get it right in my program. Here's what I have
int treatValue(int data) {
int OldRange = (1024 - 0);
int NewRange = (180 - 0);
int NewValue = (((data - 0) * NewRange) / OldRange) + 0;
return NewValue;
}
Where data is the value between 0 and 1024. What have I got wrong?
Here's some output using that code above.
X: 24 (506) Y: 22 (493)
X: 24 (506) Y: 21 (489)
X: 24 (506) Y: 20 (481)
X: 24 (506) Y: 19 (475)
X: 24 (506) Y: 17 (463)
X: 24 (506) Y: 11 (432)
X: 24 (506) Y: 9 (418)
X: 24 (506) Y: 8 (410)
X: 24 (506) Y: 7 (405)
X: 24 (506) Y: 6 (399)
X: 24 (506) Y: 3 (384)
X: 24 (506) Y: 1 (374)
X: 24 (506) Y: 0 (365)
X: 24 (506) Y: -1 (356)
X: 24 (506) Y: -4 (341)
X: 24 (506) Y: -10 (307)
X: 24 (506) Y: -14 (279)
X: 24 (506) Y: -17 (263)
X: 24 (506) Y: -20 (249)
X: 24 (506) Y: -24 (227)
X: 24 (506) Y: -27 (209)
X: 24 (506) Y: -29 (197)
X: 24 (506) Y: -30 (188)
X: 24 (506) Y: 31 (181)
X: 24 (506) Y: 29 (168)
X: 24 (506) Y: 27 (158)
X: 24 (506) Y: 24 (137)
X: 24 (506) Y: 19 (110)
X: 24 (506) Y: 14 (81)
X: 24 (506) Y: 10 (61)
X: 24 (506) Y: 6 (38)
X: 24 (506) Y: 2 (16)
X: 24 (506) Y: 1 (6)
X: 24 (506) Y: 0 (0)