Arduino Code Freezing.

Hello,

Hope that I can get the help from you guys out there.
I have a problem with my project.
The problem is summarized by the fact that the Arduino code stops working after some time.
T
I am using I2C for LCD.

Is it possible to help me in debugging ?
Do you think the millis() function or the I2c itself might create any problem ?

Do you think its directly related to the loop() function freezing ?

Thank you a lot for your support.
:confused:

i2c.ino (8.96 KB)

A common cause of such things is the use of String. I’d be disinclined to look further until that’s remedied.

Thanks wildbil,

I was wondering,what do you recommend I use instead ?

Plain old C strings will serve you better. Strings (note case) tend to trash your heap and cause your sketch to crash when it can no longer allocate memory for them.

Yes, using the String class is probably the problem. There are two problems:

1) You are using the String class to accumulate the command line characters. Use a char array instead (also called C strings).

2) You are expecting a complete command line from Serial in this section:

      int count = 0;
        while(Serial.available()and not (character == '^') and count < 50)
        {
              character = Serial.read();          
              if (character != '^') content.concat(character);
              delay(5);   
              count ++;
        }

Look at Serial Input Basics for one way to accumulate characters into an array. When the complete line is received (when a newline ‘\n’ is received), then proceed to checking for various commands, like “open” or “close”.

Then you can use the C string functions to compare or manipulate that command with other C string constants, like “open”. Instead of this:

void loop()
{
      String content = "";

          ...

    if (content=="open")

… do this:

const size_t CONTENT_SIZE = 30;
char content[ CONTENT_SIZE ];
size_t index;
bool newData = false;

void loop()
{
        ...

    recvWithEndMarker();
    if (newData) {
      newData = false;

      //  A complete command line was received, parse it.

      if (strcmp( content, "open") == 0)

Description of strcmp here (and many others).

Then use C string functions to print parts of the command line. Instead of this (line 169):

                 if (content.substring(0,3) == "#0#"){    
                        lcd.setCursor(0,0);
                        content = content.substring(3);
                  }
                  else if (content.substring(0,3) == "#1#") {
                        lcd.setCursor(0,1);
                        content = content.substring(3);
                  }  
                 lcd.print(content);

… do this:

                 char *ptr = content; // default to the start of the command
                  if (strstr( content, "#0#" ) == content){    
                        lcd.setCursor(0,0);
                        ptr += 3; // step past first 3 chars
                  }
                  else if (strstr( content, "#1#" ) == content) {
                        lcd.setCursor(0,1);
                        ptr += 3; // step past first 3 chars
                  }  
                 lcd.print( ptr ); // print starting from whatever ptr is pointing at

This prints the characters from the array, starting at ptr. If it is a special command, it advances the ptr from the beginning of the command to the 3rd element.

The rest of your sketch appears to correctly use the “Blink Without Delay” technique (no delays). Just make the Serial command processing work the same way.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :)

Thank you @-dev and @wildbill

I just updated my code.

I will try to test it through the week and if its solved i will definitely post the code so that others might benefit from your help!

Thanks!