You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • To make it easier to upload your code, it might be an idea to put all the calls to setInterval or setTimeout into an onInit function. They'll then only be called after you've typed save().

    You could also really speed up your median code I reckon. For starters you could use Float32Array (which is a bit more memory efficient too), and you could then use the 'views' and built-in sum - also the default compare function for sorting does what you want I think, so you can just call sort with no arguments:

    function MedianRead(n,Pin) {
      var myarr = new Float32Array(n);
      for(i=0; i<n; i++) myarr[i] = analogRead(Pin);
      myarr.sort();
      // cut off first and last 2 elements
      var view = new Float32Array(myarr.buffer, 4/*sizeof(float32)*/*2, n-4);
      console.log(E.sum(view)/view.length);
    }
    

    I've not tested it though...

    As a way to make Espruino respond a lot faster, you might also want to look at Waveforms.

    You could do something like:

    var w = new Waveform(128/* your buffer size */,{doubleBuffer:true, bits:16});
    w.on("buffer", function(myarr) { 
       myarr.sort();
       // cut off first and last 2 elements
       var view = new Uint16Array(myarr.buffer, 2/*sizeof(uint16)*/*2, n-4);
       console.log(E.sum(view)/(view.length*655­36));
    });
    w.startInput(Pin,2000 /* Samples per second */,{repeat:true});
    

    Waveforms allow you to do analog readings at a fixed frequency in the background - it'll then only call your JavaScript code when it has filled up a buffer full of data. All the rest of the time it'll be able to do other things.

    Only thing to note is it puts the data into a 16 bit value, so you can do exactly the same stuff but you have to divide by 2^16 (65536) at the end.

  • @Gordon the Waveform that you recommended will come in handy. I was looking for away to trigger the other sensor readings once the espruino receives the temp. The temp is needed first before I request the other sensor readings.

    I will respond back hopefully in a couple days with my results/issues.

About

Avatar for Gordon @Gordon started