Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect movement from accelerometer

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);
    }
}
like image 218
Lechucico Avatar asked Sep 04 '26 07:09

Lechucico


1 Answers

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);
    }
}
like image 105
Iharob Al Asimi Avatar answered Sep 06 '26 04:09

Iharob Al Asimi