• 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;
    };
    
    
About

Avatar for MaBe @MaBe started