You are reading a single comment by @oesterle and its replies. Click here to read the full conversation.
  • @Gordon, Puck.light() is returning lower values than expected after the first call. Probably because I'm lighting up LED1.

    Short story: I think this can be fixed by extending the settling time delay in jswrap_puck_light().

    I type this in the console, and I get:

    >LED1.write(1);
    =undefined
    >LED1.write(0);
    =undefined
    >Puck.light()
    =0.15357589721
    >Puck.light()
    =0.55925130844
    >
    

    The last 0.559 reading is the most accurate.

    Here's my workaround that gives consistent light readings:

    var checkLight = function(){
      digitalWrite(LED1,0);
      analogRead(LED1);
      delay(50);
      light = analogRead(LED1) * NRF.getBattery() / (3 * 0.45);
      light = Math.round(light * 100);
      if (light < 20){
        console.log("  Seems pretty dark in here: " + light + "% brightness");
      } else {
        console.log("  Seems light in here: " + light + "% brightness");
      }
    };
    
    var delay = function(milliseconds){
      // Always try to use setTimeout or setInterval instead
      // of this function. This function, used for long periods
      // uses battery power!
      
      var timeout = new Date().ms + milliseconds;
      while(new Date().ms < timeout);
    };
    
    

    Like your C code for jswrap_puck_light(), I reset the LED1 pin, take a reading, wait, and take another (better) reading. But I wait 50 milliseconds, rather than 5 milliseconds.

About

Avatar for oesterle @oesterle started