You are reading a single comment by @azer and its replies. Click here to read the full conversation.
  • hello,

    I've just make corrections suggested. It's working. But the colors jumps from color to another one. How can I make a fading animation between colors ?

    // variables to set
    var wifiSSID = "xxxx";
    var wifiPass = "xxxx";
    
    // do not touch after this line
    var LED1 = D5;
    var LED2 = D14;
    var LED3 = D12;
    var button = D4;
    var colFrom = [0,0,0];
    var colTo = [0,0,0];
    var colFade = 0;
    var powerState = "off";
    pinMode(button, 'input_pulldown');
    
    function setLeds() {
      colFade += 0.5;
      if (colFade>=1) {
        colFade = 0;
        colFrom = colTo;
        var c = E.HSBtoRGB(Math.random(),1,1);
        colTo = [ (c&0xFF)/256, ((c>>8)&0xFF)/256, ((c>>16)&0xFF)/256 ];
      }
    
      analogWrite(LED1,colFrom[0]*(1-colFade) + colTo[0]*colFade, {soft:true});
      analogWrite(LED2,colFrom[1]*(1-colFade) + colTo[1]*colFade, {soft:true});
      analogWrite(LED3,colFrom[2]*(1-colFade) + colTo[2]*colFade, {soft:true});
    }
    
    function power(state) {
      if (state === "on") {
        powerState = "on";
        setInterval(setLeds, 500);
      }
      if (state === "off") {
        powerState = "off";
        clearInterval();
        analogWrite(LED1, false);
        analogWrite(LED2, false);
        analogWrite(LED3, false);
      }
    }
    
    function startServer() {
      setWatch(function(){
        if (powerState === "off") {
          power("on");
          powerState = "on";
        } else {
          if (powerState === "on") {
            power("off");
            powerState = "off";
          }
        }
      }, button, {repeat:true, edge:"rising", debounce:50});
    }
    
    function onInit() {
      var wifi = require('Wifi');
      wifi.stopAP();
      wifi.connect(wifiSSID, {password:wifiPass}, function(err, data){
        if (err) {
          print('Error while connecting to access point' + wifiSSID);
          return;
        }
    
        print("connected? err=", err, "info=", wifi.getIP());
        startServer();
      });
    }
    power('off');
    
  • @azer

    Have you attempted this tutorial yet?
    http://www.espruino.com/Individually+Add­ressable+LEDs

    It has a fading effect between colors, similar to that which you might be seeking.

    @Gordon has spent a considerable amount of his valuable time coding a framework for you to hang your work on. It would be prudent to blend the above tutorial example into your work to get the desired effect you are after.

    It is difficult for anyone to write a solution to an idea you have firmly cemented in your mind. A bit of study should provide the insight to a solution to that which you envision. I'm sure you will be proud of your own effort once you achieve success.

    “There are no secrets to success. It is the result of preparation, hard work and learning from failure.” - Colin Powell

About

Avatar for azer @azer started