user4527: I have a pair of potentiometers, on the Arduino, the output seems to drift, by one or 2 digits, back and forth. I would prefer they were read as one fixed value, without variation when their knobs are not turning. You can see in the image, the first and second values are shifting back and forth slightly. How should I address this?
I'm thinking of making a standard deviation function, but then I thought that might be unnecessarily complex.

My code is that the 2 sensor values are setting points in an SVG polyline. Variance means extra points will get plotted, while the device waits for valid input.
How should I deal with the variance in potentiometer readings to avoid outputting noise?
Here's my source:
/**Fucntion Defs**/
void svgHeader();
void svgFooter();
void polyLineBegin();
void polyLineEnd();

int Reset = 4;     //programmatically reset the arduino
int potX = A0;
int potY = A1;

int sensorValX = 0;
int sensorValY = 0;
int oldX = 0;
int oldY = 0;

int del = 2;       //a button on pin 2 for deleting drawn content
int delbutton = 0;
void setup() {
  Serial.begin(9600);
  pinMode(del, INPUT);
  svgHeader();
  polyLineBegin();
  delay(1000);
}

void loop() {
  // close the svg and start over
  if (digitalRead(del) == LOW ) {
    delbutton = 1;
    polyLineEnd();
    svgFooter();
    delay(5000);
    digitalWrite(Reset, LOW);
    digitalWrite(Reset, HIGH);
    svgHeader();
    polyLineBegin();
  } else {
    delbutton = 0;
    //Serial.println(delbutton);

  }

  //dont draw if its not changing
  sensorValX = analogRead(potX);
  sensorValY = analogRead(potY);
  if (sensorValX == oldX && sensorValY == oldY) {
    ; //dont do shi.
  } else {
    Serial.print(sensorValX);
    Serial.print(",");
    Serial.print(sensorValY);
    Serial.print(",");
    oldX = sensorValX;
    oldY = sensorValY;
    // Serial.println(delbutton);
    delay(1000);
  }
}
/**Functions**/

void polyLineBegin() {
  Serial.println ("<polyline points=\""); //x,y points go here

}

void polyLineEnd() {
  Serial.println ("0, 0\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");  //a dirty hack to avoid dealing with 1 last trailing comma.

}

