1

Hey folks … I was hoping some of you might cast your eye over my code.

It splits up a string read from EEprom into an array of separate strings. Based on String length information stored in a separate array. (tempArray[])

It reads fine … and also seems to process fine (ie. it does turn one long string into separate array items) for some reason it stops working no the 8th array item?

The splitting code (problem) starts on line 80 and i have included the output above it in comments.

http://pastebin.com/Ax4R6t6J << Full code also available here.

void reJigFromMemory(){

  Serial.print("Eeprom read 1 :");
  Serial.println(eepromReadOne);
  Serial.print("EEprom read 2 :");
  Serial.println(eepromReadTwo);
  String tempArray[10];

  int charLength=eepromReadOne.length()+1;
  char charBuff[charLength];
  eepromReadOne.toCharArray(charBuff, charLength);

  //// GET MEMORY LOCATIONS
  /*
converts long string of numbers:
   13161717171717252533
   into separate strings:
   13
   16
   17
   17
   17
   17
   17
   25
   25
   33

   And stores them in an array.
   */
  for (int i = 0; i <10 ; i++){
    tempArray[i]+=charBuff[i*2];
    tempArray[i]+=charBuff[(i*2)+1];
    Serial.print("I just translated :");
    Serial.println(tempArray[i]);
  }
  /// GET THE STRINGS USING THE ABOVE ARRAYS
  /*
converts long list of data ie:
   |0|Bingo#999#|1|Retest2#9222#|2|Retest3#92299#|3|Retest4#99339#|4|Retest5#95599#|5|Retest6#96699#|6|Retest7#99119#|7|User Defined eighth#0#|8|User Defined ninth #0#|9|User Defined tenth (last) .#0#

   into individual array strings:

   From Number = :0
   To number : 13
   Record 0 :          |0|Bingo#999#
   From Number = :13
   To number : 29
   Record 1 :          |1|Retest2#9222#
   From Number = :29
   To number : 46
   Record 2 :          |2|Retest3#92299#
   From Number = :46
   To number : 63
   Record 3:           |3|Retest4#99339#
   From Number = :63
   To number : 80
   Record 4:           |4|Retest5#95599#
   From Number = :80
   To number : 97
   Record 5:           |5|Retest6#96699#
   From Number = :97
   To number : 114
   Record 6 :          |6|Retest7#99119#
   From Number = :114
   To number : 139
   Record 7 :               <<<<<< THIS ONE IS MISSING ? WHY?
   From Number = :139
   To number : 164
   Record 8 :               <<<<<< THIS ONE IS MISSING ? WHY?
   From Number = :164
   To number : 197
   Record 9 :               <<<<<< THIS ONE IS MISSING ? WHY?

   */
  String tempArray2[10];
  int fromLocation = 0;
  int toLocation=0;

  for (int i = 0; i <10 ; i++){                 /////// LINE 80

    toLocation += tempArray[i].toInt();

    fromLocation += tempArray[i-1].toInt();
    tempArray2[i]=eepromReadTwo.substring(fromLocation,toLocation);
    Serial.print("From Number = :");
    Serial.println(fromLocation);
    Serial.print("To number : ");
    Serial.println(toLocation);
    Serial.print("I just translated :");
    Serial.println(tempArray2[i]);


  }
}
  • please post code that is complete, i.e. it compiles and can be uploaded to an Arduino. Posting only some parts of your code may not be helpful. – jfpoilpret Oct 6 '14 at 19:42
  • Full Code Link in serial monitor type 1 to write data to the eeprom then 2 to read the eeprom to variables and then type 5 to process it and see the failure. Thank you! – hapi Oct 6 '14 at 20:25
  • The first issue I can see in your contrived code is fromLocation += tempArray[i-1].toInt(); where i=0: behavior there is undefined (may crash immediately or override memory used later...) – jfpoilpret Oct 6 '14 at 20:51
  • Thanks for advise, I will try and alter that part. I'm a beginner and i appreciate all input :) – hapi Oct 6 '14 at 21:50
1

Your 2nd for loop should look something like this:

int fromLocation = 0;
int toLocation=0;

for (int i = 0; i <10 ; i++)
{                 /////// LINE 80

    toLocation += tempArray[i].toInt();

    tempArray2[i]=eepromReadTwo.substring(fromLocation,toLocation);
    Serial.print("From Number = :");
    Serial.println(fromLocation);
    Serial.print("To number : ");
    Serial.println(toLocation);
    Serial.print("I just translated :");
    Serial.println(tempArray2[i]);
    fromLocation += tempArray[i].toInt();
  }
}

On the first pass through the loop, fromLocation is 0. toLocation starts at 0 and gets the value in tempArray2[0] added to it.

After extracting a string, you add the current array value to fromLocation to advance the pointer to the beginning of the next string.

Note that String objects are very inefficient way to save 2 digit numbers. It would be better use an array of ints, and write code that extracts 2 characters at a time into int values.

With only 20 String objects it's not a huge deal, but bear in mind that String objects take a lot of memory overhead on a memory-starved platform like an Arduino. Better to learn how to use C strings.

  • Great thank you so much. Strangely using the code about gave me all entries EXCEPT number 7. However once I changed the Search locations from char to int everything started working :D Final Function Code - PasteBin – hapi Oct 7 '14 at 1:42

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.