Shop OBEX P1 Docs P2 Docs Learn Events
Algebra nerd needed - number scaling equation — Parallax Forums

Algebra nerd needed - number scaling equation

rogersydrogersyd Posts: 223
edited 2012-04-08 17:35 in Learn with BlocklyProp
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:
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

  • LeonLeon Posts: 7,620
    edited 2012-04-01 06:56
    I solved it for X, and got:

    X = ((Y - NewMin) * (OldMax - OldMin) / (NewMax - NewMin)) - OldMin

    At any rate, it should be something like that. I haven't checked it..
  • jazzedjazzed Posts: 11,803
    edited 2012-04-01 08:56
    Given:
    
    NewValue = (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin
    
    Solve for OldValue 
    
    1.  NewValue - NewMin  = ((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)
    2. (NewValue - NewMin) * (OldMax - OldMin) = (OldValue - OldMin) * (NewMax - NewMin)
    3. (NewValue - NewMin) * (OldMax - OldMin) / (NewMax - NewMin) = OldValue - OldMin
    4. (NewValue - NewMin) * (OldMax - OldMin) / (NewMax - NewMin) + OldMin = OldValue
    5.  OldValue = (NewValue - NewMin) * (OldMax - OldMin) / (NewMax - NewMin)) + OldMin
    
    Replace OldValue with X and NewValue with Y:
    
    X = (Y - NewMin) * (OldMax - OldMin) / (NewMax - NewMin) + OldMin
    
  • LeonLeon Posts: 7,620
    edited 2012-04-01 09:09
    That's almost what I made it! I replaced all the values with a, b, c. etc. to make things easier. I forgot to change the sign of OldMin.
  • jazzedjazzed Posts: 11,803
    edited 2012-04-01 12:57
    Leon wrote: »
    That's almost what I made it! I replaced all the values with a, b, c. etc. to make things easier. I forgot to change the sign of OldMin.

    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.
  • rogersydrogersyd Posts: 223
    edited 2012-04-01 14:56
    Thank you both very much indeed! Ill give it a whirl this week.
  • rogersydrogersyd Posts: 223
    edited 2012-04-08 15:22
    I figured this out. Thanks again for the advice. I didnt explain what I was looking for very clearly so the solutions provided didnt generate the results I needed, but thanks none the less for trying o help.

    What I was trying to do is probably easiest to explain if I show the working code so here she be:
      case c0
         2059..2055: 
           offset:=0
         2054..0:    'LEFT
           offset:=(((c0 - 2054) * (80 - 0)) / (0 - 2054)) + 0
         2060..4093: 'RIGHT
           offset:=(((c0 - 2060) * (80 - 0)) / (4093 - 2060)) + 0
    

    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!
  • W9GFOW9GFO Posts: 4,010
    edited 2012-04-08 17:35
    rogersyd wrote: »
    I figured this out. Thanks again for the advice. I didnt explain what I was looking for very clearly so the solutions provided didnt generate the results I needed, but thanks none the less for trying o help.

    What I was trying to do is probably easiest to explain if I show the working code so here she be:
      case c0
         2059..2055: 
           offset:=0
         2054..0:    'LEFT
           offset:=(((c0 - 2054) * (80 - 0)) / (0 - 2054)) + 0
         2060..4093: 'RIGHT
           offset:=(((c0 - 2060) * (80 - 0)) / (4093 - 2060)) + 0
    

    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
Sign In or Register to comment.