Avatar for Joe

Joe

Member since Aug 2017 • Last active Aug 2017
  • 1 conversations
  • 13 comments

Arduino/Raspberry pi enthusiast and tinkerer.

Most recent activity

  • in Puck.js
    Avatar for Joe

    Also, for the unhandled promise, can you try changing characteristic.writeValue(val); to return ?characteristic.writeValue(val);? I think that'll be the problem - looks like all Espruino's docs need updating as none of them mentioned that.

    I'll give it a try and update.

  • in Puck.js
    Avatar for Joe

    Update: after it disconnects on its own, I get the 3 LEDs of death followed by the red LED flashing once.

  • in Puck.js
    Avatar for Joe

    Hi Gordon,

    I have a piece of code with a for loop (loops only 10 times to get lower, avg and upper bounds of light) inside an interval that outputs to the console - see print screen. It keeps working if it's not printing anything, but sometimes I need to either re-upload or switch off the bulb and then back on. You can also see that there is also a promise that wasn't caught which should have been already caught.

  • in Puck.js
    Avatar for Joe

    I had a try with some code with an interval of 30 seconds and coded some behaviour that allows some light tolerance so that it does not change back and forth all the time. I noticed that sensing light has some variance and this can cause issues. One odd behaviour I notice from the Puck is that when viewing console output after some time, console looping stops and I need to reconnect again to the puck. Although I find that what works well is to switch the AWOX bulb off and then back on. Is this a sync issue?

    Ill copy my code so far here with some refactoring done.

    E.setBootCode();
    var downTime;
    var allowPush = true;
    var on = false;
    var len =30000;
    var autoSense = true;
    var flag = false;
    var isBusy = false;
    var brightness = 5;
    var toggleT = 0,toggleC = 0;
    //k->v pairs service to char
    var bulbBrightness = ["fff6fe25-469d-42bc-9179-b3a093f19032",­"d8da934c-3d8f-4bdf-9230-f61295b69570"];­ 
    var bulbtemp = ["fff6fe25-469d-42bc-9179-b3a093f19032",­"5b430c99-cb06-4c66-be2c-b538acfd1961"];­
    var bulbIO = ["33160fb9-5b27-4e70-b0f8-ff411e3ae078",­"217887f8-0af2-4002-9c05-24c9ecf71600"];­
    var bulbRGB = ["b882e31b-5096-43d1-90a9-edbf95073337",­"74532143-fff1-460d-8e8a-370f934d40be"];­
    function setLed(led, duration, callback) {
      if (duration === undefined) {
        duration = 10;
      }
      digitalPulse(led, 1,[duration]);
      setTimeout(function(){
        digitalWrite(led, false);
        if (callback) {
          callback();
        }
      }, duration);
    }
    
    function alert(led, onDuration, gap) {
      if (onDuration === undefined) {
        onDuration = 50;
      }
      if (gap === undefined) {
        gap = 100;
      }
    
      setLed(led, onDuration);
      setTimeout(function(){ setLed(led, onDuration); }, gap);
      setTimeout(function(){ setLed(led, onDuration); allowPush = true; }, gap * 2);
    }
    
    function onUp() {
      const delta = Date().ms - downTime;
      if (delta > 750 && delta < 5000) {
       toggle++;
        if(toggle==4){toggle=0;}
        if(toggle==0){colour = 0;}
        if(toggle==1){colour = 25;}
        if(toggle==2){colour = 75;}
        if(toggle==3){colour = 127;}
        alert(LED1);
        alert(LED2);
        alert(LED3);
        autoSense=true; 
        changeInterval(1,len);
        flag=false;
        setBulb(bulbtemp,colour);
        console.log("Press up");
      } else {
        alert(LED2);
      }
    }
    
    function onDown() {
      if (allowPush) {
        allowPush = false;
        downTime = Date().ms;
        autoSense=false; 
        isBusy=false;
        toggleC++;
        if(toggleC==5){toggle=0;}
        if(toggleC==0){rgb = "00-08-08";}
        if(toggleC==1){rgb = "FF-00-00";}
        if(toggleC==2){rgb = "00-FF-00";}
        if(toggleC==3){rgb = "00-00-FF";}
        if(toggleC==4){rgb = "00-00-00";}    
        setBulb(bulbRGB,rgb);
        setWatch(onUp, BTN, { edge: 'falling', debounce: 50 });
      }
    }
    function setBulb(service,val){
        var gatt;
      var ser = service[0];
      var ch = service[1];
      
    if (isBusy) throw new Error("Busy!");
                    isBusy = true;               NRF.connect("98:7b:f3:67:75:33").then(fu­nction(g) {
                      gatt = g;
                      return gatt.getPrimaryService(ser);
                    }).then(function(service) {
                      return service.getCharacteristic(ch);
                    }).then(function(characteristic) {
                          characteristic.writeValue(val);
                    }).then(function(){
                      gatt.disconnect();
                      console.log("Done in setting temp!");
                      isBusy = false; 
                    }).catch(function(e) {
                      console.log("Problem in setting colour temp: "+e);
                      if (gatt) gatt.disconnect();
                      isBusy = false;
                    });
    }
    setWatch(function() {
      onDown();
    }, BTN, { repeat:true, edge:"rising", debounce:50 });
    
    setInterval( function()
    {
    var noLight=true;
    var threshold = 0.35;
    var li=0.0,liUp=0.0,liLow=1.0,avg=0.0;
      for(i=0;i < 10;i++)
      {
        li = Puck.light();
        console.log(li);
        if(liUp < li){liUp = li;}
        if(liLow > li){liLow = li;}
        avg += parseFloat(li);
      }
        li = avg / 10;
        console.log((liLow) + " " + li + " " +(liUp));
        console.log(autoSense);
        noLight = (liUp < threshold);
          if(autoSense)
          {
            if(!flag)
            {
              setBulb(bulbIO,true);
              console.log("I see darkness - switching on!");
              digitalPulse(LED1,1,50,200,50,200,50);
              flag=true;
            }
            else
            {
              if(noLight)
              {
                 if(brightness <= 127)
                  {
                    console.log("Increasing brightness");
                    brightness++;
                    digitalPulse(LED2,50,1);
                    setBulb(bulbBrightness,brightness);
                  }
              }
              else
              {
                 if(brightness > 0 && brightness <= 127)
                 {
                     console.log("Reducing brightness");
                        brightness--;
                        digitalPulse(LED2,50,1);
                        setBulb(bulbBrightness,brightness);
                 }
              }
            }
          }
       }
     ,len);
    
  • in Puck.js
    Avatar for Joe

    I can definitely sympathise with you :)

    My code so far is not terribly very clean; there are opportunities to refactor some functions to make it cleaner. Once it's squeaky clean and behaves as it should, I'll post it here.

  • in Puck.js
    Avatar for Joe

    It's only doing this task, so no advertisement. It's weird. I ran it again, and I don't get it anymore.

    As a precaution, I have set the interval to be 30 seconds. I'll let you know if it kicks up a fuss :)

  • in Puck.js
    Avatar for Joe

    HI Gordon,

    So far, so good - the behaviour is much more stable, but I'm getting an "Out of memory while appending to array" warning as the interval keeps checking for light levels. Is there a way of doing this in JS or is it C level malloc stuff?

  • in Puck.js
    Avatar for Joe

    Hi Gordon,

    I have tested this over the few days and what I observed is that if there is a timeout error which is caught (sometimes weirdly it doesn't get caught) it will get into a busy state and nothing will happen. What I have to do (manually) is switch the light bulb off from the mains and then back on again and it will resume operation.

  • in Puck.js
    Avatar for Joe

    Gordon,

    Cheers for this, I have updated my puck and will give this a go. I'll let you know of any progress.

Actions