-
• #2
Number.toFixed() returns a string with the specified format.
What you would want to do is:
var tfixed = temp.toFixed(2); console.log("Temp: " + tfixed); return temp; //we probably want to return the number, not a string
or
console.log("Temp: " + temp.toFixed(2)); return temp;
-
• #4
DrAzzy, thanks for your help. Gordon, your right the code never worked. Which explains why I was getting erratic values from my sensor readings. Now, my sensor readings are stable.
-
• #5
Code that works:
function onInit() { var cmd=""; Serial1.setup(9600/*baud*/); Serial1.onData(function (e) { console.log("RX Data: " + e.data); }); function getResTempValue(x) { var temp=0; var avgTemp=0; var tfixed=0; 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 + tempinf; console.log(temp); console.log(i); } temp = temp / x; tfixed = temp.toFixed(2); console.log("Temp: " + tfixed); return temp; } getResTempValue(10); } onInit();
Life happened so I put my project to the side for a couple of months. I decided to pick the project back up today and updated the firmware on the Espruino. By the way I really like the new GUI of the IDE :-)
The issue I am having is toFixed(2) doesn't seem to get the decimal place to only two decimal places. toFixed(2) seemed to work before I took a break from my project.
I hit the Send to Espruino button. I get the following output.
CODE: