Why is my code just counting???

So I found this code a while ago and it worked great it was running 2 MPU6050 sensors on the same Arduino board and they controlled two different servos. I also get readings from both sensors on the serial monitor for yaw pitch and roll. I then left for a week and came back and forgot to save it. I just copied and pasted the code back into the serial monitor, but instead of getting readings the serial monitor said I was connected then just started counting up by seconds in one column. I've done a lot of work including finding the exact code I was using before and It was still counting. I've been trying to find the bug in the code and found were it is printing counting numbers by seconds (which is marked with a bunch of dashes after it in the code below). When I remove those lines of code it says interrupt not detected and doesn't read. I think it has to do with the librarys maybe but I have no clue besides those lines of code marked below in the code. I really need someone's help I am extremely stumped and I'm looking for true help.

Thanks in advance

p.s. the code is split into first half and second half because it doesn't fit all of it on one post.

Show the true sketch.

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


// 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_6Axis_MotionApps20.h"

// 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 accelgyroIC1(0x68);
MPU6050 accelgyroIC2(0x69);

#define OUTPUT_READABLE_YAWPITCHROLL

int16_t ax1, ay1, az1;
int16_t gx1, gy1, gz1;

int16_t ax2, ay2, az2;
int16_t gx2, gy2, gz2;

#define LED_PIN 13
bool blinkState = false;
bool attInt = false;

// New Stuff 
long unsigned currentTime = 0;
long unsigned counter = 0;
boolean flip = true;

//


