You are reading a single comment by @psc1988 and its replies. Click here to read the full conversation.
  • Hi Gordon,

    first of all thank you very much for the information, I didn´t know that.

    I have two more questions about this. If I delete this lines:

      if (avrX > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");}
           if (avrX < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");}
           if (avrY > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");}
           if (avrY < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");}
    
    

    I don´t get any feedback in the console. If I let it in, I get this message, even if it works with the connecting/disconnecting thing:

    Uncaught Error: Unhandled promise rejection: Error: BLE task CHAR_WR is already in progress
    

    The other thing is I try to implement now the button and the part of the code for the button works in another program and also if the Accel is not in that one. What is the issue, when I want to use that additional code? Is it that uart.write is busy in some way? Sometimes the signal from the button receives the MDBT but after 2 or 3 times it stops or the LED on the MDBT does not go off again.

    Here the complete 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() {
        uart = undefined;
        connected = false;
        });
        return require("ble_uart").connect(device);
        }).then(function(u) {
        log("Connected");
        digitalPulse(LED3, 1, 300);
        uart = u;
        connected = true;
        log (connected);
        });}
    
        if ((l < 0.3) && (connected == true)){
    
        if (uart) uart.disconnect();
        uart = undefined;
        connected = false;
        log (connected);
    
        }
    
    
        }
        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("\x03\x10digitalWrite(LED1, 1), digitalWrite(D31, 1);\n");}
           if (avrX > 105){
           uart.write("\x03\x10digitalWrite(LED1, 1),digitalWrite(D27, 1);\n");}
           if (avrY < 90){
           uart.write("\x03\x10digitalWrite(LED1, 1),digitalWrite(D28, 1);\n");}
           if (avrY > 105){
           uart.write("\x03\x10digitalWrite(LED1, 1),digitalWrite(D29, 1);\n");}
    
           if (avrX > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");}
           if (avrX < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");}
           if (avrY > 90){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");}
           if (avrY < 105){
           uart.write("\x03\x10digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");}
    
     }
    
    });
    
    
    
    
    
    
    
         setWatch(function(c) {
    
           
           
         if (connected == true) {
    
           
         setTimeout(function () {
         {
         uart.write("\x03\x10digitalWrite(LED2,1)­, digitalWrite(D30, 1);\n");
         }
    
         }, 400);
         }
    
         }, BTN, { repeat: true, edge: 'rising' });
    
    
    
    
         setWatch(function(c) {
     
     
         if (connected == true) {
           
         {
         uart.write("\x03\x10digitalWrite(LED2,0)­, digitalWrite(D30, 0);\n");
            } 
         }
         
          }, BTN, { repeat: true, edge: 'falling' });
    
    
    
    
About

Avatar for psc1988 @psc1988 started