Problem with controling dc motor with condition

So the idea is to control the motor using L289N and arduino. the arduino will read from MU6050 sensor and will control the motor depending on the sensor value.
however, the program loops about 10 times and stops(around 10 times, not fix everytime), and i realize that the program works longer when i supply a lower voltage( i tried with 4v and 8v), the motor is rated 3v-12v.
I am really desperate.

#include “Wire.h” // This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
double accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
double gyro_x, gyro_y, gyro_z; // variables for gyro raw data
double temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, “%6d”, i);
return tmp_str;
}
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);// this is just to make the debugging easier, you can ignore this.

}
void loop() {

float totalx = 0;
float totaly = 0;
float totalz = 0;

for (int i = 0; i<50;i++){
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 72, true); // request a total of 72=14 registers
accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)

totalx = totalx + accelerometer_x;

totaly = totaly + accelerometer_y;
totalz = totalz + accelerometer_z;

}
float averagex= totalx/50.0/16384.0;
float averagey= totaly/50.0/16384.0;
float averagez= totalz/50.0/16384.0;

if(averagex>=0){

analogWrite(3,200);
analogWrite(5,0);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(averagex);

Serial.println(“left”);}
else if (averagex<0){

analogWrite(3,0);
analogWrite(5,200);
digitalWrite(LED_BUILTIN, LOW);
Serial.println(averagex);

Serial.println(“right”);
}

Have you done any trouble shooting other than to try a different battery?

You could for example replace the MPU code with just a random number output to simplify things. That would remove the possibility of the MPU and / or its code causing any hanging. (Essentially just testing the motors.)

Have you run it with the motors disconnected, and just watched the led?

You need to provide a circuit schematic.

So I have tried different motor control moduels, L289N is one and the other one is L293d, they both present the same problem.

I have run the code without powering the motor, and the senor it self works just fine.
I have run the motor without the MPU6050, it works just fine with the L289N. i can turn it on/off multiple times without problem.

So I am really confised. meawhile, as I pointed out, if I power the motor with 4V, it seems to work longer. with 8v, it usually just stuck in one of the if condition, the serial monitor would stop printing.

After the initial post, I also replace the L289N with a single MOSFET IRF510. this would only allow me to control the speed of the motor not the direction. i still have the same issue.

I think there is something deeper going on on the physical level, not just the code.

circuit shown in attachment, things are simplified, the INT pin on MPU6050 is not used, I dont know if thats the problem.

How to attach an image

If your circuit really looks like that diagram there's a massive loop between the logic signals and their ground-return path, which goes round the motor.

That's maximizing the probability of interference from the motor.

Every signal wire should be routed with its ground return and well away from motors and high current paths.

Could you elaborate on the massive loop between the logic signal and their ground?
How should i wire the ground?