I am now attempting to get a more accurate result by finding the average of multiple temp results. The issue I am having is, when I find the average I am getting undefined and NaN from the output of the variable that I supposedly saved the average to. The issue I think is stemming from the way I am saving the temp value. Also, besides taking the average results, is there another way I could achieve a more accurate temperature?
My code:
function getResTempValue(x) {
var temp;
var avgTemp;
for (var i=0;i<x;i++)
{
var vOut = E.getAnalogVRef() * analogRead(A0); // if you attached VOUT to Ao
var vZero = 0.4;
var tCoeff = 19.5 / 1000;
var tempinc = (vOut - vZero) / tCoeff;
var tempinf = tempinc * (9 / 5) + 32;
temp = temp + tempinc;
console.log(temp);
if (i==x) {
avgTemp = temp / x;
temp = temp / x;
}
console.log(i);
}
console.log("Temp: " + temp + " Avg temp: " + avgTemp);
return temp.toFixed(2);
}
//IE getResTemp(5);
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.
I am now attempting to get a more accurate result by finding the average of multiple temp results. The issue I am having is, when I find the average I am getting undefined and NaN from the output of the variable that I supposedly saved the average to. The issue I think is stemming from the way I am saving the temp value. Also, besides taking the average results, is there another way I could achieve a more accurate temperature?
My code: