Avatar for user85064

user85064

Member since Jan 2018 • Last active Jan 2018
  • 1 conversations
  • 2 comments

Most recent activity

  • in ESP32
    Avatar for user85064

    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.

    /* 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);
    
    
  • in ESP32
    Avatar for user85064

    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();
    }
    
Actions