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.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working 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:
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.