-
• #4
ok - use search to find all conversation on this topic like this
http://forum.espruino.com/search/?q=analog+type:conversation+inTitle:true+sort:date
-
• #5
let us know which topic was helpful :-)
-
• #6
I guess the easiest is just:
function analogReadAvr(p) { var n=0,i=100; while (i--) n+=analogRead(p); return n/100; }
But you can do other things - for example reading in the background using
Waveform
, and/or using.sort
to do median filtering. -
• #7
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; };
-
• #8
Declaring / Initializing i and median in the function
var i, median;
would be a healthy thing... otherwise some things may get messed up... -
• #9
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; };
hi, I want to write code for analogReads sampling, in C i use for loop, I want to make let say 50-100 reads before I print them to console or to display, then can make analogRead a bit more precise as the readings floats a bit when use just simple analogRead function and timer Interval,
I appreciate some advice in this case