accelerometer pitch, roll, yawn as midi cc

Posted on
  • Hello, I am trying to send the accelerometer angles as midi cc when a button is being pushed.
    But I am struggling getting the accelerometer data separately. Any help is welcome!
    thank you!

    // Include the MIDI library
    NRF.setServices({
      0x180D: {
        0x2A37: {
          value : [0],
          writable : true
        }
      }
    }, { advertise: ['180D'] });
    
    // Set up the button pin
    var BUTTON_PIN = BTN;
    pinMode(BUTTON_PIN, "input_pullup");
    
    // Set up the accelerometer
    var accel = require("puckjsv2-accel").use({
      frequency: 10,
      samples: 10
    });
    
    // Function to send accelerometer data as MIDI CC values
    function sendAccelerometerData() {
      // Get the accelerometer data
      var data = accel.get();
      // Send MIDI CC messages for each axis
      MIDI.send([0xB0, 1, data.x >> 3]);
      MIDI.send([0xB0, 2, data.y >> 3]);
      MIDI.send([0xB0, 3, data.z >> 3]);
    }
    
    // Set up a timer to continuously send accelerometer data
    var sendTimer = null;
    function startSending() {
      sendTimer = setInterval(function() {
        sendAccelerometerData();
      }, 50);
    }
    
    function stopSending() {
      clearInterval(sendTimer);
    }
    
    // Set up button events to start and stop sending
    setWatch(function(e) {
      if (e.state == false) {
        // Button was released, stop sending
        stopSending();
      } else {
        // Button was pressed, start sending
        startSending();
      }
    }, BUTTON_PIN, { edge: "both", debounce: 50, repeat: true });
    
  • could also be HID gamepad actually ::)

  • Hi - I'm not sure I've seen require("puckjsv2-accel") before - it's not a module we provide. The MIDI library isn't built in either. Where did you find the code? I imagine you get a bunch of errors when trying to run it?

  • Hi @Gordon
    thanks for your reply!
    I am not that familiar with the espruino env (coming from arduino IDE)
    tbh this was me trying to take a shortcut and being lazy using chat-gpt to find answers ::)
    I did read examples instead and ended up figuring out ::)

  • this was me trying to take a shortcut and being lazy using chat-gpt to find answers

    Yes, I thought that might be it! Please can you try not to post code from ChatGPT up without telling us it's from there first though? It just means we spend ages looking at it trying to figure out what on earth is going on, when in fact it'd have been faster to have just written something from scratch :)

  • oops sorry should have mentioned earlier ::)

About

accelerometer pitch, roll, yawn as midi cc

Posted by Avatar for user155285 @user155285

Actions