// MPU control/status vars
bool dmpReady1 = false;  // set true if DMP init was successful
uint8_t mpuIntStatus1;   // holds actual interrupt status byte from MPU
uint8_t devStatus1;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize1;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount1;     // count of all bytes currently in FIFO
uint8_t fifoBuffer1[64]; // FIFO storage buffer
bool dmpReady2 = false;  // set true if DMP init was successful
uint8_t mpuIntStatus2;   // holds actual interrupt status byte from MPU
uint8_t devStatus2;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize2;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount2;     // count of all bytes currently in FIFO
uint8_t fifoBuffer2[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q1;           // [w, x, y, z]         quaternion container
VectorInt16 aa1;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal1;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld1;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity1;    // [x, y, z]            gravity vector
float euler1[3];         // [psi, theta, phi]    Euler angle container
float ypr1[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
Quaternion q2;           // [w, x, y, z]         quaternion container
VectorInt16 aa2;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal2;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld2;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity2;    // [x, y, z]            gravity vector
float euler2[3];         // [psi, theta, phi]    Euler angle container
float ypr2[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector



// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt1 = false;     // indicates whether MPU interrupt pin has gone high
volatile bool mpuInterrupt2 = false;     // indicates whether MPU interrupt pin has gone high

void dmpDataReady1() {
    mpuInterrupt1 = true;
}

void dmpDataReady2() {
    mpuInterrupt2 = true;
}


void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
Wire.begin(); 
TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
//Fastwire::setup(400, true);

// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(38400);

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

// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyroIC1.testConnection() ? F("MPU6050-1 connection successful") : F("MPU6050-1 connection failed"));
Serial.println(accelgyroIC1.testConnection() ? F("MPU6050-2 connection successful") : F("MPU6050-2 connection failed"));

    // wait for ready
  /*  Serial.println(F("\nSend any character to begin DMP programming and demo: "));
    while (Serial.available() && Serial.read()); // empty buffer
    while (!Serial.available());                 // wait for data
    while (Serial.available() && Serial.read()); // empty buffer again
*/
    // load and configure the DMP
    Serial.println(F("Initializing DMP..."));
    devStatus1 = accelgyroIC1.dmpInitialize();
    devStatus2 = accelgyroIC2.dmpInitialize();

    // supply your own gyro offsets here, scaled for min sensitivity
    accelgyroIC1.setXGyroOffset(220);
    accelgyroIC1.setYGyroOffset(76);
    accelgyroIC1.setZGyroOffset(-85);
    accelgyroIC1.setZAccelOffset(1788); // 1688 factory default for my test chip
    accelgyroIC2.setXGyroOffset(220);
    accelgyroIC2.setYGyroOffset(76);
    accelgyroIC2.setZGyroOffset(-85);
    accelgyroIC2.setZAccelOffset(1788); // 1688 factory default for my test chip

    // make sure it worked (returns 0 if so)
    if (devStatus1 == 0 && devStatus2 == 0) {
        // turn on the DMP, now that it's ready
        Serial.println(F("Enabling DMP..."));
        accelgyroIC1.setDMPEnabled(true);
        accelgyroIC2.setDMPEnabled(true); 

        // enable Arduino interrupt detection
        Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
      attachInterrupt(0, dmpDataReady1, RISING); // *** MOVED FROM SETUP TO LOOP
      attachInterrupt(1, dmpDataReady2, RISING); // *** MOVED FROM SETUP TO LOOP
        mpuIntStatus1 = accelgyroIC1.getIntStatus();           
        mpuIntStatus2 = accelgyroIC2.getIntStatus();   

        // set our DMP Ready flag so the main loop() function knows it's okay to use it
        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady1 = true;
        dmpReady2 = true;
        
        // get expected DMP packet size for later comparison
        packetSize1 = accelgyroIC2.dmpGetFIFOPacketSize();        
        packetSize2 = accelgyroIC2.dmpGetFIFOPacketSize(); 
            } else {
        // ERROR!
        // 1 = initial memory load failed
        // 2 = DMP configuration updates failed
        // (if it's going to break, usually the code will be 1)
        Serial.print(F("DMP Initialization failed (code "));
        Serial.print(devStatus1);
        Serial.print("\t");
        Serial.print(devStatus2);
        Serial.println(F(")"));
    }

                 

// configure Arduino LED for
    pinMode(0, INPUT); // *** ADDED FROM ORIGINAL CODE
    pinMode(1, INPUT); // *** ADDED FROM ORIGINAL CODE  
pinMode(LED_PIN, OUTPUT);
}

[code/]
void loop() {

    if (attInt == false){
      attachInterrupt(0, dmpDataReady1, RISING); // *** MOVED FROM SETUP TO LOOP
      attachInterrupt(1, dmpDataReady2, RISING); // *** MOVED FROM SETUP TO LOOP
      attInt = true;
  }

      // if programming failed, don't try to do anything
    if (!dmpReady1 || !dmpReady2 ) return;

       // wait for MPU interrupt or extra packet(s) available
    while (!mpuInterrupt1 && !mpuInterrupt2 && fifoCount1 < packetSize1 && fifoCount2 < packetSize2) {
        // other program behavior stuff here
        // .
        // .
        // .
        // if you are really paranoid you can frequently test in between other
        // stuff to see if mpuInterrupt is true, and if so, "break;" from the
        // while() loop to immediately process the MPU data
        // .
        // .
        // .
        currentTime = millis();
        if (currentTime % 1000 == 0){
          Serial.println(" ");
          Serial.println(currentTime / 1000);
        }
       
    }

    mpuInterrupt1 = false;     // indicates whether MPU interrupt pin has gone high
    mpuInterrupt2 = false;     // indicates whether MPU interrupt pin has gone high
    mpuIntStatus1 = accelgyroIC1.getIntStatus();
    mpuIntStatus2 = accelgyroIC2.getIntStatus();    

    fifoCount1 = accelgyroIC1.getFIFOCount();
    fifoCount2 = accelgyroIC2.getFIFOCount();

    // check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus1 & 0x10) || fifoCount1 == 1024) {
        // reset so we can continue cleanly
        accelgyroIC1.resetFIFO();
        Serial.println(F("FIFO 1 overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus1 & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount1 < packetSize1) fifoCount1 = accelgyroIC1.getFIFOCount();

        // read a packet from FIFO
        accelgyroIC1.getFIFOBytes(fifoBuffer1, packetSize1);
        
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount1 -= packetSize1;

            // display Euler angles in degrees
            accelgyroIC1.dmpGetQuaternion(&q1, fifoBuffer1);
            accelgyroIC1.dmpGetGravity(&gravity1, &q1);
            accelgyroIC1.dmpGetYawPitchRoll(ypr1, &q1, &gravity1);
            Serial.print("ypr1\t");
            Serial.print(ypr1[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(ypr1[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(ypr1[2] * 180/M_PI);
    }
    
    if ((mpuIntStatus2 & 0x10) || fifoCount2 == 1024) {
        // reset so we can continue cleanly
        accelgyroIC2.resetFIFO();
        Serial.println(F("FIFO 2 overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus2 & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount2 < packetSize2) fifoCount2 = accelgyroIC2.getFIFOCount();

        // read a packet from FIFO
        accelgyroIC2.getFIFOBytes(fifoBuffer2, packetSize2);
        
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount2 -= packetSize2;

            // display Euler angles in degrees
            accelgyroIC2.dmpGetQuaternion(&q2, fifoBuffer2);
            accelgyroIC2.dmpGetGravity(&gravity2, &q2);
            accelgyroIC2.dmpGetYawPitchRoll(ypr2, &q2, &gravity2);
            Serial.print("ypr2\t");
            Serial.print(ypr2[0] * 180/M_PI);
            Serial.print("\t");
            Serial.print(ypr2[1] * 180/M_PI);
            Serial.print("\t");
            Serial.println(ypr2[2] * 180/M_PI);
    }
   

// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
Serial.println(" ");
}

[/code]

If you're not getting interrupts I would change this:

    // configure Arduino LED for
    pinMode(0, INPUT); // *** ADDED FROM ORIGINAL CODE
    pinMode(1, INPUT); // *** ADDED FROM ORIGINAL CODE

to

    // configure Arduino LED for
    pinMode(0, INPUT_PULLUP); // *** ADDED FROM ORIGINAL CODE
    pinMode(1, INPUT_PULLUP); // *** ADDED FROM ORIGINAL CODE

If you want to use only INPUT you probably need to call accelgyroIC1.setInterruptDrive( true ) and accelgyroIC2.setInterruptDrive( true ) to force the gyro INT pins to totem-pole mode.

Hi,

I just copied and pasted the code back into the serial monitor,

What do you mean by that?
You cannot reprogram your controller by dumping code into the serial monitor.

Tom.... :slight_smile:

sorry when I said I copied and pasted the code into the serial monitor I meant I copied and pasted it into the Arduino IDE. I tried the pullup resistors but nothing worked. I also tried turning the sensors into totem mode in void setup but it didn't work either. Any other ideas.

Hi,
Have you loaded an example from the MPU library to see if the basics of the sensor are communicating properly?

Tom.. :slight_smile:

You're using pins 0 and 1 for connection to the MPUs but are also using the serial monitor.

Have you tried different Arduino pins for the INTs from the MPUs?