You are reading a single comment by @Chaapu and its replies. Click here to read the full conversation.
  • 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();
    }
    
About

Avatar for Chaapu @Chaapu started