A LED class might make it easier for a beginner like me to start with Espruino. In all, loving Espruino so far. Need to learn Javascript to fully appreciate it though.
/* This example works by dividing 1 second into 10 ms time intervals and increasing/decreasing the led's analogWrite output by .01 per interval, to produce the fade-in and fade-out effects.
*/
var ledPower =0;
var ledPin = D15;
function fadeIn(callback){
//start fadeIn effect should take 1 second to finish.
var result = setInterval(function(){
analogWrite(ledPin, ledPower);
ledPower+= 0.01;
if(ledPower >= 1){
clearInterval(result);
return;
}
}, 10);
//set the fade-out effect to start after 1 second.
setTimeout(callback, 1000);
}
function fadeOut(){
var result = setInterval(function(){
analogWrite(ledPin, ledPower);
ledPower-= 0.01;
if(ledPower <= 0){
clearInterval(result);
return;
}
}, 10);
}
setInterval(function(){
fadeIn(fadeOut);
} , 2000);
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.
I have got it to work(sort of), but I am not happy with it. It feels like there is/should be a better way to do it than below.
While googling I found this jhonny-five example, which is quite nice. https://github.com/rwaldron/johnny-five/blob/master/docs/led-fade.md
A LED class might make it easier for a beginner like me to start with Espruino. In all, loving Espruino so far. Need to learn Javascript to fully appreciate it though.