• Gordon -

    Works like a champ with the added delay. Here's the full code just for anybody looking to see it all together:

    // Are we busy?
    var busy = false;
    
    // The device, if we're connected
    var connected = false;
    
    // The 'tx' characteristic, if connected
    var txCharacteristic = false;
    
    // Function to call 'toggle' on the other Puck
    function sendToggle() {
      if (!busy) {
        busy = true;
        if (!connected) {
          NRF.requestDevice({ filters: [{ name: 'Puck.js ....' }] }).then(function(device) {
            return device.gatt.connect();
          }).then(function(d) {
            connected = d;
            console.log("Connected");
            return new Promise(r => setTimeout(r,500));
          }).then(function() {
            return connected.getPrimaryService("6e400001-b5­a3-f393-e0a9-e50e24dcca9e");
          }).then(function(s) {
            return s.getCharacteristic("6e400002-b5a3-f393-­e0a9-e50e24dcca9e");
          }).then(function(c) {
            txCharacteristic = c;
            busy = false;
            // Now actually send the toggle command
            sendToggle();
          }).catch(function(e) {
            console.log("error: ", e);
            connected=false;
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
            if (connected) connected.disconnect();
          });
        } else {
          txCharacteristic.writeValue("toggle()\n"­).then(function() {
            digitalPulse(LED2, 1, 500); // light green to show it worked
            busy = false;
          }).catch(function(e) {
            console.log("error: ", e);
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
            if (connected) connected.disconnect();
          });
        }
      }
    }
    
    // Call sendToggle when the button is pressed
    setWatch(sendToggle, BTN, { edge:"rising", debounce:50, repeat: true });
    

    Thanks for the help!

About

Avatar for Kymvaris @Kymvaris started