You are reading a single comment by @Cale and its replies. Click here to read the full conversation.
  • Slave Handler:

    var _interval, _spr, _zones = [B10,B13,B14,B15];
    function pwm(zone,brightness, Hz){// 0<brightness<1 Hz>0
      if((typeof _interval) !== "undefined"){
        clearInterval(_interval);
        var i = 0;
        while (_zones[i]){
          _zones[i].reset();
          i++;
        }
        _interval = undefined;
      }
      if(0>brightness<1&&0<Hz){
        _interval = setInterval(function(){
          digitalPulse(_zones[zone], 1, brightness * (1000/Hz));
        }, 1000/Hz);
      }
    }
    
    //power burst on the solenoid then bring the power down to reduce amp draw
    function ctrlPwm(y){
      _spr=y;
      pwm(_spr,0.9,60);
      setTimeout('pwm(_spr,0.5,40)',500);
    }
    
    //read voltage to send back to master controller and turn on requested solenoid
    function spCmd(cmd){
      var volt = analogRead('B1');
      switch(cmd){
        case "one":
          ctrlPwm(0);
          return "one on and volts "+volt;
        case "two":
          ctrlPwm(1);
          return "two on and volts "+volt;
        case "three":
          ctrlPwm(2);
          return "three on and volts "+volt;
        case "four":
          ctrlPwm(3);
          return "four on and volts "+volt;
        case "status":
          return "volts "+volt;
        case "off":
          return "all off and volts "+volt; 
        default:
          return "woops";
      }
    }
    
    SPI1.setup({sck:B3, miso:B4, mosi:B5});
    var nrf = require("NRF24L01P").connect(SPI1, B6, B7);
    function onInit() {
      nrf.init([0,0,0,0,1], [0,0,0,0,2]);
    }
    
    
    nrf.solenoidHandler = function() {
      while (this.getDataPipe()!==undefined) {
        var data = this.getData();
        for (var i in data) {
          var ch = data[i];
          if (ch===0 && this.cmd!=="") {
            var c = this.cmd;
            this.cmd = "";
            var nrf = this;
            // evaluate and return a result in the timeout, 
            //so that evaluation errors don't cause the slaveHandler
            //interval to get removed
            setTimeout(function() {
              print("...>"+c);
              var result = spCmd(c); // evaluate
              print("...="+result);
              // send the result back after a timeout
              setTimeout(function() {
                nrf.sendString(result);
              }, 500);
            }, 1);
          } else if (ch!==0) {
            this.cmd += String.fromCharCode(ch);
          }
        }
      }
    };
    
    
    onInit();
    setInterval(function() {
      nrf.solenoidHandler();
    }, 50);
    

    Response is:

    ...>one
    ...=one on and volts 0.05297932402
    Uncaught ReferenceError: "b" is not defined
     at line 1 col 73
    ...his.getData();for(var d in b){var a=b[d];if(a>==0&&this.cmd!...
                                  ^
    in function "solenoidHandler" called from line 1 col 21
    nrf.solenoidHandler();
                        ^
    in function called from system
    
About

Avatar for Cale @Cale started