Example #1
0
board.on("ready", function() {
  var piezoInPin = new JF.Pin({
    pin: 'A0'
  });

  var leds = [
    new JF.Led(9),
    new JF.Led(10),
    new JF.Led(11),
    new JF.Led(12),
    new JF.Led(13),
  ];

  piezoInPin.on('data', function (level) {

    leds.forEach(function(led){
      led.off();
    });

    if(level > threshold) {
      var numOfLedsOn = parseInt(leds.length * (level/20), 10);
      console.log('leds:', numOfLedsOn, 'level ->', level);
      for (var i = 0; i < numOfLedsOn; i++ ) {
        if(leds[i]){
          leds[i].on();
        }
      }
    }
  });
});
Example #2
0
function boardIsReady() {
  /* jshint validthis:true */
  var tst = new five.Pin(cfg);
  tst.query(function (state) { console.log(state); });
  // this.pinMode(cfg.pin, five.Pin.INPUT);
  tst.on('data', dataCallback);
  tst.io.analogRead(tst.pin, readCallback1);
  five.Pin.read(tst, readCallback2);
}
  primus.on('connection', function(spark) {
    var gameState;
    console.log('connection:\t', spark.id);

    // Store connections based on spark id.
    // Retrieve it if it already exists
    if (!connections[spark.id]) {
      connections[spark.id] = {
        gameState: new GameState(spark, GAME_TIMER_LIMIT)
      };
    }
    gameState = connections[spark.id]["gameState"];

    // --------------------------------------------
    // Set up sensors
    // --------------------------------------------

    snowman.on("high", _.throttle(function() {
      if (!gameState.isGameOver()) {
        // Snow man is hit!
        gameState.incrementScore();

        var data = {
          type: "score",
          message: {
            score: gameState.score
          }
        };

        spark.write(JSON.stringify(data));
      }
    }, SENSOR_THROTTLE_TIME));


    // Send light data
    photoResistor.on("read", function(value) {
      var normVal = helper.inRange(value, LIGHT_SENSOR_MIN, LIGHT_SENSOR_MAX, LIGHTNESS_MIN, LIGHTNESS_MAX);
      var data = {
        type: "light",
        message: {
          lightVal: normVal
        }
      };

      // console.log("light:", normVal, "(", value, ")");
      if (normVal < LIGHT_ON_THRESHOLD) {
        data.message.lowLight = true;
      } else {
        data.message.lowLight = false;
      }

      spark.write(JSON.stringify(data));
    });

    // --------------------------------------------
    // Read data sent from browser
    // --------------------------------------------

    spark.on('data', function(data) {
      var messageType = data["type"];
      var message = data["message"];

      console.log(data);

      if (messageType === "gameState") {
        if (message === "init") {
          gameState.init(spark);
        } else if (message === "start") {
          gameState.start(spark);
        } else if (message === "restart") {
          gameState.restart(spark);
        }
      }
    });
  });