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

    thanks for that help.

    What I found out is, if I delete the part with the accelerometer it works. I can connect and disconnect like it should be all the time.

    Did I made a mistake in the accelerometer section? I ask for
    if connection is true, uart.write.

    var log = print;
    var status = 0;
    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;
        status = 1;
        log (status);
        log (connected);
        });}
    
    
        if ((l < 0.3) && (status == 1)){ 
          
        if (uart) uart.disconnect();
        uart = undefined;
        connected = false;
        status = 0;
        log (status);
        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 (status == 1) {
    
           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){
           uart.write("digitalWrite(LED1, 0), digitalWrite(D31, 0);\n");}
           if (avrX < 105){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D27, 0);\n");}
           if (avrY > 90){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D28, 0);\n");}
           if (avrY < 105){
           uart.write("digitalWrite(LED1, 0),digitalWrite(D29, 0);\n");}
    
      }
          
      
    
    });
    
    
About

Avatar for psc1988 @psc1988 started