# Problem with mapping servo range to input values

Hi

I’m having issues with controlling a servo with a pot in a script for a Mini Maestro (12 ch). What I want to achieve is to map a servo range, say around 900 microseconds to 2000 microseconds, to a smaller portion of the full range of the potentiometer input values, say between 100 and 140. I’m tinkering with the example script given in the documentation but can’t seem to find the right conversion to achieve this. Anyone have an idea of how this can be done? Thanks beforehand.

Hi.

Does the example script work if you run it without modification? An analog input range of 100-140 represents a difference of just 200mV. Is your input signal really that small? Is it from a potentiometer? Could you post your script and describe what happens when you run it along with what you want to happen?

-Claire

Hi!

Yes it’s not that the script doesn’t work in that sense, what I mean is with this code for example:

begin
1 get_position # get the value of the pot, 0-1023
4 times 4000 plus # scale it to 4000-8092, approximately 1-2 ms
0 servo # set servo 0 based to the value
repeat

The base example for controlling a servo with a potentiometer, taken from the documentation. With this example, I have to rotate the potentiometer from minimum to maximum for the servo to turn it’s whole range, roughly speaking.

What I want is to map the range 1ms to 2ms but only for a small portion of the potentiometers range, so to move the pot a little to the left would move the servo from 1.5ms to 1ms and moving it a little to the right would move the servo from 1.5ms to 2ms if you get what I mean!

Hello.

To map an analog reading to a specific range, you can start by breaking it down and finding an equation to relate the two. For example, mapping a reading of 100-140 to target positions of 4000-8000, the relationship could be:

targetPosition = (analogReading - 100) × 100 + 4000

Then, you would just need to adjust the script to use that relationship:

begin
1 get_position # get the value of the pot, 0-1023
100
minus
100 times
4000 plus # scale it to 4000-8092, approximately 1-2 ms
0 servo # set servo 0 based to the value
repeat

However, please note that 200mV is a pretty small variation for that kind of range, so small amounts of noise could create a lot of variation in our servo position.

Brandon

Hi!

Allright I understand, I have already figured out that it’s a math problem in that sense, I was just hoping that there was some trick to the approach (I’m not terribly good at either math or the maestro scripting syntax). But your example does help a bit in terms of how the conversion can be done, thanks!

I understand that it will result in the servo being “more sensitive” but I have also been a bit unclear maybe. By the values 100-140 I meant how they are presented in the Status-tab, from 0-255. So the difference 100-140 would be something closer to roughly 400-450mV which is a bit better I guess.

Thanks for the input, I’ll have to experiment some more!

By the way, in case you aren’t aware, please note that while the “Status” tab shows the position indicator varying between 0 to 255μs for the analog input, in your script, this range corresponds to numbers from 0 to 1023. So, if your 100-140 reading is referring to the slider position, then the 40 unit difference should correspond to around a 780mV difference. Additionally, the scaling in the code I posted previously would not work correctly. The equation would look something like this instead, since 100-140μs corresponds to analog readings between 400-560:

targetPosition = (analogReading - 400) × 25 + 4000

Brandon

Oh yeah I’m aware! Thanks though. Ah oh yeah of course ~1/5 would correspond to 800mV and not 450mV (told you I wasn’t very good at math hehe).

Yeah I landed on something similar although your equation seems to work best

Thank you for the help!