Help in logging accelerometer values

I have written a code to log accelerometer values from RuuviTag using Espruino. I am able to save the values in a file using Storage class. This is the code I have written.

var period = 10; // milliseconds
var st = require(“Storage”);
var myFile = st.open(“data.csv”,“a”);
const length = 200; // number of records
var ruuvi = require(“Ruuvitag”);
var records = [];

function log() {
ruuvi.setAccelOn(true);
var date = new Date();
var isoDateTime = new Date(date.getTime() - (date.getTimezoneOffset() * 60000));
var line = isoDateTime + “,” + ruuvi.getAccelData().x +"," + ruuvi.getAccelData().y + “,” + ruuvi.getAccelData().z + “\n”;
if(records.push(line) > length){
records.splice(0,1);
}
ruuvi.setAccelOn(false);
}

function onInit() {
E.setTimeZone(5.5);
setInterval(log, period);
myFile.write(“Sl No,Time,X,Y,Z\n”);
}

function getData() {
records.forEach( function(d,i,o) {
myFile.write(i+1+ “,” + d);
});
records.splice(0, records.length);
}

The problem I am facing is that sometimes the program crashes. Also the values recorded for the first time after calling the getData() function don’t get updated and stay the same. Also the number of values recorded remains the same even though I have specified the length.

Can someone please help me as to what I should do in order to rectify these errors?

Hello,

My hunch would be that crash is related to concurrent access to records, interrupt here

if(records.push(line) > length){
  records.splice(0,1);
}

and getData here

records.forEach( function(d,i,o) {
    myFile.write(i+1+ “,” + d);
});

However I don’t know enough of Espruino internals to be sure. Also, the program will run out of memory at some point as the file grows.

The file is opened in append more, the contents should not be updated by program as far as I can tell. Only new records can be appended.

I do not understand this issue, are you seeing length records, less or more?

I get less than the length

Can you verify somehow that you’re not running out of memory?

I actually wanted to know if this was possible??

I don’t know a lot about the Espruino specifics, but it seems to be possible to print the free memory:https://github.com/espruino/Espruino/wiki/Memory-Management