0

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)
Community
  • 1
  • 1
TMH
  • 6,096
  • 7
  • 51
  • 88
  • Why do you think you're doing something wrong -- what unexpected behavior are you seeing? Also, please add an appropriate tag for what programming language this is supposed to be. C? C#? Java? etc. – TypeIA Feb 10 '14 at 20:27
  • @PeteR This is integer math, so doing it that way is doomed to failure (the result will always be 0 unless `data==1024`). – TypeIA Feb 10 '14 at 20:28
  • Converted all the vars to floats, and it works now, thanks for that tip, I'm still getting used to needed int/floats etc, I don't need that kinda thing in PHP really :P. – TMH Feb 10 '14 at 20:31
  • @dvnrrs: Oops, sorry. Used to working with floats and storing the result in an integer. It's a PLC thing ;) – The Blue Dog Feb 10 '14 at 20:31
  • I've got it working now by converting the vars to floats, and the above code the X and Y are coming from a joystick, the values in brackets are the raw values (0 to 1024 range) and the other value is what was returned from the above function. – TMH Feb 10 '14 at 20:33
  • "convert it to a range of ..." is vague. The suggestions so far seem to focus on a linear mapping, which is definitely one way to do it. However, if your only requirement is an output number in the suggested range, then something like `input % 180` would work as well, as would many other different "mappings". It might help to clarify exactly what you need... – twalberg Feb 10 '14 at 21:01
  • @Tom Hart: Could you please write this up as an answer and accept it. It's disappointing to have to read a lengthy question and 6 comments just to find out it's already been solved. – tom10 Feb 10 '14 at 23:02

1 Answers1

0

This is the code what worked.

float treatValue(int data) {
    return (data/1024)*180;
}
TMH
  • 6,096
  • 7
  • 51
  • 88
  • If you're ok with the answer being slightly more approximate (due to truncation instead of rounding), you could get away with not using floating point by reorganizing it to say `(data*180)/1024` - but watch out for overflow if `data` gets too large. Shouldn't be an issue given your original problem statement, though, as long as you have integers that are at least 20 bits... – twalberg Feb 11 '14 at 13:23