Algebra nerd needed - number scaling equation
rogersyd
Posts: 223
Hi all.
My high school algebra teacher would probably be very disappointed in me for not retaining this information but I need some help with an equation:
This works brilliantly for scaling a value from 0-4095 to a value between 30-90 (for example). Here, Y would equal 30 if X was 0. I hope that others may find that useful in spin. I scoured the net for a while and found many examples, but this was the easiest to follow.
Now I need an equation that would reverse the resulting scale. So in the above example, if X was 0, Y would equal 90, and if X is 4095 Y would equal 30.
I could use some help. Thanks all!
My high school algebra teacher would probably be very disappointed in me for not retaining this information but I need some help with an equation:
Y:= (((X - 0) * (90 - 30)) / (4095 - 0)) + 30 'NewValue := (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
This works brilliantly for scaling a value from 0-4095 to a value between 30-90 (for example). Here, Y would equal 30 if X was 0. I hope that others may find that useful in spin. I scoured the net for a while and found many examples, but this was the easiest to follow.
Now I need an equation that would reverse the resulting scale. So in the above example, if X was 0, Y would equal 90, and if X is 4095 Y would equal 30.
I could use some help. Thanks all!
Comments
X = ((Y - NewMin) * (OldMax - OldMin) / (NewMax - NewMin)) - OldMin
At any rate, it should be something like that. I haven't checked it..
Ya I was going to do that for the example to make it look easier, but it would have to be unrolled near the end anyway and I found it awkward and somewhat error prone.
What I was trying to do is probably easiest to explain if I show the working code so here she be:
So offset runs from 80...0 then 0..80. Im using an analog stick and an ADC to control two motors. Wow that explanation would have been really useful in my original post. LOL. I was trying to do this a number of ways when this solution hit me square in the face. Who hoo! Progress! Thanks again jazzed and Leon!
Wouldn't something like this be easier?
offset = (c0 - 2057) / 25