You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • It's actually pretty easy - I used it for the robot demo in the KickStarter video and I may have forgotten to post the code up. Basically you treat Bluetooth like any other serial device:

    For instance to handle colours:

    Bluetooth.on('data', function(d) { 
      if (d[0]=="!" && d[1]=="B") {
        var b = d.substr(2,2);
        if (b[0]=="1") LED1.write(0|b[1]);
        if (b[0]=="2") LED2.write(0|b[1]);
        if (b[0]=="3") LED3.write(0|b[1]);
        // 4
      if (d[0]=="!" && d[1]=="C") {
        analogWrite(LED1, d.charCodeAt(2)/800);
        analogWrite(LED2, d.charCodeAt(3)/256);
        analogWrite(LED3, d.charCodeAt(4)/300);

    or for the robot itself I did:

    Bluetooth.on('data', function(d) {
      if (d[0]=="!" && d[1]=="B") {
        var b = d.substr(2,2);
        var m = 0;
        if (b[1]=="1") {
          if (b[0]=="5") m=5;
          if (b[0]=="6") m=10;
          if (b[0]=="7") m=9;
          if (b[0]=="8") m=6;

    Because of the way BLE works, you'll get one big chunk of data received in one go, so you don't have to worry about detecting start and end - each time the handler is called it'll have a complete command in it.

    However: it's not quite that simple. By default, the actual JS interpreter is running on that Bluetooth UART, and will intercept any characters and try and execute them as JS before your code gets to see them.

    You need to force the console to stay on some other device, maybe with LoopbackA.setConsole(1). But, once you do that you can no longer program Puck.js until something like Bluetooth.setConsole() is called, or you reset it!

    So probably what you want is simply:

    setWatch(function() {
    }, BTN, {repeat:true, edge:"rising", debounce:50});

    That will move the console out of the way temporarily, when you press a button. So now when everything is running you can connect the Adafruit app, press the Puck's button to move the console out the way, and it all works fine.

    When you disconnect and reconnect with the PC, the console will go back to being on Bluetooth so you can program it as normal!


Avatar for Gordon @Gordon started