problem motion detection + midi

Posted on
  • hi,
    I am trying to send midi cc only on motion detection, but I get nothing coming and the terminl issue me an error: WARNING: SD ERR 0x11 (BUSY) (:835)

    I am using require("puckjsv2-accel-movement").on();­ and suspect I am using setservices twice then which could be the problem? but not really sure where to look at.

    here is the code:

    require("puckjsv2-accel-movement").on();­
    
    /// Sends a raw MIDI command
    function cmd(cmd, d1, d2) {
      NRF.updateServices({
        "03B80E5A-EDE8-4B33-A751-6CE34EC4C700": {
          "7772E5DB-3868-4112-A1A9-F2669D106BF3": {
            value: [0x80, 0x80, cmd, d1, d2],
            notify: true
          }
        }
      });
    }
    
    /// Send a 'control change' (0xB0) MIDI command
    function midisend(channel, controller, value) { cmd(0xB0+channel,controller,value); }
    
    
    
    function rescaleToMidiCC(value, oldMin, oldMax) {
      var newMin = 0;
      var newMax = 127;
      var rescaled = Math.round((value - oldMin) * (newMax - newMin) / (oldMax - oldMin) + newMin);
      rescaled = Math.min(127, Math.max(0, rescaled));
      return rescaled;
    }
    
    
    
    /// Turns the device into a MIDI controller
    function init(name) {
      //NRF.setSecurity({passkey:"123456", lesc:1, bond :1});
      NRF.setServices({
        "03B80E5A-EDE8-4B33-A751-6CE34EC4C700": { // MIDI
          "7772E5DB-3868-4112-A1A9-F2669D106BF3": {
            readable: true,
            writable: true,
            notify: true,
            value: [0x80, 0x80, 0x00, 0x00, 0x00]
              }
        }
      });
      NRF.setAdvertising([
        // Flags: LE Limited Discoverable Mode, BR/EDR Not Supported
        0x02, 0x01, 0x05,
        // Complete Local Name: PuckCC
        0x07, 0x09,
        name.charCodeAt(0),
        name.charCodeAt(1),
        name.charCodeAt(2),
        name.charCodeAt(3),
        name.charCodeAt(4),
        name.charCodeAt(5),
        // MIDI
        0x11, 0x06, 0x00, 0xC7, 0xC4, 0x4E, 0xE3, 0x6C, 0x51,
        0xA7, 0x33, 0x4B, 0xE8, 0xED, 0x5A, 0x0E, 0xB8, 0x03
      ]);
      //NRF.startBonding(true);
      //NRF.setConnectionInterval(20);
      NRF.setTxPower(4);
    
    }
    
    init("PuckOI");
    
    
    var idleTimeout;
    var active;
    Puck.on('accel',function(a) {
      LED.set();
      active = true ;
      if (idleTimeout) clearTimeout(idleTimeout);
      idleTimeout = setTimeout(function() {
        idleTimeout = undefined;
        LED.reset();
        active = false ;
      },1000);
    });
    
    if (active) {
    print("yes");
    //Puck.accelOn(); // default is 12.5Hz, with gyro
    //Puck.accelOn(1.6);// for 1.6Hz low power, without gyro
    // Turn events off with Puck.accelOff();
    
      const accel = Puck.accel();
      var x = ((accel.acc.x/128000)*10) *127;
      var y = ((accel.acc.y/128000)*10) *127;
      x = rescaleToMidiCC(x, -80, 80);
      y = rescaleToMidiCC(y, -80, 80);
      midisend(0,1,x);
      midisend(0,2,y);
    }
    
  • Hi - I see you closed this. Was it because you found the problem?

    Ideally you would call setServices once but it may not be a problem.

    It is possible that you're calling midisend too often though, and that is what is causing the problem?

  • Hi Gordon,
    I closed it because I realized it was a stupid mistake on my end ::)

    if you look closely , the midi send part isn't inside Puck.on('accel',function(a) {}
    The code was working but I was just never calling that part¯_(ツ)_/¯
    The if (active) statement is also completely useless ...

    This being said, using ble midi, needs the device to restart after uploading the code, meaning I need to flash vs ram. I noticed that after a few different uploads the device will start to act oddly ( aka bits of older code will still be there but merged with the new ones?)
    In this case what is the best approach to avoid this or start fresh? hard reset? reset() + save() ?
    thank you!

  • I noticed that after a few different uploads the device will start to act oddly ( aka bits of older code will still be there but merged with the new ones?)

    That'd odd - I guess maybe you had managed to save to flash so it runs all the time - but that's a bit dangerous so it's buried in the settings, or maybe you wrote to .boot0 or similar - this might help: https://www.espruino.com/Saving

    Writing to certain files means that you can have code that runs all the time, even after a reset.

    However require("Storage").eraseAll();reset() will clear everything out and refresh the device making it completely clean, whatever came before :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

problem motion detection + midi

Posted by Avatar for user155285 @user155285

Actions