Shop OBEX P1 Docs P2 Docs Learn Events
How to scale numeric ranges — Parallax Forums

How to scale numeric ranges

electroelectro Posts: 2
edited 2007-07-17 22:08 in Learn with BlocklyProp
Sorry if this is clearly discussed somewhere else but I am having a little trouble getting my mind around the process of converting one range of numbers to another scale. For example: I want to drive a servo (300-1500) with the output of a Ping! module and have 1-50 from the ping map to the 300-1500 scale needed for the servo. I saw the explanation of this sort of operation in the Smart Sensors documentation but am still having trouble putting it into practice. If anyone can spare a couple seconds to try and maybe give an alternate perspective on how this is done I would be very grateful.

Thanks again!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-07-16 23:33
    It's simple numeric scaling. A servo range of 300-1500 is a span of 1200 (think 1500 minus 300). It's much easier to have the PING range over the values 0 to 50 so a PING value of 0 corresponds to the 300 and a PING value of 50 corresponds to the 1500. Just for reference, a PING value of 1 corresponds to a servo value of 324. If it's important to have the servo start at 300, you can adjust the 24 and the 300.

    If you have the PING value in the variable "p" and you want the servo value in the variable "s", you do:
    s = (p * 24) + 300
  • Ed StennieEd Stennie Posts: 18
    edited 2007-07-16 23:57
    Mike is correct. You might wonder where the 24 comes from however. There are 51 posible Ping values, 0 - 50. The number of steps that can exist is 50 - 0 or 50. If we divide the span (1200) by 50 we get 24.
  • electroelectro Posts: 2
    edited 2007-07-17 11:46
    Thanks guys! That really helped! Seeing that jittery little servo respond to the Ping brought a smile to my face. Again, thanks for such an awesome and speedy reply from you both!
  • SSteveSSteve Posts: 808
    edited 2007-07-17 22:08
    There's a really good write-up on scaling values beginning on page 76 in Smart Sensors and Applications. You can download it for free from www.parallax.com/html_pages/downloads/siccurriculum/documentation_sic_curriculum.asp

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
Sign In or Register to comment.