Hi - there is getTime() which is like millis() except that it returns seconds (but as a fractional number).
However the Arduino code keeps going around the loop and grabbing values every samplingInterval. In Espruino it probably makes more sense to use setInterval to run your code exactly every samplingInterval (whenever you need a value), and to then to use a callback whenever you have the result...
So for example you could try (untested!):
SEN0161.prototype.getScale = function (callback) {
var sensor = this;
var Offset = 0.00;
var readings = [];
var interval = setInterval(function() {
readings.push(analogRead(sensor.pin));
if (readings.length>=5) {
var voltage = E.getAnalogVRef()*E.sum(readings)/readings.length;
var pHValue = 3.5 * voltage + Offset;
clearInterval(interval);
callback({pHValue:pHValue,voltage:voltage});
}
, 20/*samplingInterval*/);
};
// ...
sensor.getScale(function(d) {
console.log(" Voltage: "+d.voltage.toFixed(2));
console.log(" pH value: "+d.pHValue.toFixed(2));
});
So whenever you want a value, that'll go away and will get 5 values (meanwhile other code can be doing other things), and it will then average them and return the result.
A few things I noticed that might help:
analogRead in Espruino returns a floating point value between 0 and 1, rather than an integer between 0 and 1023
Espruino is 3.3v, so you have to multiply by 3.3 rather than 5 for the voltage. In the code above I'm using E.getAnalogVRef() though, which actually measures the voltage based on an internal voltage reference.
If you want to output a value to 2 decimal places, just use value.toFixed(2) - console.log(x,2) will just return the value of x, and then 2
There's some really odd-looking filtering being done on the data that's received. I've just replaced it with an average using E.sum (which is fast, and looks tidier) which should at least give you a value to work with.
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.
Hi - there is
getTime()
which is likemillis()
except that it returns seconds (but as a fractional number).However the Arduino code keeps going around the loop and grabbing values every
samplingInterval
. In Espruino it probably makes more sense to usesetInterval
to run your code exactly every samplingInterval (whenever you need a value), and to then to use a callback whenever you have the result...So for example you could try (untested!):
So whenever you want a value, that'll go away and will get 5 values (meanwhile other code can be doing other things), and it will then average them and return the result.
A few things I noticed that might help:
analogRead
in Espruino returns a floating point value between 0 and 1, rather than an integer between 0 and 1023E.getAnalogVRef()
though, which actually measures the voltage based on an internal voltage reference.value.toFixed(2)
-console.log(x,2)
will just return the value ofx
, and then2