• 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);
    
About

Avatar for Joe @Joe started