second half if needed
void loop() {
// if failed
if (!dmpReady1 || !dmpReady2 ) return;
// wait for MPU interrupt
while (!mpuInterrupt1 && fifoCount1 < packetSize1 && !mpuInterrupt2 && 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
// .
// .
// .
}
mpuInterrupt1 = false; // indicates whether MPU interrupt pin has gone high
mpuInterrupt2 = false; // indicates whether MPU interrupt pin has gone high
mpuIntStatus1 = mpu1.getIntStatus();
mpuIntStatus2 = mpu2.getIntStatus();
fifoCount1 = mpu1.getFIFOCount();
fifoCount2 = mpu2.getFIFOCount();
// check for FIFO overflow
if ((mpuIntStatus1 & 0x10) || fifoCount1 == 1024) {
// reset so we can continue cleanly
mpu1.resetFIFO();
Serial.println(F("Malfunction"));
// else check for DMP data ready interrupt
} else if (mpuIntStatus1 & 0x02) {
// wait for corrrect data length
while (fifoCount1 < packetSize1) fifoCount1 = mpu1.getFIFOCount();
// read a packet from FIFO
mpu1.getFIFOBytes(fifoBuffer1, packetSize1);
// track FIFO count here in case there is > 1 packet available bypassing interrupt
fifoCount1 -= packetSize1;
#ifdef OUTPUT_READABLE_YAWPITCHROLL
// display Euler angles in degrees
mpu1.dmpGetQuaternion(&q1, fifoBuffer1);
mpu1.dmpGetGravity(&gravity1, &q1);
mpu1.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);
myservo1.write(180-(90+ ypr1[2] * -180/M_PI));
#endif
}
if ((mpuIntStatus2 & 0x10) || fifoCount2 == 1024) {
// reset so we can continue cleanly
mpu2.resetFIFO();
Serial.println(F("Malfunction on 2"));
// else check for DMP data ready interrupt
} else if (mpuIntStatus2 & 0x02) {
// wait for correct available leangth
while (fifoCount2 < packetSize2) fifoCount2 = mpu2.getFIFOCount();
// read a packet from FIFO
mpu2.getFIFOBytes(fifoBuffer2, packetSize2);
// track FIFO count here in case there is > 1 packet available bypassing interrupt
fifoCount2 -= packetSize2;
// angles in degrees
mpu2.dmpGetQuaternion(&q2, fifoBuffer2);
mpu2.dmpGetGravity(&gravity2, &q2);
mpu2.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);
myservo2.write(180-(90+ ypr2[2] * -180/M_PI));
}
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}