I have a Micro:Bit. It has an accelerometer, so I'm able to measure acceleration on x,y,z axis.
The idea is to wear it on the arm and send over bluetooth when it detects some movement on the arm.
So, I would like to check the acceleration and generate an event if it passes some kind of threshold, but I don't know how to do this.
This would be something like this:
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i",x,y,z);
uart->send(ManagedString(buffer));
}
int main() {
while (1) {
x = uBit.accelerometer.getX();
y = uBit.accelerometer.getY();
z = uBit.accelerometer.getZ();
// Check if device is getting moved
if (accel > 1) onAwake(x,y,z); // Some kind of threshold
sleep(200);
}
}
If the magnitude of acceleration doesn't change the device could be moving anyway, so you would need to store all 3 values and compare.
Something like this
void onAwake (int x, int y, int z){
snprintf(buffer, sizeof(buffer), "%i/%i/%i", x, y, z);
uart->send(ManagedString(buffer));
}
int main() {
int x;
int y;
int z;
while (1) {
int nx = uBit.accelerometer.getX();
int ny = uBit.accelerometer.getY();
int nz = uBit.accelerometer.getZ();
// Check if device is getting moved
if ((x != nx) || (y != ny) || (z != nz))
onAwake(x, y, z); // Some kind of threshold
sleep(200);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With