• Very nice!

    Of course my first line of defense should be proper programming on my side. I really didn't want to do this (show my beginner programming..) but somehow this code below makes my Puck restart within a day. Driving me nuts..

    Basically a guarding program that check for the absence of light and the presence of a magnet. It puts the result in the bluetooth name. There is an extra feature that checks for the presence of a bluetooth name 'base'. At the moment it just adds a ">" to the front when it isn't there, eventually it should do a NRF.sleep() when it is out of reach of the base to save battery.

    It sets a message "LM:LM", the first two tell whether it has seen light or lost the magnet during the running time of the puck, the second pair shows whether it sees light now or whether the magnet is absent right now. Good news would be "--:--". (It also adds the time, free memory and battery for debugging.)

    I am assuming something is wrong with the function notifyBase(statusMessage), where I look for the base and set the advertisement. It is asynchronous I know that, is it being run on top of itself maybe, should I be catching errors?

    var firstLightAlarm; //keep track of whether there has been a light alarm
    var firstMagnetAlarm; //keep track of whether there has been a magnet alarm
    var timeStart, timeNow;
    clearWatch(); // just to make sure I don't keep stacking watches and intervals
    clearInterval();
    
    
    
    //check for light(light>.2) or loss of magnet (strength<10000)
    function doCheck() {
      var lightLevel = Puck.light();
      var seeLight = lightLevel > 0.2;
      var mag = Puck.mag();
      var magForce = Math.sqrt(mag.x * mag.x + mag.y * mag.y + mag.z * mag.z );
      var lostMagnet = magForce !== 0 && magForce < 10000;
      statusMessage = setStatus(seeLight, lightLevel, lostMagnet, magForce);
      notifyBase(statusMessage); // go set the advertisement as statusMessage
    }
    
    
    // construct a status message out of sensor values, history, time.
    //LM:L- means "Seen light and lost magnet in the past, at the moment seeing light, but have the magnet close.
    function setStatus(seeLight,lightLevel,lostMagnet­,magForce) {
      var statusMessage;
      var secondsRunning = Math.floor(getTime() - timeStart);
      firstLightAlarm = firstLightAlarm || seeLight;
      firstMagnetAlarm = firstMagnetAlarm || lostMagnet;
      statusMessage =
        (firstLightAlarm ? "L" : "-") +
        (firstMagnetAlarm ? "M" : "-") + ":" +
        (seeLight ? "L" : "-") +
        (lostMagnet ? "M " : "- ") +
        "T=" + Math.floor(secondsRunning/(60))+ //minutes since initialization
        //" L="+Math.floor(lightLevel * 500)+
        //" M="+Math.floor(magForce/100) + " "+ //watch out for growing string error
        " B="+Puck.getBatteryPercentage()+
        " FR=" + process.memory().free;
        console.log(statusMessage);
      return statusMessage;
    }
    
    
    // Set BLE advertisement if 'base' present. When base not present Puck would normally stop advertising It now just adds a > in front of the status.
    function notifyBase(statusMessage){
      NRF.findDevices(function(devices){
        var foundBase=false;
          for (var i = 0; i < devices.length; i++){
           var name = devices[i].name;
           if( name == "base"){ foundBase= true; break;}
          }
        if (foundBase){NRF.setAdvertising({},{name:­statusMessage});}
        else {NRF.setAdvertising({},{name:">" + statusMessage});}  //temporary, without base it should stay quiet
       },1000);
    }
    
    
    
    //main program, sets the 4s interval to do a check
    function onInit() {
      firstLightAlarm = false;
      firstMagnetAlarm = false;
      timeStart = getTime();
      digitalPulse(LED1,1,100);
      clearInterval(); // otherwise you run out of memory on many button presses
      setTimeout(function(){
        setInterval(function(){doCheck();}, 4000);
      },2000); // 2s timout to place Puck without triggering early alarm
    }
    
    //click button to re-initialise.
    setWatch(
      function () {
        onInit();}, BTN, { repeat:true, edge:'falling', debounce : 50 
      }
    );
    
    
    onInit();
    
About

Avatar for user79451 @user79451 started