I need to know the falling time of an object with MPU6050

Hello,
My name is Javier and I'm new in this forum.

I'm trying to get the time a MPU6050 (accelerometer sensor) is falling. I get to know when is falling, but I need to know the duration of that fall.

I wrote this and is working. (It works based on the code from playground Arduino Playground - MPU-6050)

{

float accel_x = accel_t_gyro.value.x_accel;
float accel_y = accel_t_gyro.value.y_accel;
float accel_z = accel_t_gyro.value.z_accel;

float mag = sqrt(accel_xaccel_x + accel_yaccel_y + accel_z*accel_z);

if (mag < 1000){
Serial.println("fall");
}
delayMicroseconds(50);
}

Thank you!
Sorry for the bad english!
Cheers from Chile

I get to know when is falling,

If you can tell the difference between falling and not falling, you can tell when the change from not falling to falling happens. Call millis() (or micros()) to record when that happens.

If you can tell the difference between falling and not falling, you can tell when the change from falling to not falling happens. Call millis() (or micros()) to record when that happens.

The difference between the two times will be the time that the object was falling.

javierlopezzani:
I need to know the duration of that fall.

I think you have to do a state-change-detection and find out the times when the

  • state changes to falling: remember start time
  • state changes to not falling: create time difference to start time and display result.

Try something like:

float oldmag=0.0; // declare as global variable
unsigned long fallStartTime;

void loop()
{
  float accel_x = accel_t_gyro.value.x_accel;
  float accel_y = accel_t_gyro.value.y_accel;
  float accel_z = accel_t_gyro.value.z_accel;
  float mag = sqrt(accel_x*accel_x + accel_y*accel_y + accel_z*accel_z);

  if (mag<1000 && oldmag>=1000) fallStartTime=micros();
  if (mag>=1000 && oldmag<1000)
  {
    long falltime=micros()-fallStartTime;
    Serial.print("Time [us]: ");
    Serial.println(falltime);
  }
  oldmag=mag;
}

P.S.: Most likely you will have to include a "hysteresis" in the state change detection.

Thank you guys! I'll work with those two answers!

You just have to hope that the event that causes the "no longer falling" state transition isn't catastrophic!

javierlopezzani:
Thank you guys! I'll work with those two answers!

And don't forget to actually read the sensor within the loop code!

In your (and my) example code, the sensor is never read, because no function call is invoked for doing so!

The example code seems to contain just calculations which use the last read values, but never read new values from the sensor.