Shock sensing with TinyAccelerometer


  • Jr. Member
  • **
    • Posts: 8
    • View Profile
The default code example for the TinyAccelerometer reads raw accelerometer values which isn't terribly useful. I wanted to get a shock value, for example, if you attach it to a ball and want to measure how hard it was kicked, you can use this code in the loop():

void loop()
{;//This function gets new data from the accelerometer
  // Get magnitude of acceleration: sqrt(x*x+y*y+z*z) and subtract out gravity.
  int32_t x2 = (int32_t)accel.X * (int32_t)accel.X;
  x2 += (int32_t)accel.Y * (int32_t)accel.Y;
  x2 += (int32_t)accel.Z * (int32_t)accel.Z;
  SerialMonitorInterface.println(sqrt(x2) - 260);
  delay(250);//We'll make sure we're over the 64ms update time set on the BMA250

Works nicely. If I flick the sensor with my finger I see a reading of about 200-500. +1g is equal to ~260. If you find this useful let me know.


SMF spam blocked by CleanTalk