mpu6050 timer to see how long it is on

// MPU-6050 test for movement
//

#include<Wire.h>
const int MPU=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int OldAcX,OldAcY,OldAcZ,OldTmp,OldGyX,OldGyY,OldGyZ;

int AcSensitivity = 1000;
boolean on = false;

unsigned long timer = 0;

void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

  timer = millis();
  
  Serial.begin(9600);
 
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

/*  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
*/

  if (abs(OldAcX - AcX) > AcSensitivity) {
    on = true;
  }

  if (abs(OldAcY - AcY) > AcSensitivity) {
    on = true;
  }

  if (abs(OldAcY - AcY) > AcSensitivity) {
    on = true;
  }
  

   
  if (on == true) {
      Serial.println("On");
      
       
  }

  OldAcX = AcX;
  OldAcY = AcY;
  OldAcZ = AcZ;
  
  on = false; 
  
  delay(333);
}

im looking for a way to record how long it is on also maybe like a 5 second safety to prevent accidental bumps

so if the sensor is moving for more then 5 seconds a timer will start and when it stops moving record how long it was that the sensor was moving

Record the value of millis() when the sensor starts moving, and again when it stops moving. Subtract the first from the second to learn how long the movement lasted, in milliseconds.

The hard part of this project is to clearly distinguish "sensor moving" from "sensor not moving". If the sensor stops moving for 200 milliseconds, then starts moving again, is that a separate event?

You will have to experiment under typical conditions to find the best approach.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.