在以下代码中,NaN
获得了fulldata
。其他所有内容的值均为0
。我不知道为什么会这样。问题是什么?我需要改变什么?
var fulldata = 0;
var pulses = 0;
var flowrate; // liters per minute
var value = 0;
var d = new Date();
var liters = 0;
var gallons = 0;
var cost = 0;
var file = ('/var/lib/cloud9/data.txt');
var oldTime;
var oldAmount = b.readTextFile(file);
var totalCost = oldAmount * 0.00313;
app.listen(8090);
console.log('Server running on: http://' + getIPAddress() + ':8090');
var Sensor = "P8_19";
b.pinMode('P8_19', b.INPUT);
console.log(b.digitalRead('P8_19'));
b.digitalWrite('P8_19', b.HIGH);
oldTime = Date.now();
b.attachInterrupt('P8_19', pulseCounter, b.FALLING);
io.on('connection', function(socket) {
var tm = setInterval(function() {
var newTime = Date.now();
var timeDifference = (newTime - oldTime);
if (timeDifference > 1000) {
b.detachInterrupt('P8_19');
b.pinMode('P8_19', b.INPUT);
value = b.digitalRead('P8_19');
//console.log(timeDifference);
flowrate = (1000.0 / (timeDifference)) * ((pulses) / 4.5);
//console.log(flowrate);
oldTime = newTime;
liters = flowrate / 60;
console.log(liters + "L");
gallons = (liters * 0.264);
var newData = gallons;
console.log(gallons + "G");
cost = gallons * 0.00313;
fulldata += newData;
totalCost += cost;
console.log(fulldata);
console.log("$" + cost);
socket.emit('water', {'water': fulldata});
socket.emit('money', {'money': cost});
socket.emit('month', {'month': totalCost});
pulses = 0;
b.attachInterrupt('P8_19', pulseCounter, b.FALLING);
}
}, 500);
socket.on('disconnect', function() {
oldAmount = (oldAmount + fulldata);
b.writeTextFile(file, oldAmount);
clearInterval(tm);
});
});