Hello,
I am new to Espruino and Javascript and am struggling with the asynchronous nature of it. For my first shot at Espruino, I tried to fade in and out an LED. I have done this before in Arduino, but struggling here. What's the best way to do it?
Here is what I did, but I realize this is wrong as both the fadeIn and fadeOut methods a running in parallel. Thanks in advance.
var ledPower = 0;
var ledPin = D15;
function fadeIn(){
var result = setInterval(function(){
if(ledPower >= 1){
clearInterval(result);
}
ledPower+= 0.001;
analogWrite(ledPin, ledPower);
}, 50);
}
function fadeOut(){
result = setInterval(function(){
if(ledPower <= 0){
clearInterval(result);
}
ledPower-= 0.001;
analogWrite(ledPin, ledPower);
}, 50);
}
while(true){
ledPower =0;
fadeIn();
fadeOut();
}
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.
Hello,
I am new to Espruino and Javascript and am struggling with the asynchronous nature of it. For my first shot at Espruino, I tried to fade in and out an LED. I have done this before in Arduino, but struggling here. What's the best way to do it?
Here is what I did, but I realize this is wrong as both the
fadeIn
andfadeOut
methods a running in parallel. Thanks in advance.