Avatar for azer

azer

Member since Oct 2016 • Last active Apr 2017
  • 5 conversations
  • 18 comments

Most recent activity

  • in General
    Avatar for azer

    Hi. I've already tried lot of tutos found over the net. But there is a lot of them for official espruino boards so it doesn't works on mine. I'm not a big pro developper but i have lots of boards programed by myself. But i never had to use rgb leds with this board.

    I've just tried to control my led using an Arduino Uno. It works very well. So while looking for fading on esp, i found that esp can't use hard pwm. That's why all my test codes doesn't works as expected.

    I have found a soft pwm fonction to do the same work. I'll try it.

    https://www.espruino.com/Software+PWM

    Thank you for all helps @Gordon

    • 14 comments
    • 5,247 views
  • in General
    Avatar for azer

    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');
    
  • in General
    Avatar for azer

    Hello,

    I use nodemcu. Ordinary I post in other boards topic. For this I have done a mistake.

    Sorry for that.

  • in General
    Avatar for azer

    Sorry for the late answer. I can't access to my personal documents from work.
    Here it i the full code:

    // 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;
    pinMode(button, 'input_pulldown');
    
    function setLeds() {
      colFade += 0.05;
      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";
        analogWrite(LED1, false);
        analogWrite(LED2, false);
        analogWrite(LED3, false);
      }
    }
    
    function startServer() {
      // start future http server
      var powerState = "off";
      power("off");
      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();
      });
    }
    
    E.on('init', onInit());
    save();
    
  • in General
    Avatar for azer

    There is something I missed. When the board starts, the led is off. Good. When I press the button, the led start directly and is not fading. The color jump from one to another one

    function setLeds() {
      colFade += 0.05;
      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});
    }
    
  • in General
    Avatar for azer

    it's working but is it possible to fade from 0 (black) to randoms colours automatically ?
    Exemple:

    • Push button --> start led sequence from 0, 0, 0 (R, G, B)
    • calculate new random values for each led
    • fade from 0, 0, 0 to new values a, b, c
    • When fade done, calculate new random values
    • fade from a, b, c to new values d, e, f

    I have a big logical problem for that. I presume that it is not so difficult but I can't really understand how it works !

  • in General
    Avatar for azer

    wow, i'll test this asap :)

  • in General
    Avatar for azer

    Hi, forgive me for my bad english.

    I'm trying to control a simple RGB led. My actual code is working, but I'd make it more ... fluid:

    function fade(led) {
      for (i=0; i<=255; i++) {
        digitalWrite(led, i);
      }
    }
    

    I call this funtion with the pin number of the led to control. But I'd like an async function that can run a "sequence" of colours while checking in the same time the state of a push button (to turn on/off).

    I'm really not a developper, but I know that someone could help me to finish my little led lamp project for my daughter :)

Actions