You can use a moving average with an interval of 8 or 16 samples .
I used the following code to get stable readings from a 10-bit encoder.
Using the dithered noise resolution is in fact improved to 1 1bits.
setInterval(function() {
enc_deg = sma16(degree(C1).toFixed(1)); // console.log("Encoder Angle[°] = "+enc_deg.toFixed(1));
}, 100 );
function degree( pin ) {
var ratio = 4.0;
var offset = 0.5;
var factor = 0.73333333333;
var phys = analogRead(pin) * 3.3; // *E.getAnalogVRef();
var meas = 360.0 * ( phys / factor - offset ) / ratio ;
console.log("Input[V] = "+phys.toFixed(3)+" Angle[°] = "+meas.toFixed(1));
return meas;
}
// ------------------------------------
// simple parametric moving averager
// ------------------------------------
function sma(period) {
var nums = [];
return function(num) {
nums.push(num);
if (nums.length > period) nums.splice(0,1);
var sum = 0; var n = period;
function sum_num (n) { sum+=Number(n); };
nums.forEach(sum_num);
if (nums.length < period) n = nums.length;
return(sum/n);
}
}
var sma16 = sma(16);
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.6
Input[V] = 0.496 Angle[°] = 15.9 Next reading = 15.9, SMA = 15.7
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.7
Input[V] = 0.498 Angle[°] = 16.1 Next reading = 16.1, SMA = 15.8
Input[V] = 0.497 Angle[°] = 16.0 Next reading = 16.0, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.496 Angle[°] = 15.9 Next reading = 15.9, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.8
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
You can use a moving average with an interval of 8 or 16 samples .
I used the following code to get stable readings from a 10-bit encoder.
Using the dithered noise resolution is in fact improved to 1 1bits.
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.6
Input[V] = 0.496 Angle[°] = 15.9 Next reading = 15.9, SMA = 15.7
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.7
Input[V] = 0.498 Angle[°] = 16.1 Next reading = 16.1, SMA = 15.8
Input[V] = 0.497 Angle[°] = 16.0 Next reading = 16.0, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.496 Angle[°] = 15.9 Next reading = 15.9, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7 Next reading = 15.7, SMA = 15.8
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.8
Input[V] = 0.494 Angle[°] = 15.6 Next reading = 15.6, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.8 Next reading = 15.8, SMA = 15.8
Input[V] = 0.495 Angle[°] = 15.7