Arduino Uno Strucks Randomly

Hi, arduino uno struck randomly with the attached code(unable to post it due to character limit).

Code size is
Sketch uses 22418 bytes (69%) of program storage space. Maximum is 32256 bytes.
Global variables use 1111 bytes (54%) of dynamic memory, leaving 937 bytes for local variables. Maximum is 2048 bytes.

is size of code problem ? or did i messed up anything in the code ?

modifiedSketch.ino (16.7 KB)

Where in the program do you think it is getting stuck?

I think after sendBTOutput() function i.e start of loop()

sendBTOutput() is the last item in loop(), not at the start.

It's good to see you have some Serial.print statements. What do you see in the serial monitor?

You may need to add more to see which functions are being called.

You will need to provide lots of that information as not many people will have the hardware or libraries to test your project on their own.

I'm not sure you can declare btDataStartMillis like this

unsigned long btDataStartMillis = millis();

I would declare btDataStartMillis as a global, and in setup() execute the assignment btDataStartMillis = millis();. I know that will work.

adwsystems:
I'm not sure you can declare btDataStartMillis like this

unsigned long btDataStartMillis = millis();

I would declare btDataStartMillis as a global, and in setup() execute the assignment btDataStartMillis = millis();. I know that will work.

Will do that.

is it safe to assume that code size not the issue in this case ?
btw what are the Max limit for code size (like 75 % ?)

My Tx Batteries just died, i have to wait until they are back to test the code.

At a certain point the IDE will give you a warning about the memory usage. I have a few sketches that I optimized just enough to get rid of the warning and they run fine. You are seeing only the status, you do not have the warning message. I would not worry.

I think the problem is with MPU6050 i2C Dev library.
find github issue https://github.com/jrowberg/i2cdevlib/issues/252.

Any Alternative libraries for mpu6050 ?

anilkunchalaece:
Hi, arduino uno struck randomly with the attached code(unable to post it due to character limit).

Yours is a long and complex program.

Presumably during the development process there was a working version that was not getting stuck.

What is the difference between the working version and the problem version?

...R

The example in the library is working fine with the current setup (I dont think i checked it long enough to notice freezing, will do that now).
so i think hardware may not be the problem.

Difference between example and working code is the following lines in the setup

    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
        Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

If code is unresponsive, no lights flashing, no messages to the serial console etc. , then you can start suspecting statements like the following:

while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

This may block and no message is generated.

I Modified it to the following to get a serial message

    while (fifoCount < packetSize) {
      fifoCount = mpu.getFIFOCount();
      Serial.println(F("waiting for fifo count"));
    }

anilkunchalaece:
Difference between example and working code is the following lines in the setup

Are you saying that if you add those lines into setup() in the code in your Original Post then it will work properly?

If that is not what you are saying then there is more to the change than you have suggested.

...R

This all sounds odd considering you are using Serial.print() for debugging:

// There may be some conflict with softwareSerial and Servo Library So Hc-05 will be using Hardware Serial
//#include <SoftwareSerial.h> //Software Serial Library for HC-05 Bluetooth Module - this module act as a Slave and sends the data to the master or receive the data from master
//SoftwareSerial BTSerial(A0,A1); // TX, RX
. . .
. . .
Serial.begin(38400); //hc-05 is using hardware serial , 38400 is HC-05 Default Baud Rate

What, if anything, have you got connected to pins 0 and 1 ?

Robin2:
Are you saying that if you add those lines into setup() in the code in your Original Post then it will work properly?

If that is not what you are saying then there is more to the change than you have suggested.

...R

No, its not working even i added those lines,for other changes I am still looking but didnt find any

Edit : After i added those lines in setup code is struck in

  while (!mpuInterrupt && fifoCount < packetSize) {
    //Do nothing while mpu is not working
    //they are saying it is a short delay .. i need a way to avoid this
    Serial.print("waiting for mpu");
  }//end of while loop

6v6gt:
What, if anything, have you got connected to pins 0 and 1 ?

There is no connections for pins 0 and 1. if there are any, it wont allow me to upload the code right ?

anilkunchalaece:
No, its not working even i added those lines,for other changes I am still looking but didnt find any

You seem to be coming at the problem backwards.

My Reply #7 was aimed at getting you you to start from a version that does work and then consider carefully why the subsequent changes caused it to stop working.

Working forward from something that works is much easier than working backwards from something that is broken.

...R