void svgHeader() {
  Serial.println("<svg width=\"1023\" height=\"1023\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
};

void svgFooter() {
  Serial.println ("</svg>");
};


user68310: I think you should handle the trailing comma within the Arduino by using leading commas instead, but omit the first leading comma using a flag to indicate the first coordinate:
bool firstCoordinate;

void loop()
{
    . . .
    if (xVariance >= threshold || yVariance >= threshold)
    {
        if (!firstCoordinate)
        {
            Serial.print(",");
            firstCoordinate = false;
        }
        Serial.print(sensorValX);
        Serial.print(",");
        Serial.print(sensorValY);
        oldX = sensorValX;
        oldY = sensorValY;
    }
    . . .
}

void polyLineBegin()
{
    Serial.println("<polyline points=\""); //x,y points go here
    firstCoordinate = true;
}


void polyLineEnd()
{
    // Clean line ending.
    Serial.println("\" stroke=\"red\" fill=\"transparent\" stroke-width=\"5\"/>");
}

Alternatively, the first coordinate could be added to the line when it begins, then there is no need for a flag:
void polyLineBegin()
{
    Serial.println("<polyline points=\""); //x,y points go here
    Serial.println(sensorValX);
    Serial.println(",");
    Serial.println(sensorValY);
}

void loop()
{
    . . .
    if (xVariance >= threshold || yVariance >= threshold)
    {
        Serial.print(",");
        Serial.print(sensorValX);
        Serial.print(",");
        Serial.print(sensorValY);
        oldX = sensorValX;
        oldY = sensorValY;
    }
    . . .
}

The thresholding technique is great, but, as an aside, I recently discovered this excellent statistics library by Majenko while working on a non-blocking averaging function for Arduino and looking at converting it into a class. I found that that library already does what I want.

user4143: Some methods for "dealing" with the noise:

Take multiple samples. Calculate the average ("mean"). Discard the decimal. This is "oversampling" along with resolution reduction (but reduction of the now increased oversampled resolution).
Take multiple samples. Find the most common value ("mode").
Divide the value by 2 (or 4 if needed). This reduces the range of values and the sampled resolution but discards the noise portion.

The problem you are suffering from is "quantisation noise". This is because an analog signal and a digital signal are not completely compatible. The ADC converts the analog signal into a digital value by successively comparing it to smaller and smaller voltages in steps ("quanta"). There is a limit to how small the steps can get (the resolution), but an analog voltage doesn't adhere to those steps, it will always be somewhere between the final two steps. Sometimes it will be detected as the lower step, sometimes the upper step. By taking multiple samples and averaging them you are estimating how close to one step or the other the value is and "refining" the result. This is "oversampling" and is used to increase the resolution of the ADC at the cost of sample speed.
With those extra samples you can either say "It's this much closer to the upper step" (take the "mean"), or say "It's more likely to be nearer the upper step" (take the "mode") to try and "fix" the value to one of the steps.

user40318: Remove the analog component from your device.
Use a rotary encoder.
It will sense the knob rotation digitally.
One huge advantage is that you can easily select the sensitivity of the input. One click can advance the counter by .001 just as easily as advancing by a 1000.
https://duckduckgo.com/?q=rotary+encoder&iar=images&iax=images&ia=images


user4527: ok, once I got the idea I would need to do it in code, it became simpler to solve. In looking around the terms of quantization, the term thresh-holding came up, and I realized how to solve the problem.
I created a threshold value, and compared the difference between old and new variables.
int threshold =3;
int xVariance = abs(sensorValX-oldX);
int yVariance = abs(sensorValY-oldY);
if (xVariance >=threshold && yVariance >= threshold ||xVariance >=threshold || yVariance >=threshold){
  Serial.print(sensorValX);
  Serial.print(",");
  Serial.print(sensorValY);
  Serial.print(",");
 oldX=sensorValX;
 oldY=sensorValY; 
  
}else{
; //dont do shi.
 // Serial.println(delbutton);
delay(1000);
    } 

now, when I turn either potentiometer, I get a response, and output I desire, but not while the device is just sitting there.

4527: <p>I have a pair of potentiometers, on the Arduino, the output seems to drift, by one or 2 digits, back and forth. I would prefer they were read as one fixed value, without variation when their knobs are not turning. You can see in the image, the first and second values are shifting back and forth slightly. How should I address this?</p>
<p>I'm thinking of making a standard deviation function, but then I thought that might be unnecessarily complex.</p>
<p><a href="https://i.stack.imgur.com/7Kisx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/7Kisx.png" alt="wandering pots" /></a></p>
<p>My code is that the 2 sensor values are setting points in an SVG polyline. Variance means extra points will get plotted, while the device waits for valid input.</p>
<p>How should I deal with the variance in potentiometer readings to avoid outputting noise?</p>
<p>Here's my source:</p>
<pre class="lang-cpp prettyprint-override"><code>/**Fucntion Defs**/
void svgHeader();
void svgFooter();
void polyLineBegin();
void polyLineEnd();

int Reset = 4;     //programmatically reset the arduino
int potX = A0;
int potY = A1;

int sensorValX = 0;
int sensorValY = 0;
int oldX = 0;
int oldY = 0;

int del = 2;       //a button on pin 2 for deleting drawn content
int delbutton = 0;
void setup() {
  Serial.begin(9600);
  pinMode(del, INPUT);
  svgHeader();
  polyLineBegin();
  delay(1000);
}

void loop() {
  // close the svg and start over
  if (digitalRead(del) == LOW ) {
    delbutton = 1;
    polyLineEnd();
    svgFooter();
    delay(5000);
    digitalWrite(Reset, LOW);
    digitalWrite(Reset, HIGH);
    svgHeader();
    polyLineBegin();
  } else {
    delbutton = 0;
    //Serial.println(delbutton);

  }

  //dont draw if its not changing
  sensorValX = analogRead(potX);
  sensorValY = analogRead(potY);
  if (sensorValX == oldX &amp;&amp; sensorValY == oldY) {
    ; //dont do shi.
  } else {
    Serial.print(sensorValX);
    Serial.print(&quot;,&quot;);
    Serial.print(sensorValY);
    Serial.print(&quot;,&quot;);
    oldX = sensorValX;
    oldY = sensorValY;
    // Serial.println(delbutton);
    delay(1000);
  }
}
/**Functions**/

void polyLineBegin() {
  Serial.println (&quot;&lt;polyline points=\&quot;&quot;); //x,y points go here

}

void polyLineEnd() {
  Serial.println (&quot;0, 0\&quot; stroke=\&quot;red\&quot; fill=\&quot;transparent\&quot; stroke-width=\&quot;5\&quot;/&gt;&quot;);  //a dirty hack to avoid dealing with 1 last trailing comma.

}

void svgHeader() {
  Serial.println(&quot;&lt;svg width=\&quot;1023\&quot; height=\&quot;1023\&quot; version=\&quot;1.1\&quot; xmlns=\&quot;http://www.w3.org/2000/svg\&quot;&gt;&quot;);
};

void svgFooter() {
  Serial.println (&quot;&lt;/svg&gt;&quot;);
};
</code></pre>
 68310: <p>I think you <em>should</em> handle the trailing comma within the Arduino by using leading commas instead, but omit the first leading comma using a flag to indicate the first coordinate:</p>
<pre class="lang-cpp prettyprint-override"><code>bool firstCoordinate;

void loop()
{
    . . .
    if (xVariance &gt;= threshold || yVariance &gt;= threshold)
    {
        if (!firstCoordinate)
        {
            Serial.print(&quot;,&quot;);
            firstCoordinate = false;
        }
        Serial.print(sensorValX);
        Serial.print(&quot;,&quot;);
        Serial.print(sensorValY);
        oldX = sensorValX;
        oldY = sensorValY;
    }
    . . .
}

void polyLineBegin()
{
    Serial.println(&quot;&lt;polyline points=\&quot;&quot;); //x,y points go here
    firstCoordinate = true;
}


void polyLineEnd()
{
    // Clean line ending.
    Serial.println(&quot;\&quot; stroke=\&quot;red\&quot; fill=\&quot;transparent\&quot; stroke-width=\&quot;5\&quot;/&gt;&quot;);
}
</code></pre>
<p>Alternatively, the first coordinate could be added to the line when it begins, then there is no need for a flag:</p>
<pre class="lang-cpp prettyprint-override"><code>void polyLineBegin()
{
    Serial.println(&quot;&lt;polyline points=\&quot;&quot;); //x,y points go here
    Serial.println(sensorValX);
    Serial.println(&quot;,&quot;);
    Serial.println(sensorValY);
}

void loop()
{
    . . .
    if (xVariance &gt;= threshold || yVariance &gt;= threshold)
    {
        Serial.print(&quot;,&quot;);
        Serial.print(sensorValX);
        Serial.print(&quot;,&quot;);
        Serial.print(sensorValY);
        oldX = sensorValX;
        oldY = sensorValY;
    }
    . . .
}
</code></pre>
<p>The thresholding technique is great, but, as an aside, I recently discovered this <a href="https://github.com/MajenkoLibraries/Average/blob/master/src/Average.h" rel="nofollow noreferrer">excellent statistics library by Majenko</a> while working on a non-blocking averaging function for Arduino and looking at converting it into a class. I found that that library already does what I want.</p>
 4143: <p>Some methods for &quot;dealing&quot; with the noise:</p>
<ol>
<li>Take multiple samples. Calculate the average (&quot;mean&quot;). Discard the decimal. This is &quot;oversampling&quot; along with resolution reduction (but reduction of the now increased oversampled resolution).</li>
<li>Take multiple samples. Find the most common value (&quot;mode&quot;).</li>
<li>Divide the value by 2 (or 4 if needed). This reduces the range of values and the sampled resolution but discards the noise portion.</li>
</ol>
<p>The problem you are suffering from is &quot;quantisation noise&quot;. This is because an analog signal and a digital signal are not completely compatible. The ADC converts the analog signal into a digital value by successively comparing it to smaller and smaller voltages in steps (&quot;quanta&quot;). There is a limit to how small the steps can get (the resolution), but an analog voltage doesn't adhere to those steps, it will always be somewhere between the final two steps. Sometimes it will be detected as the lower step, sometimes the upper step. By taking multiple samples and averaging them you are estimating how close to one step or the other the value is and &quot;refining&quot; the result. This is &quot;oversampling&quot; and is used to increase the resolution of the ADC at the cost of sample speed.</p>
<p>With those extra samples you can either say &quot;It's this much closer to the upper step&quot; (take the &quot;mean&quot;), or say &quot;It's more likely to be nearer the upper step&quot; (take the &quot;mode&quot;) to try and &quot;fix&quot; the value to one of the steps.</p>
 40318: <p>Remove the analog component from your device.</p>
<p>Use a rotary encoder.</p>
<p>It will sense the knob rotation digitally.</p>
<p>One huge advantage is that you can easily select the sensitivity of the input. One click can advance the counter by .001 just as easily as advancing by a 1000.</p>
<p><a href="https://duckduckgo.com/?q=rotary+encoder&amp;iar=images&amp;iax=images&amp;ia=images" rel="noreferrer">https://duckduckgo.com/?q=rotary+encoder&amp;iar=images&amp;iax=images&amp;ia=images</a></p>
<p><img src="https://cdn.hackaday.io/images/3405091516957678589.jpg" alt="enter image description here" /></p>
 4527: <p>ok, once I got the idea I would need to do it in code, it became simpler to solve. In looking around the terms of quantization, the term thresh-holding came up, and I realized how to solve the problem.</p>
<p>I created a threshold value, and compared the difference between old and new variables.</p>
<pre><code>int threshold =3;
int xVariance = abs(sensorValX-oldX);
int yVariance = abs(sensorValY-oldY);
if (xVariance &gt;=threshold &amp;&amp; yVariance &gt;= threshold ||xVariance &gt;=threshold || yVariance &gt;=threshold){
  Serial.print(sensorValX);
  Serial.print(&quot;,&quot;);
  Serial.print(sensorValY);
  Serial.print(&quot;,&quot;);
 oldX=sensorValX;
 oldY=sensorValY; 
  
}else{
; //dont do shi.
 // Serial.println(delbutton);
delay(1000);
    } 
</code></pre>
<p>now, when I turn either potentiometer, I get a response, and output I desire, but not while the device is just sitting there.</p>
