You are reading a single comment by @psc1988 and its replies. Click here to read the full conversation.
  • I have a little problem that I'm stuck on.

    What I basically want to do is:

    1. with a short press of the Puck.js <0,3s a Bluetooth connection to the MDBT42Q should be established >>> works.
    2. directly after the connection, the accelerometer should execute its function and switch various pins on the MDBT when the threshold values are exceeded >>> works
    3. when pressing the Puck.js again < 0,3s the Bluetooth connection shall be disconnected again.

    When I upload the code it either doesn't work at all or once and then not anymore.

    I get following in the Console:

    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: Unhandled promise rejection: Error: BLE task 6 is already in progress
    Uncaught Error: BLE task 6 is already in progress
     at line 1 col 14
    k.disconnect()
                 ^
    in function "disconnect" called from line 1 col 17
    uart.disconnect();
                    ^
    in function called from system
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connected
    Uncaught Error: Unhandled promise rejection: Not connecte
    

    Here the code:

    var log = print;
    var uart;
    var l;
    var connected = false;
    var avrX = 0;
    var avrY = 0;
    var accZero = Puck.accel().acc; // get the base value when we start up
    
    
    
    //BLE Connect
    
        function flag(e) {
      
        var l = e.time-e.lastTime;  
        if ((l < 0.3) && (connected == false)){
        log("Sending command");
        log("Searching for device...");
        NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }]         }).then(function(device) {
        log("Connecting...");
        device.on('gattserverdisconnected', function() {
        });
        return require("ble_uart").connect(device);
        }).then(function(u) {
        log("Connected");
        digitalPulse(LED3, 1, 300);
        uart = u;
        connected = true;
        });}
    
          
        if ((e.time - e.lastTime < 0.3) && (connected == true) ){
        setTimeout(function(device) {
        uart.disconnect();
        log("Disconnected");
        connected = false;
        }, 1000);}
    
    
        }
        setWatch(flag, BTN, {repeat:true, edge:"falling"});
    
    
    
    
    ///Accelerometer
    
    
        Puck.accelOn();
    
    Puck.on('accel', function(data) {
      avrX = (avrX*0,99) + 0.01*(data.acc.x - accZero.x);
      avrY = (avrY*0,99) + 0.01*(data.acc.y - accZero.y);
    ///log (avrX, avrY);
    
       if (connected == true) {
    
           if (avrX < 90){
           uart.write("digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");}
           if (avrX > 105){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");}
           if (avrY < 90){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");}
           if (avrY > 105){
           uart.write("digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");}
    
           if (avrX > 90 && avrX < 105 && avrY > 90 && avrY < 105){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D31, 0), digitalWrite(D27, 0), digitalWrite(D28, 0),                digitalWrite(D29, 0) ;\n");}
       }
    
    
    });
    
    
About

Avatar for psc1988 @psc1988 started