Thanks for naming the median filtering technic, using this now for several devices running Espruino. Works fine for me with sampleCount = 21.
analogReadMedian = function(p, sampleCount) {
var samples = Math.floor(sampleCount);
var analogValues = new Array(samples);
// read analog values into array
i = samples;
while(i--) analogValues[i] = analogRead(p);
//sort array, smalest first and largest last
analogValues.sort(function(a, b){return a-b;});
// set median
i = Math.floor(samples/2);
if ( samples % 2 ){ //even
median = (analogValues[i]+analogValues[i+1])/2;
} else { // odd
median = analogValues[i];
}
return median;
};
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.
Thanks for naming the median filtering technic, using this now for several devices running Espruino. Works fine for me with sampleCount = 21.