Skip to main content
add link
Source Link

If the problem is due to jittery pot readings, you may want to smooth the potval data sequence by code like the following, where rawreading stands for what you just read from the potentiometer's input line.

potval = alpha*potval + (1-alpha)*rawreading;

Set alpha to a number (eg 0.8 or 0.9 or 0.95 etc) between 0 and 1. alpha represents the fraction of the old value that you retain, and 1-alpha represents the fractional influence of the new raw reading. alpha closer to 1 causes smoother but slower response.

If you prefer integer arithmetic for faster or smaller code, then represent alpha as a ratio of integers. For example:

potval = (potval*12 + rawreading)/13;

The technique mentioned above is called exponential smoothing or exponential moving average.

If the problem is due to jittery pot readings, you may want to smooth the potval data sequence by code like the following, where rawreading stands for what you just read from the potentiometer's input line.

potval = alpha*potval + (1-alpha)*rawreading;

Set alpha to a number (eg 0.8 or 0.9 or 0.95 etc) between 0 and 1. alpha represents the fraction of the old value that you retain, and 1-alpha represents the fractional influence of the new raw reading. alpha closer to 1 causes smoother but slower response.

If you prefer integer arithmetic for faster or smaller code, then represent alpha as a ratio of integers. For example:

potval = (potval*12 + rawreading)/13;

If the problem is due to jittery pot readings, you may want to smooth the potval data sequence by code like the following, where rawreading stands for what you just read from the potentiometer's input line.

potval = alpha*potval + (1-alpha)*rawreading;

Set alpha to a number (eg 0.8 or 0.9 or 0.95 etc) between 0 and 1. alpha represents the fraction of the old value that you retain, and 1-alpha represents the fractional influence of the new raw reading. alpha closer to 1 causes smoother but slower response.

If you prefer integer arithmetic for faster or smaller code, then represent alpha as a ratio of integers. For example:

potval = (potval*12 + rawreading)/13;

The technique mentioned above is called exponential smoothing or exponential moving average.

Source Link

If the problem is due to jittery pot readings, you may want to smooth the potval data sequence by code like the following, where rawreading stands for what you just read from the potentiometer's input line.

potval = alpha*potval + (1-alpha)*rawreading;

Set alpha to a number (eg 0.8 or 0.9 or 0.95 etc) between 0 and 1. alpha represents the fraction of the old value that you retain, and 1-alpha represents the fractional influence of the new raw reading. alpha closer to 1 causes smoother but slower response.

If you prefer integer arithmetic for faster or smaller code, then represent alpha as a ratio of integers. For example:

potval = (potval*12 + rawreading)/13;