My sketch STOPS after a few iterations

hello,
im using an arduino UNO,
Im using the imu 6050 and an encoder in order to estimate distance. I get the yaw angle from the imu and the distance from the encoder (ky-040) and with a formule I estimate position. this positions are stored after that in the sd card by a MicroSD reader for arduino.
my problem is that after some iteration (1 ,5, 10, 40 more or less) my arduino stop sending information to the SD Card.

This is my code:

#include <MPU9250.h>
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"
#define YAW      0
#define PITCH    1
#define ROLL     2
MPU6050 mpu;

bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer
int a = 0;
double t1;
Quaternion q;        // [w, x, y, z]         quaternion container
VectorFloat gravity; // [x, y, z]            gravity vector
float ypr[3];        // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
volatile bool mpuInterrupt = false;     // Indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
  mpuInterrupt = true;
}
void setup() {
  Wire.begin();
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  Serial.begin(9600);
  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("Initializing DMP..."));
  devStatus = mpu.dmpInitialize();
  mpu.setXAccelOffset(436);//556
  mpu.setYAccelOffset(-1485);//1448
  mpu.setZAccelOffset(914);//902
  mpu.setXGyroOffset(47);//81
  mpu.setYGyroOffset(-24);//-13
  mpu.setZGyroOffset(-28);//-27/8
  if (devStatus == 0) {
    Serial.println(F("Enabling DMP..."));
    mpu.setDMPEnabled(true);
    Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0 : #pin2)..."));
    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(")"));
  }
}
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;
    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);  ///save in this order [yaw, pitch, roll] in ypr table
    Serial.print(ypr[YAW] * (180 / M_PI)); Serial.print("\t");
    Serial.print(ypr[PITCH] * (180 / M_PI)); Serial.print("\t");
    Serial.println(ypr[ROLL] * (180 / M_PI));
  }
}

The serial prints that you are using to figure out where and when the program stopped indicate
what part of the program is sto... Oh, there are no serial prints in your code for troubleshooting purposes. May I suggest the OP add some serial prints to see the where/when happening?

1 Like

...but don't forget a Serial.flush after each debug print, at least to begin with.
And increase the serial speed - 9600 bits per second is sooo 1970s

1 Like

Could you put your sketch between lines with three backslash-single-quotes ?

```
Your sketch
```

If your I2C bus is not okay, then the Wire library can halt a sketch.

The I2Cdev library has a few issues. The FastWire is not faster, that is a myth. However, the I2Cdev is slower because of bugs.

1 Like

@dib_islam, your topic was moved to a more suitable location on the forum.

1 Like

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

1 Like

I don't need this, when it stp, it write in the serail monitor , ""error opening test.txt" and this happend repeatedly.

I dod , but the same problem :frowning:

Thank u, I did and the post had been edited
Cordialement

How can I know if this is the problem source?
Have u a good I2c library ?

Would have been good troubleshooting info for you to had disclosed right off the bat, no? I would not have wasted my time with un-needed advice. Well, good luck to you and trying to figure out your issue.

what is your native language?

Arab, frensh in the second then english !!

I don't mean that,
enyway, I'll try what u told me
thank u soo much

some users are ..... and don't react on experienced questions.

Well so it seems they have to go the hard way of a long term ping-pong-game with minimal progress

best regards Stefan

thank u for the traslation,
if I wrote my texts in arabic, then traslate it to english it will not be good
so I m tring to write in english, then if u don't understand u just mention the line or the paragraphe.
Which details would u like to have in ordre to help me ?

@dib_islam, the translation makes it harder to understand for you and for us. If something is not clear, ask if we can write it in a different way.

Can you tell more about your I2C bus ?
Which MPU-6050 module do you have (can you give link to it) ?
Do you use long wires, or a breadboard with bad contacts or a flat ribbon cable ?
Are there more sensors connected to the I2C bus ?
Do you use 5V or 3.3V to power the MPU-6050 module ?

The I2Cdev library should work, despite the issues.
If you ask a better library, then we say: "buy a better sensor, and then use a better library".

Your baudrate is very slow, can you make it 115200 baud ? Also set that baudrate in the Serial Monitor.
You might be sending too much text. If the TX buffer (inside the Serial library) is full, then the sketch will become very slow.