Gyro Readings Suddenly Stop

The program I've written is for straight line motion and then a U-Turn. I'm using Arduino Mega 2560 and MPU6050 gyroscope for keeping it straight and turning. However, while turning, the gyroscope reading stops at some random value and due to that, the bot keeps rotating forever. There is no pattern as to where the readings stop, it's very random. And there are times it doesn't stop too. I have no idea what the error is, please help. Attached is my code.

mode_1.ino (6.52 KB)

const int motor11=30;//LEFT MOTOR FW
const int motor12=31;//LEFT MOTOR BK
const int motor1pwm=2;
const int motor21=40;//RIGHT MOTOR FW
const int motor22=41;//RIGHT MOTOR BK
const int motor2pwm=8;

If you used meaningful names, like leftFW, leftBK, leftSpeed, rightFW, rightBK, and rightSpeed, you would not need comments AND you couldn't possible activate the wrong pin accidentally.

int flag1=0, flag2=0, flag3=0, flag4=1, flag5=0;//flag1 is for set point, flag2 is for slow start, flag3, flag4 for changing x, flag5 for changing y

Better names would eliminate the need for useless comments AND the need to scroll back to figure out WTF the code is supposed to be doing.

      timer = millis();

The millis() function returns a time, NOT a timer.

          if(y==5)
          {
            while(true)
            {
              digitalWrite(motor11, LOW);
              digitalWrite(motor12, LOW);
              analogWrite(motor1pwm, 120);   
              digitalWrite(motor21, LOW);
              digitalWrite(motor22, LOW);
              analogWrite(motor2pwm, 30);          
            }
          }

If y ever does equal 5, you've got an infinite loop here to assure that the robot never does anything more than the hokey-pokey for the rest of time.

Your code is in serious need of more Serial.print() statements, so you KNOW what it is doing.

Assume the correct pins have been activated. Comments are just for guiding a third person. Sorry about the naming thing. As for the millis() thing, that shouldn't be the problem because I haven't used 'timer' in the turning part. As for the y=5, it is intended to stop the robot after the y-coordinate is 5. When I add Serial.print statements, my gyroscope U-Turn value seems to be changing for some reason, which is why I removed all of them. I still don't understand, why the Gyro readings on the Serial Monitor just stop randomly.

I still don't understand, why the Gyro readings on the Serial Monitor just stop randomly.

There are no Serial.print() statements in loop(), so the output to the Serial Monitor app stops because setup() has ended.

The reason I removed print statements is because I felt the gyro readings are stopping to be read, because there is too much data being sent to the Serial port? So, what could be the reason data randomly stops being taken, is the question I'm getting at.

Please see this one. The only change is in the turning section. I've replaced while loops by if statements. Yet when I ran this, it continously took a turn non-stop. This shows that Serial.print or ont, the Gyro readings are stopping to be taken after some time. Why is this so?

whiletoif.ino (6.24 KB)