waveform in prototype functions?

Posted on
  • I'm receiving negative values when attempting to use this code:

    TempSensor.prototype.getTempValue = function () {
      var p = this.aPort; //<-- equals A0 
    
      var w = new Waveform(128/* your buffer size */,{doubleBuffer:false, bits:16});
      var first = true;
    
          var myarr = w.buffer;
          myarr.sort();
          // cut off first and last 2 elements
          var view = new Uint16Array(myarr.buffer, 2/*sizeof(uint16)*/*2, myarr.length-4);
          var vOut = E.getAnalogVRef() * E.sum(view)/(view.length*65536); // if you attached VOUT to Ao
          var vZero = 0.4;
          var tCoeff = 19.5 / 1000;
          var tempinc = (vOut - vZero) / tCoeff;
          var tempinf = tempinc * (9 / 5) + 32;
          console.log("Temp in C: " + tempinc);
          console.log("Temp in F: " + tempinf);
          //console.log(E.sum(view)/(view.length*6­5536));
    
        w.startInput(p,2000 /* Samples per second */);
    
    };
    
  • Could you post up the contents of myarr.buffer, the contents of view and the actual results you're getting?

    As far as I can see, if you get an analog reading less than 0.4 (so 0.4*3.3 = 1.32v) it's going to give you a negative temperature in degrees C.

  • I added:

    console.log("Temp in C: " + tempinc);
          console.log("Temp in F: " + tempinf);
      
          console.log("myarr: " + myarr.buffer);
          console.log("view: " + view);
    

    output:

    Temp in C: -20.51282051282 Temp in F: -4.92307692307 myarr:
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0
    view:
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,­0,0,0,0

  • Outside of the prototype function I did, after adding an ifstatement, with success:

    var w = new Waveform(128/* your buffer size */,{doubleBuffer:false, bits:16});
    var first = true;
    setInterval(function() {
      if (!first) {
        var myarr = w.buffer;
        myarr.sort();
        // cut off first and last 2 elements
        var view = new Uint16Array(myarr.buffer, 2/*sizeof(uint16)*/*2, myarr.length-4);
        var vOut = E.getAnalogVRef() * E.sum(view)/(view.length*65536); // if you attached VOUT to Ao
        var vZero = 0.4;
        var tCoeff = 19.5 / 1000;
        var tempinc = (vOut - vZero) / tCoeff;
        var tempinf = tempinc * (9 / 5) + 32;
        console.log("Temp in C: " + tempinc);
        console.log("Temp in F: " + tempinf);
        //console.log(E.sum(view)/(view.length*6­5536));    
      } else first = false;
      // start it again
      w.startInput(A0,2000 /* Samples per second */);
    }, 200);
    
  • Oh, I just looked again - and you're just creating a Waveform, not doing anything, and then reading back the values. It's not really surprising the array is just full of 0?

    What about doing what's described on the waveform page, and using the finish event?

    var w = new Waveform(128);
    w.on("finish", function(buf) {  //<----------- this
      // your temperature reading code
    });
    w.startInput(A0,2000,{repeat:false});
    
  • Working code:

    TempSensor.prototype.getTempValue = function () {
      var w = new Waveform(128/* your buffer size */,{doubleBuffer:false, bits:16});
      var p = this.aPort;
    
      w.on("finish", function(buf) {
        var first = true;
    
          var myarr = w.buffer;
          myarr.sort();
          // cut off first and last 2 elements
          var view = new Uint16Array(myarr.buffer, 2/*sizeof(uint16)*/*2, myarr.length-4);
          var vOut = E.getAnalogVRef() * E.sum(view)/(view.length*65536); // if you attached VOUT to Ao
          var vZero = 0.4;
          var tCoeff = 19.5 / 1000;
          var tempinc = (vOut - vZero) / tCoeff;
          var tempinf = tempinc * (9 / 5) + 32;
          console.log("Temp in C: " + tempinc);
          console.log("Temp in F: " + tempinf);
    
          console.log("myarr: " + myarr.buffer);
          console.log("view: " + view);
          //console.log(E.sum(view)/(view.length*6­5536));
    
      });
    
      w.startInput(p,2000 /* Samples per second */);
    };
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

waveform in prototype functions?

Posted by Avatar for d0773d @d0773d

Actions