Accelerometer - How to toggle [SOLVED]

Posted on
  • Hi!

    I'm working on using Puck 2's accelerometer in order to detect when I'm braking on my bike!

    As of yet, I haven't been able to turn off the Accelerometer using Puck.accelOff(). Accel reads seem to be blocking my code to turn it off - but I think it's more the case I'm doing something wrong.

    Here's my code:

    
    // NRF.findDevices(function(devices) {
    //   console.log(devices);
    // }, 1000);
    
    console.log(`Battery ${Puck.getBatteryPercentage()}%`);
    
    let accelerometerEnabled = false;
    
    // Are we busy?
    var busy = false;
    
    // The device, if we're connected
    var connectedDevice = false;
    
    // The 'tx' characteristic, if connected
    var txCharacteristic = false;
    
    // Function to call 'toggle' on the other Puck
    function sendDataOverBLE(data) {
      if (!busy) {
        busy = true;
        if (!connectedDevice) {
          NRF.requestDevice({ filters: [{ id: 'a1:a1:a1:a1:a1:a1 public' }] }).then(function(device) {
            console.log('got device');
            return device.gatt.connect();
          }).then(function(device) {
            console.log('connected');
            connectedDevice = device;
            return device.getPrimaryService("4fafc201-1fb5-­459e-8fcc-c5c9c331914b");
          }).then(function(service) {
            return service.getCharacteristic("beb5483e-36e1­-4688-b7f5-ea07361b26a8");
          }).then(function(characteristic) {
            txCharacteristic = characteristic;
            busy = false;
          }).catch(function(e) {
            console.log(e);
            connectedDevice = false;
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
            if (connectedDevicected) { 
              connectedDevice.disconnect();
            }
          });
        }
        else {
          if (!accelerometerEnabled) { 
            connected.disconnect();
            busy = false;
            return; 
          }
          txCharacteristic.writeValue(data ? `${data.acc.x}` : "no data yet").then(function() {
            //digitalPulse(LED2, 1, 500); // light green to show it worked
            busy = false;
          }).catch(function(e) {
            console.log(e);
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
          });
        }
      } else {
        // console.log('busy');
      }
    }
    
    function toggleAccelerometer() {
     if (accelerometerEnabled) {
       accelerometerEnabled = false;   
       Puck.accelOff();   
       digitalWrite(LED1,1);
       digitalWrite(LED1,0);
       console.log('Accel OFF');
       if (connected) connected.disconnect();
     }
     else {
       accelerometerEnabled = true;   
       Puck.accelOn(); // default is 12.5Hz, with gyro    
       digitalWrite(LED2,1);
       digitalWrite(LED2,0);   
       console.log('Accel ON');
     }
    }
    
    function checkAccelerometerEnabled(data) {
      if(accelerometerEnabled) { 
        sendDataOverBLE(data);
      }
    }
    
    // Call function when the button is pressed
    setWatch(toggleAccelerometer, BTN, { edge:"rising", debounce:50, repeat: false });
    
    // or Puck.accelOn(1.6); for 1.6Hz low power, without gyro
    Puck.on('accel', function(data) {
      if (accelerometerEnabled) { 
        checkAccelerometerEnabled(data);
        const x = data.acc.x;
        const y = data.acc.y;
        const z = data.acc.z;
        const xZero = 750;
        const yZero = 15;
        const zZero = -8200;
        console.log(x+xZero,y+yZero,z+zZero);
      }
      else {
        // console.log(x+xZero,y+yZero,z+zZero);
      }
    
    });
    
    

    No issues with sending the data over BLE to my ESP32 however - works like a charm!

    One other thing I wanted to know was how to "normalise" the accel's values, but I'll post that up in another convo.

  • Hi - is your Puck firmware up to date (2v08)?

    accelOff should definitely work! What if you just try something simple like:

    Puck.on('accel', function(data) { digitalPulse(LED1,1,10); });
    Puck.accelOn();
    // LED should  blink
    // Now run Puck.accelOff()
    

    What you've got there definitely all seems ok to me. What I can see though is you're using connected but don't seem to be defining it anywhere, so when accelerometerEnabled is false, line 48 will probably cause an error, and that could be causing some issues for you?

  • OK, found the issue!

    It was my misunderstanding of the repeat option on setWatch... All working now once set to true, thank you!

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

Accelerometer - How to toggle [SOLVED]

Posted by Avatar for Stoaty @Stoaty

Actions