Puny problem with wrong pot selection

CuriousFox

New member
Hi, hello everyone, I am building a oscilloscope clock based on Teensy 3.6 and I made a slight mistake of buying pots for XY position with wrong charasteristics - LOG instead of LIN. They are working as a simple voltage divider so nothing fancy, but I want to correct my mistake by altering the code apropriately. I wouldn't bother with doing so if only these worked as positioning pots, but these are also used as controlls for tetris and pong, which can also be run there. Can someone help me understand how i should proceed with correcting my mistake? I am a newbie when it comes to programing such devices, so I am kinda a bit lost atm. Pointers would be definetly usefull, although I seriously wouldn't mind some more, direct help per say. Code is here: https://github.com/amalmin/SCTVcode/tree/main/SCTVcode Still, I can probably do it myself, I perhaps need some pointers, or instructions, or explanation, dunno really, never done such thing in the past. :/
 
Been playing with 'Phind' (an intelligent search engine and assistant for programmers) for a few days now and decided to throw your question at it.
Here is the link to my question: [url]https://www.phind.com/search?cache=pgf8ezjr8w7r88hijp3rey0y[/URL].

This is the code it generated. More detail can be found in the original response on my question (click link above).
Code:
double logPotToLinear(double logPotOutput, double minLogPotOutput, double maxLogPotOutput, double minLinearOutput, double maxLinearOutput) {
    // First, normalize the log pot output to a value between 0 and 1
    double normalizedLogPotOutput = (logPotOutput - minLogPotOutput) / (maxLogPotOutput - minLogPotOutput);

    // Apply the inverse logarithm function to convert the normalized log pot output to a linear scale
    double linearOutput = minLinearOutput + (maxLinearOutput - minLinearOutput) * (1 - exp(-normalizedLogPotOutput));

    return linearOutput;
}

Perhaps you can add this function to your code and see whether it fits your need?

Paul
 
Before you write any code, I'd recommend first using a very simple program with just Serial.println(analogRead(mypin)) and watch the numbers as you turn the pot. A large knob and maybe a piece of paper or other material where you can mark the angle will also help. The main goal is to learn what the pot actually does.

Most of those "log" pots are actually more like piecewise linear, usually with 2 or 3 regions that give only a fairly crude approximation of logarithmic change. The manufacturing process pretty much involves coating different areas of surface the pot's wiper (center pin) touches with materials of different conductivity. You'll likely see a nearly linear change in each region which has its own material.

Even after you get the log-to-linear conversion correct, the main problem you will face is the effect of noise. Every ADC has noise, and any ADC inside a microcontroller can be susceptible to the effects of the CPU running and especially any of the digital pins changing while the ADC is acquiring the signal. With a linear pot, this unavoidable noise gives a consistent amount of uncertainty about the pot's actual physical position. But with a log pot, at one end of its physical range, even a small amount of movement gives a large change in signal. When the pot is turned to that region, the effect of noise on the final result will be much greater. Maybe your project isn't very demanding where not-so-good quality results are ok? But if this is important, you'll probably need to get linear pots.
 
Last edited:
Back
Top