1

Two days ago, my code is working fine on the arduino board. But yesterday, and today I tried so many time to upload the arduino code, which contains 1000 lines of code. It was successfully uploaded the code into arduino. But it only upload half code into arduino when I saw the serial monitor results (I use serial println(); test where the code is located. ) I am so confusing, why the code is working fine two days ago. but later, only half code being used. Please help.

Here is the memory I left when I uploaded it.

Sketch uses 48,314 bytes (19%) of program storage space. Maximum is 253,952 bytes. Global variables use 2,603 bytes (31%) of dynamic memory, leaving 5,589 bytes for local variables. Maximum is 8,192 bytes.

Here is the code it stuck :
   Get Parameters 
//
//  This function reads the information from the SD card for a given monitoring
//  site.
//

//

int getParameters(){
  String lowerLimit, upperLimit;

   char buffer[36];
  char newChar;
  int index, index1, index2, index3, line, theLength, numSensors;
  myFile.setTimeout(2000);
  line = 0;
  while (myFile.available()) {
    index = 0;
    do{
      newChar = myFile.read();
      buffer[index] = newChar;
      index += 1;
    }
    while(newChar != 10);
    buffer[index] = 0;
    theLines[line] = buffer;
    Serial.print(theLines[line]);
    //here the result is something like this
    //theLines[0] , class 111
    //theLines[1] , 192.168.1.10
    //theLines[2] , Mac Address

    line += 1;
  }

//so it is working fine above the code. But here is the problem stuck.
//the code cannot go through the rest of the code below. Nothing happened!!!
  for (int line = 0; line < 10; line++){
    Serial.print(theLines[line]);

  }
2
  • Please edit question and say how much program memory is in use, how much RAM is being used, and perhaps provide a diff between current code and the code that worked a few days ago – James Waldby - jwpat7 Jul 13 '15 at 18:35
  • I have posted it. – Yang Jul 13 '15 at 19:47
0

A portion of your code is shown below.

  myFile.setTimeout(2000);
  ....
  while (myFile.available()) {
    index = 0;
    do {
      newChar = myFile.read();
      buffer[index] = newChar;
      index += 1;
    } while(newChar != 10);
   ....
  }

Apparently your code is accepting and printing several lines of option setting text and then hangs up in the code extract shown above.

Suppose, for example, one extra character appears after the supposedly-last linefeed character. Your while (myFile.available()) loop will run again. It will store the extra character, then try to read another character. If another character isn't available, newChar = myFile.read(); will return a -1 that your code will store. That will happen repeatedly, clobbering RAM.

To see if this is what's happening, you could change while(newChar != 10); to (eg) while(newChar != 10 && index<36);. If the code then stops hanging up, look into where the extra character is coming from.

2
  • I think this may well be correct, however also, how big is this array theLines? How many lines are in the file you are reading? Possibly you have exceeded the size of the array. Or possibly you have run out of RAM. – Nick Gammon Jul 13 '15 at 20:44
  • 1
    In addition change while(newChar != 10) to while(newChar != 10 && myFile.available()) to allow for running out of file data. – Nick Gammon Jul 13 '15 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.