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.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@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:
The last 0.559 reading is the most accurate.
Here's my workaround that gives consistent light readings:
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.