First approach to tare a scale with multiple calls and median filter.
var SCK = D23,
DOUT = D24;
scale = require('HX711').connect({
sck: SCK,
miso: DOUT,
});
function filterMedian(values) {
var samples = values.length,
median;
var i = Math.floor(samples / 2);
values.sort(function(a, b) { return a - b; });
if (samples % 2) { //even
median = (values[i] + values[i + 1]) / 2;
} else { // odd
median = values[i];
}
return median;
}
function scaleTare(samples, scale, cb) {
var values = new Array(samples);
var tare = function(x) {
if (!scale.miso.read())
values[x] = scale.readRaw();
if (++x === samples) {
scale.zero = filterMedian(values);
cb();
}
};
function setIntervalX(samples, delay, cb) {
var x = 0;
var id = setInterval(function() {
cb(x);
if (++x === samples) {
clearInterval(id);
}
}, delay);
}
// 100ms deleay is needed when HX711 is in 10Hz mode to read another value
// maybe add sps value to options like sps:10 = 100ms, sps:80 = 12,5ms
setIntervalX(samples, 100, tare);
}
setTimeout(() => { scaleTare(10, scale, () => { console.log("scale is now tared"); }); }, 1E3);
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.
First approach to tare a scale with multiple calls and median filter.
Any hints and tweaks are welcome.