# Need to 'linearize' a logarithmic pot

**URL:** <https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [March 26, 2019, 3:27pm UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647 "2019-03-26T15:27:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)\
**Post date:** [March 26, 2019, 3:27pm UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/1 "2019-03-26T15:27:03Z")

</div>

Have been working with @jeremydouglass on using a joystick that was returning unusual values, and I couldn’t get them to work like a ‘normal’ joystick.

As it turns out - the pot on this stick has a logarithmic curve! I’ve verified this by converting each x & y value from the pot to log(x) and log(y) and use that to draw a line. And I get a nice straight line at some angle on the screen!

So I’ve been searching for ways to turn log values to linear - and getting some HEAVY math I don’t quite ‘get’ :-). I’ve also done some ‘shoot-from-the-hip’ coding (as in the code below) in Processing and am getting SOMETHING… close but no cigar!

In the ballpark - but not there yet. The line drawn runs all over the screen - so there must be a way to ‘math’ these values into linear values. I need them to be 0 to height and 0 to width where w&h/2 = screen center. Then they have to be converted to Cartesian coordinates.

* * *

```auto
// X is the value from the stick which currently runs from 170 to around 900;
float fX = pow(10,log(X/20));
float fY = pow(10,log(Y/30));
line(10,fX,fY,70);

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 26, 2019, 4:14pm UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/2 "2019-03-26T16:14:53Z")

</div>

> [@alan856](#):
>
> And I get a nice straight line at some angle on the screen!

In that case, all you need to do is scale the line and cancel its angle. ax + b = cy.

Can you share your point data?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [March 26, 2019, 4:15pm UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/3 "2019-03-26T16:15:50Z")

</div>

> [@alan856](#):
>
> As it turns out - the pot on this stick has a logarithmic curve! I’ve verified this by converting each x & y value from the pot to log(x) and log(y) and use that to draw a line. And I get a nice straight line at some angle on the screen!

> [@alan856](#):
>
> so there must be a way to ‘math’ these values into linear values.

Since you are getting a straight line then have you not solved the problem?

You have the code

> [@alan856](#):
>
> float fX = pow(10,log(X/20));

This will not solve the problem since the log function is “logarithm to the base e” but your power function is trying to reverse a value calculated as “logarithm to the base 10”.

```auto
for(float x = 170; x < 900; x += 20){
  println(x , log(x), exp(log(x)));
}

```

If you try this code you will see that to reverse the `log` you need to take the `exponential`,

Based on your description the curve is not _logarithmic_ rather it is _exponential_

HINT: Map the pot values to the range 0-1 rather than 0-width and 0-height this means the code can be used with any screen size by multiplying by the screen size.

---

<div class="post-metadata">

**Author:** ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)\
**Post date:** [March 26, 2019, 11:51pm UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/4 "2019-03-26T23:51:25Z")

</div>

This is a joystick data-stream - can’t really make a copy! 🙂 Enclosed is a picture of this beast, up on wood blocks so I can get to things. It using an Arduino Micro to send to a CP-2104 USB-Serial chip which Processing can open as a port.

I’ve been trying so many ways to get this #@)$@ thing to work… getting  
kinda punchy. I don’t recall now how I got the idea, but I tried converting the data with the log() function - which I forgot was logN. So…

All I need to do is get the movement of the joystick to create x-y points on any place on the screen. As I may have said - it was working FINE with the Logitech 3D stick. I don’t know if I’ve mentioned this - but I sub’d in a little Parallax ‘thumb-stick’ and I could get the kind of motion I’m looking for. This confirmed the Navy pots are NOT linear -  
not sure what they are now… @Quark thought it might be exponential.  
I am seriously thinking of finding a 10k pot with 360 rotation and replacing those big pots. It has been hard to find anything at a decent  
price so far.

=A.

![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/45356442217e666a9c17a653ed41d7b683eb803c.jpeg)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 27, 2019, 12:29am UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/5 "2019-03-27T00:29:50Z")

</div>

Well, for example, hold the joystick and record the output values:

left 90 : 170  
left 45 : ?  
center(90) : 300  
right 45 : ?  
right 90 : 900

…and then do the same for up-down.

If you only go with the three values you already shared with us (170, 300, 900) for whichever axis that was, then you get something like this:

> **[Online curve-fitting at mycurvefit.com](https://mycurvefit.com/index.html?action=openshare&id=68c936f2-030f-4454-aa7f-60f33c6d24c7)**
>
> An online curve-fitting solution making it easy to quickly perform a curve fit using various fit methods, make predictions, export results to Excel/PDF/Word/PowerPoint, perform a custom fit through a user defined equation and share results online.

```
y = 134.0426 + 165.9574*e^(+1.529395*x)

```

so given x (the joystick) ranging from -1 to 1, that produces 170-300-900.

---

<div class="post-metadata">

**Author:** ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)\
**Post date:** [March 27, 2019, 12:45am UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/6 "2019-03-27T00:45:04Z")

</div>

Yes - I can move the joystick to the major cardinal positions. I have an Arduino script displaying the values. The web-site looks very cool - will try it with at least 6 values. THANKS!

---

<div class="post-metadata">

**Author:** ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)\
**Post date:** [March 27, 2019, 1:07am UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/7 "2019-03-27T01:07:28Z")

</div>

Ok - did the CurveFit and here is the link to the results: By specifying log10 I got a line i produced (somehow) in my many trial and errors. So now - how do i use those formulas to get the joystick vales to be in the processing coordinate system: x=0 - height y=0 - width.  
If this doesn’t work out I have found a cheap continuous trimmer pot that should do the job. But handling it with the math is much cooler!  
🙂

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 27, 2019, 2:38am UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/8 "2019-03-27T02:38:30Z")

</div>

> [@alan856](#):
>
> how do i use those formulas

I would suggest using “share” like I did to share what you did. Your post does not contain a link to the result.

Export formula by clicking on the small gray down triangle, then clicking the button Export Equation.

---

<div class="post-metadata">

**Author:** ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)\
**Post date:** [March 27, 2019, 10:34am UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/9 "2019-03-27T10:34:20Z")

</div>

Jeremy - I actually did use the Share button, but for some reason the link did not get in my reply. Here it is again

The math here is getting way past me! Just not sure now how to apply this. Thanks for all the help.  
=Alan R.

---

<div class="post-metadata">

**Author:** ![alan856](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@alan856](https://discourse.processing.org/u/alan856)\
**Post date:** [March 27, 2019, 12:54pm UTC](https://discourse.processing.org/t/need-to-linearize-a-logarithmic-pot/9647/10 "2019-03-27T12:54:22Z")

</div>

Found an Arduino scaling solution here: [http://bit.ly/2HFZPrS](http://bit.ly/2HFZPrS)  
It looks like it is working GREAT - converting on Arduino side and sending (hopefully) ready-to-use data to Processing. But it seems slightly off.

If/when you have time, could you look at this function - I’m not sure what the ‘curve parameter’ is or should be. I’m currently using his value of -1.5.
