MPU - 6050 Value Calculation Not Working

Hello,
I have an MPU-6050 connected to an Arduino Uno. I am using the I2Cdev library with that. I adapted the code from the example sketch which displays the yaw, pitch, and roll values to display the accelerometer values, along with detecting when the accelerometer module is still. The way I’m trying to find that is by finding the percent change between the previous and the current value, and then finding whether its below a threshold.
Here is my complete code, the calculation is done at the bottom:

#include “I2Cdev.h”

#include “MPU6050_6Axis_MotionApps20.h”
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include “Wire.h”
#endif

MPU6050 mpu;
#define OUTPUT_READABLE_YAWPITCHROLL
#define OUTPUT_READABLE_REALACCEL

#define LED_PIN 13
bool blinkState = false;

bool dmpReady = false;
uint8_t mpuIntStatus;
uint8_t devStatus;
uint16_t packetSize;
uint16_t fifoCount;
uint8_t fifoBuffer[64];

Quaternion q;
VectorInt16 aa;
VectorInt16 aaReal;
VectorInt16 aaWorld;
VectorFloat gravity;
float euler[3];
float ypr[3];

volatile bool mpuInterrupt = false;
void dmpDataReady() {
mpuInterrupt = true;
}

int accelX;
int accelY;
int oldX = 0;
int oldY = 0;
int percentChangeX;
int percentChangeY;

boolean ledState = false;

void setup() {

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
TWBR = 24;
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif

Serial.begin(115200);
while (!Serial);

Serial.println(F(“Initializing I2C devices…”));
mpu.initialize();

Serial.println(F(“Testing device connections…”));
Serial.println(mpu.testConnection() ? F(“MPU6050 connection successful”) : F(“MPU6050 connection failed”));

Serial.println(F("\nSend any character to begin reading values: "));
while (Serial.available() && Serial.read());
while (!Serial.available());
while (Serial.available() && Serial.read());

Serial.println(F(“Initializing DMP…”));
devStatus = mpu.dmpInitialize();

mpu.setXGyroOffset(220);
mpu.setYGyroOffset(76);
mpu.setZGyroOffset(-85);
mpu.setZAccelOffset(1788);

if (devStatus == 0) {

Serial.println(F(“Enabling DMP…”));
mpu.setDMPEnabled(true);

Serial.println(F(“Enabling interrupt detection (Arduino external interrupt 0)…”));
attachInterrupt(0, dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();

Serial.println(F(“DMP ready! Waiting for first interrupt…”));
dmpReady = true;

packetSize = mpu.dmpGetFIFOPacketSize();
} else {

Serial.print(F(“DMP Initialization failed (code “));
Serial.print(devStatus);
Serial.println(F(”)”));
}

pinMode(LED_PIN, OUTPUT);
}

void loop() {

if (!dmpReady) return;

while (!mpuInterrupt && fifoCount < packetSize) {

}

mpuInterrupt = false;
mpuIntStatus = mpu.getIntStatus();

fifoCount = mpu.getFIFOCount();

if ((mpuIntStatus & 0x10) || fifoCount == 1024) {

mpu.resetFIFO();
Serial.println(F(“FIFO overflow!”));

} else if (mpuIntStatus & 0x02) {
while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

mpu.getFIFOBytes(fifoBuffer, packetSize);

fifoCount -= packetSize;

#ifdef OUTPUT_READABLE_YAWPITCHROLL

mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
Serial.print(“ypr\t”);
Serial.print(ypr[0] * 180/M_PI);
Serial.print("\t");
Serial.print(ypr[1] * 180/M_PI);
Serial.print("\t");
Serial.println(ypr[2] * 180/M_PI);
#endif

#ifdef OUTPUT_READABLE_REALACCEL

mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetAccel(&aa, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
Serial.print(“accel:\t”);
accelX = aaReal.x;
Serial.print(accelX);
Serial.print("\t");
accelY = aaReal.y;
Serial.print(accelY);
Serial.print("\t");
Serial.println(aaReal.z);

percentChangeX = ((aaReal.x - oldX)/ aaReal.x) * 100;
percentChangeY = ((aaReal.y - oldY)/ aaReal.y) * 100;
if (percentChangeX == 0 && percentChangeY == 0) {
ledState = true;
}
else {
ledState = false;
}

Serial.println(percentChangeX);
Serial.println(percentChangeY);
if(ledState) {
digitalWrite(LED_PIN, HIGH);

}
else {
digitalWrite(LED_PIN, LOW);
}
oldX = aaReal.x;
oldY = aaReal.y;
Serial.println(oldX);
Serial.println(oldY);

#endif
}

}

The problem is that the percent change always shows up as 0 for both X and Y, and when I move it it jumps up to 100 or 700 or 200. The oldX and oldY values print just fine, so I don’t know why the percent change isn’t working. I tried putting the calculation into a function outside void loop, but then it didn’t seem to get the accelerometer values and spat out gibberish.
Am I doing the calculation wrong?

Sorry if this seems like a dumb question, I am completely new to using the accelerometer and the library.
Thanks in advance.

Alright, just for future people who have similar problems, I solved it by making the percentChange and other variables into floats instead of integers. Hope this post maybe helps someone in the future.... :grinning: