• Hi Gordon,

    Thanks for the reply. I attach the code I am using using the light sensor and button sensor to result variables and switch the light to an off state. It might be that when len is too short as defined in the setInterval function, it kicks up the NRF error. I have tried longer len times and it resulted in crashes after several hours testing it.

    E.setBootCode();
    let downTime = undefined;
    let allowPush = true;
    let on = false;
    let len = 5000;
    let autoSense = true;
    let flag = false;
    function setLed(led, duration, callback) {
      if (duration === undefined) {
        duration = 10;
      }
    
      digitalPulse(led, 1,[duration]);
    
      setTimeout(function(){
        digitalWrite(led, false);
        if (callback) {
          callback();
        }
      }, duration);
    }
    
    function isDay(){
      var l=0;
      try{
        l = Puck.light();
      }catch(e)
      {
        console.log("Problem with light sensor: "+e);
      }
      if(l > 0.4)
      {
          return true;
      }
      return false;
    }
    
    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) {
       // toggleBeacon();
        alert(LED1);
        alert(LED2);
        alert(LED3);
        autoSense=true; 
        changeInterval(1,len);
        flag=false;
        setLight(false);
      } else {
        alert(LED2);
      }
    }
    
    function onDown() {
      if (allowPush) {
        allowPush = false;
        downTime = Date().ms;
        autoSense=false; 
        setWatch(onUp, BTN, { edge: 'falling', debounce: 50 });
      }
    }
    function setLight(isOn) {
      var gatt;
      try{
        NRF.connect("98:7b:f3:67:75:33").then(fu­nction(g) {
          gatt = g;
          return gatt.getPrimaryService("33160fb9-5b27-4e­70-b0f8-ff411e3ae078");
        }).then(function(service) {
          return service.getCharacteristic("217887f8-0af2­-4002-9c05-24c9ecf71600");
        }).then(function(characteristic) {
    characteristic.writeValue(isOn);
        }).then(function(){
          gatt.disconnect();
          console.log("Done!");
        });
        flag=isOn;
      }
        catch(e)
        {
          console.log("Problem: "+e);
        }
    }
    
    setWatch(function() {
      onDown();
    }, BTN, { repeat:true, edge:"rising", debounce:50 });
    
    
    setInterval( function()
      {
         try{
          console.log("Looping"); 
          if(autoSense)
          {
            if(!isDay() && !flag)
            {
              setLight(true);
              digitalPulse(LED1,1,50,200,50,200,50);
              console.log("I see darkness - switching on!");
            }
            else{
              if(flag && isDay()){
               setLight(false);
               digitalPulse(LED3,1,50,200,50,200,50);
               console.log("I see light - switching off!");
              }
            }
            changeInterval(1,60000);
          }
         }catch(e){console.log("Problem in Interval: " + e);}
       }
     ,len);
    

    I also added the catch after when writing to the smart bulb and it didn't catch it; the NRF error crept up and crashed the whole thing.

About

Avatar for Joe @Joe started