scaling and shifting
Shifting and Scaling
So what happens when your sensor is giving your a range of numbers that just isn't big enough? Or what happens if your sensor is giving you a range of numbers that is too big? Well, what you need is a way to shift and scale your range of numbers.
EXAMPLE 1: Under optimal conditions, sensors usually have a large range of numbers that they can output. On the Cricket, for example, this range is 0 - 255. But say that my sensor is giving me values that range between 10 and 88. This might happen to a light sensor that is in a room that never gets completely dark (a 10 value perhaps) and never gets completely light (an 88 value perhaps). Say that I want and ouput range that is 0 - 100 (perhaps those represent the notes that I want to play on a Cricket). Here's how you do it:
- Please gather the following numbers (you may need to experiment by hooking up a display module to determine these numbers)
- __Input minimum__ (What is the lowest number your sensor is giving you?)
- __Input maximum__ (What is the largest number your sensor is giving you?)
- __Output minimum__ (What is the lowest number you want your sensor to give you?)
- __Output maximum__ (What is the largest number you want your sensor to give you?)
- Evaluate the shifting and scaling equation.
- Scaled_Input = (Input_Value - Input_Min) * (Output_Max - Output_Min) / (Input_Max - Input_Min) + Ouput_Min
- For Example 1 we would do the following ...
|Scaled_Input = (Input - 10) * (100 - 0) / (88 - 10) + 0|
|Scaled_Input = (Input - 10) * (100 / 78)|
|Scaled_Input = (Input - 10) * (1.28)|
So, if you want to scale a value you first need to subtract 10 and then multiply it by 1.28.
You may find this page useful for more information. It is aimed at users of Lingo (another artistic programming language), but the math and graphics are useful.