Random freezing of arduino Nano

Hello,

I've written the attached code and it seems to work well on our machine, until it freezes solid and requires a reboot. Any ideas as to what may be causing the issue?

Freezing seems to be happening at random. I've determined no pattern and can not cause the freeze on demand based on any sequence of inputs. I've left the machine on overnight without any freezing. I've also caused it to freeze upwards of 3 times in a 5-10 minute period during use. I'd say maybe one pattern that is developing is that it freezes while I'm using it and adjusting the various inputs.

I've read about strings causing memory use issues. I'm not too sure how to tell if I'm having an issue related to this. I included the "freeRam" function and it returns a constant value. Not sure if I'm calling the function at a useful time in the program.

Any input would appreciated. Thanks in advance!

WMV2_Posted.ino (11.5 KB)

The first thing I did when I opened up your sketch was ctrl+F "String" (note the capital S). And sure enough there it was. This is most likely your problem. It is bad practice to use String in arduino due to the lack of garbage collection so you will run out of free memory and crash.

Thanks for the reply. I've read that using a char array is better than a string - is that what you'd recommend? I'm using the Strings to push data to a pair of 4 digit displays in the displayRPM() and displayPressure() functions.

if you DO recommend using a character array instead of the string could we point me in the direction of altering this function to do so?

Those functions always felt a bit "brute force". Not sure how to clean them up though.

void displayRPM()
{


String RPMString = String(RPMSetting);
String threeZero = "000";
String twoZero = "00";
String oneZero = "0";
String RPMPrint = "";

    Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

    alpha4.begin(0x70);
  
  
  if (RPMSetting <10)
  {
    String RPMPrint = threeZero + RPMString;

    alpha4.writeDigitAscii(0, RPMPrint.charAt(0));
    alpha4.writeDigitAscii(1, RPMPrint.charAt(1));
    alpha4.writeDigitAscii(2, RPMPrint.charAt(2));
    alpha4.writeDigitAscii(3, RPMPrint.charAt(3));
     
  }
  else if (RPMSetting < 100)
  {
    String RPMPrint = twoZero + RPMString;

    alpha4.writeDigitAscii(0, RPMPrint.charAt(0));
    alpha4.writeDigitAscii(1, RPMPrint.charAt(1));
    alpha4.writeDigitAscii(2, RPMPrint.charAt(2));
    alpha4.writeDigitAscii(3, RPMPrint.charAt(3));
  
   
  }
  else 
  {
    String RPMPrint = oneZero + RPMString;

    alpha4.writeDigitAscii(0, RPMPrint.charAt(0));
    alpha4.writeDigitAscii(1, RPMPrint.charAt(1));
    alpha4.writeDigitAscii(2, RPMPrint.charAt(2));
    alpha4.writeDigitAscii(3, RPMPrint.charAt(3));
   
   } 
      alpha4.writeDisplay();
}

Another thing that exhibits those symptoms is writing past the end of an array into memory that you do not "own". Check your array indexes and make sure none ever exceed the length of its array.

using a char array is better than a string

Character arrays work correctly, if you use them correctly. Strings often don't work correctly on Arduino.

Perhaps you could do something like this (RPM as an example):

void displayRPM()
{
    char
        RPMString[20];

    Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

    alpha4.begin(0x70);

    if( RPMSetting <10 )
        sprintf( RPMString, "000%d", RPMSetting );
    else if( RPMSetting < 100 )
        sprintf( RPMString, "00%2d", RPMSetting );
    else
        sprintf( RPMString, "0%3d", RPMSetting );
   
    for( int i=0; i<4; i++ )
        alpha4.writeDigitAscii( i, RPMString[i] );

    alpha4.writeDisplay();
}

You incur a bit of a flash hit by introducing libraries for sprintf() but the code is pretty tidy and String-less.

thank you for that bit of code! It dropped right in an works great. Now, I'll do some more testing with the machine and see if eliminating the Strings has stopped the freezing problem.

alogus:
thank you for that bit of code! It dropped right in an works great. Now, I'll do some more testing with the machine and see if eliminating the Strings has stopped the freezing problem.

Make sure you remove all the String instances in a similar way. I just did the RPM as an example....