• Declaring / Initializing i and median in the function var i, median; would be a healthy thing... otherwise some things may get messed up...

  • Thanks, so this will ensure that not messing up global vars with the same name.

    analogReadMedian = function(p, sampleCount) {
          var i, median;
          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