Having an issue getting the proper readings from my accelerometer using I2CDev

I am trying to build an "Oh Fudge" device as seen on Sparkfun.

I have an Arduino Nano 3.0, GY-521 Gyro/accelerometer and a Relay Module.

GY-521 is working properly. Serial monitor found it and the example code in the I2Cdev Library works fine.

Sparkfun provided the code for their original project and I tried to modify it to use the MPU6050 I2Cdev code as the accelerometer they used simply had an x y and z pin out and could be read directly, the GY-521 cant tomy knowledge.

Here's what I have.

// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//      2013-05-08 - added multiple output formats
//                 - added seamless Fastwire support
//      2011-10-07 - initial release

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/

#define RelayPin 9
#define LED_PIN 13

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high

int16_t ax, ay, az;
int16_t gx, gy, gz;

// uncomment "OUTPUT_READABLE_ACCELGYRO" if you want to see a tab-separated
// list of the accel X/Y/Z and then gyro X/Y/Z values in decimal. Easy to read,
// not so easy to parse, and slow(er) over UART.
//#define OUTPUT_READABLE_ACCELGYRO

// uncomment "OUTPUT_BINARY_ACCELGYRO" to send all 6 axes of data as 16-bit
// binary, one right after the other. This is very fast (as fast as possible
// without compression or data loss), and easy to parse, but impossible to read
// for a human.
#define OUTPUT_BINARY_ACCELGYRO

int xval = 0;                  // Initial X axis value
int yval = 0;                  // Initial Y axis value
int zval = 0;                  // Initial Z axis value

int xval2 = 0;                  // Secondary X axis value
int yval2 = 0;                  // Secondary Y axis value
int zval2 = 0;                  // Secondary Z axis value

int result1 = 0;                 // Variable for sum of all three 
int result2 = 0;                 // Varialbe for to compare to
int result_diff = 0;             // Variable for the difference
int threshold = 30;              // Variable for threshold  
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    // initialize serial communication
    Serial.begin(38400);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // configure Arduino LED for
  pinMode(LED_PIN, OUTPUT);
  pinMode(RelayPin, OUTPUT);        // set Relay pin as a digital output
  digitalWrite(RelayPin, LOW);
  delay(5000);              // Give us time to leave the package
}

void loop() {
    // read raw accel/gyro measurements from device
    //accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    accelgyro.getRotation(&gx, &gy, &gz);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
    
  xval = (gx);      // Get X axis value
  yval = (gy);      // Get Y axis value
  zval = (gz);      // Get Z axis value
  result1 = xval + yval + zval;       // Add them all up

  delay(100);                // Wait 1/10 a second

  xval2 = (gx);      // Get new X value
  yval2 = (gy);      // Get new Y value
  zval2 = (gz);      // Get new Z value
  result2 = xval2 + yval2 + zval2;      // Add new values up
  result_diff = abs(result2 - result1); // We only want + values

  // This is debug code
  Serial.print(" X_");
  Serial.print(xval);
  Serial.print(" Y_");
  Serial.print(yval);
  Serial.print(" Z_");
  Serial.print(zval);
  Serial.print("    R1_");
  Serial.print(result1);
  Serial.print(" R2_");
  Serial.print(result2);
  Serial.print("    Diff_");
  Serial.print(result_diff);
  Serial.print("    Thresh_");
  Serial.println(threshold);
//delay(5000);
  if (result_diff > threshold){  // Check to see if the difference is greater than the threshold, If it is, run code below.
digitalWrite(RelayPin, HIGH);    // Turn on relay  HONK!
delay(1000);              // Wait 1/10th a second
digitalWrite(RelayPin, LOW);     // Turn off relay
digitalWrite(13, HIGH);   // Turn on timeout LED
delay(5000);             // Wait 5 seconds
digitalWrite(13, LOW);    // Turn off timeout LED
  }
else{ digitalWrite(RelayPin, LOW); // Keep relay off and do nothing } 
}}

The problem I am running into is that result1 and result2 are never different they always show the same reading, meaning the diff variable always remains at 0 and the relay never gets triggered. I've tried inccreasing the delay between result1 and result2 but it makes no difference in the outcome.

If anyone can shed some light on this it would be greatly appreciated. Thanks!

The problem I am running into is that result1 and result2 are never different they always show the same reading,

  xval = (gx);      // Get X axis value
  yval = (gy);      // Get Y axis value
  zval = (gz);      // Get Z axis value
  result1 = xval + yval + zval;       // Add them all up

  delay(100);                // Wait 1/10 a second

  xval2 = (gx);      // Get new X value
  yval2 = (gy);      // Get new Y value
  zval2 = (gz);      // Get new Z value
  result2 = xval2 + yval2 + zval2;      // Add new values up

And, why should the values be different? Wait until hell freezes over between adding up three numbers, and the Arduino is still going to get the same result.

Now, if you were to read the accelerometer again, I'm guessing that you probably would get different values the second time.

PaulS:

The problem I am running into is that result1 and result2 are never different they always show the same reading,

  xval = (gx);      // Get X axis value

yval = (gy);      // Get Y axis value
  zval = (gz);      // Get Z axis value
  result1 = xval + yval + zval;       // Add them all up

delay(100);                // Wait 1/10 a second

xval2 = (gx);      // Get new X value
  yval2 = (gy);      // Get new Y value
  zval2 = (gz);      // Get new Z value
  result2 = xval2 + yval2 + zval2;      // Add new values up



And, why should the values be different? Wait until hell freezes over between adding up three numbers, and the Arduino is still going to get the same result.

Now, if you were to read the accelerometer again, I'm guessing that you probably would get different values the second time.

That code sample came from Sparkfun. They had it running with the gyro they used. But your point was valid and I totally overlooked it. Everything is working fine now after adding a second instance of accelgyro.getAcceleration(&ax, &ay, &az); before the second set of readings. Thanks!