Arduino Program crashes few seconds after program is run


I am making a maze-solving robot using Arduino uno, mpu 6050 gyroscope and encoder. My program crashes after some time (usually 10-13seconds) after I run the robot. It seems to get stuck in the TravelStraightForward18cm() since it spends most of the time in that function. I think it might be that gyroscope values are being overfilled into the arduino and do not get cleared in the stack. The code does use a lot of gyroscope readings to correct for angular deviations, make turns and the whole navigation. Can anyone let me know how i can improve my code to avoid such a crash.

Gold_Detection.ino (32.1 KB)

Your DistanceMeasurement() is hosed. Without the delays to give the device time to send more than one pulse, you haven't a hope in hell of getting accurate measurements.

It's a good thing you don't actually call ReadEncoder(). Turning interrupts off like that is a bad idea.

I got tired of wading through code, trying to guess which functions are actually used. That is something you should have determined BEFORE posting code.

I am not going to wade through 32k of code trying to find a problem - life is too short.

If the program is nicely organized into a series of small functions it should be possible to isolate the problem and produce a short program that illustrates it.

If the program is not nicely organized then that may be the place to start.

...R Planning and Implementing a Program

My apologies for posting a long code. You are right that the DistanceMeasuremnent function is inaccurate. The mpu 6050 gyroscope library that we are using is not made to work with any delays. Delays cause overflows such as FIFO overflow and ovf in the gyroscope output which is also important. Its like a compromise where you want the delays to give you accurate data, but they create overflows in gyroscope values.

Reply #3 is interesting, but it does not get us any closer to being able to help.