0

I'm trying to create an array of strings. I'm building each individual string by parsing a json file and getting the strings between '{'s and '}'s. However, for some reason, my array is only assigning one index with the string i want. This is my code:

  String day = "";
  String Combo[] = {"0","0","0","0","0","0","0","0"};
  int x = 2;
  int number = 0;

  /* Read data until either the connection is closed, or the idle timeout is reached. */ 
  unsigned long lastRead = millis();
  while (www.connected() && (millis() - lastRead < IDLE_TIMEOUT_MS)) {
    /* Reads one character at a time*/
    while (www.available()) {
      char sym = www.read();

      if (sym == '{') {
        x = 1;
      }
      if (x==1){
        if (!day.concat(sym)) {
          //Serial.println("concat() error!");
        }
      }
      if (x==1 && sym == '}'){
        x=0;
        Serial.println(number);
      }

      if ((number > 0) && (number < 9) && (x==0)) {
        Combo[number-1] = day;
        Serial.print(day);
        Combo[number-1] = day;

        Serial.println(F("-------------------------------------"));
        Serial.println(Combo[number-1]);


        Serial.println(F("-------------------------------------"));
        Serial.println(F("-------------------------------------"));
        day = "";
        number +=1;
        x = 2;
      } else if (number ==0 && x ==0){
        day = "";
        number +=1;
        x = 2;
      }
      lastRead = millis();

    }

  }

Which returns this:

{
    "firstString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "secondString"
    }-------------------------------------
{
    "secondString"
    }
-------------------------------------
-------------------------------------
{
    "thirdString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "fourthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "fifthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "sixthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "seventhString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "eighthString"
    }-------------------------------------

-------------------------------------
-------------------------------------
{
    "secondString"
    }-------------------------------------

Based on this output I know that day is being constructed correctly but for some reason isn't assigned to an index in Combo (except "secondString") Does anyone know what's going on?

Ideally Combo would equal: {"secondString", "thirdString", ... "eighthString"}

2
  • Any chance you could show us the JSON file you are trying to parse?
    – Majenko
    Jul 6, 2016 at 8:54
  • Perhaps you are out of memory?
    – Ariser
    Jul 6, 2016 at 13:39

1 Answer 1

1

I'm trying to understand your code.

  1. It looks like x is a state variable, with the following values:

    2 = "Looking for start brace (`{`)"
    1 = "Found! Now accumulating `day` until stop brace (`}`)"
    0 = "Finished!"
    

    If so, you may instead want to use the following:

    enum States { Looking, Adding, Done };
    
    // int x = 2;
    States state = Looking;
    
    // x = 1
    state = Adding;
    
    // if (x==1){
    if (state==Adding) {
    
  2. If you follow the flow of code, when number is still 0 and x finally becomes 0, you:

    • Print out number - the first line of your output;
    • You then miss the main if (number is not greater than 0);
    • And enter the else (which is satisfied);
    • And then you immediately clear your hard-earned day.
  3. The second time through, after x becomes 0 again, this time you:

    • Print out number - the second line of your ouptut;
    • You do enter the main if:

      • You assign day to Combo[1]
      • You print out only the first seven entries in the the array (you want <=7, not <7)

        • This should print out 0{?}000000 (I don't know what day should be).
        • The fact that it doesn't seems to imply that your concat() failed. Can you please change it to

          // day.concat(sym);
          if (!day.concat(sym)) {
              Serial.println("concat() error!");
          } // if
          
      • You then print out the row of dashes - and start a new line.

    • The third time through, it seems to work - you get your {$ "first string" $} (where $ represents \n and/or \r - can you strip those out?)

            // if (x==1){
            //    day.concat(sym);
            if (x==1 && sym>=' '){ // Don't concat new line characters!
                day.concat(sym);
      
    • And the final few times concat() seems to have failed again.

The reason I say "concat() has failed" is that it looks like day is still "" when it is assigned to Combo

Edit

You've changed the code so that the main if now does the following:

  1. Assigns day to Combo[number-1];
  2. Prints out day - this seems to work;
  3. Assigns day to Combo[number-1] again;
  4. Prints out a line of hyphens - this works;
  5. Prints out Combo[number-1] - this does NOT seem to work;
  6. Prints out two lines of hyphens - this works.

Since 5 is printing out nothing, it implies that 3 failed. This really does look like you're in a low-memory condition, and the system is failing to allocate memory from the heap.

You don't say what Arduino you're working on - note that most don't have much memory, and if you're already parsing a JSON file you've probably used a lot of it. You also don't say which version of the Arduino IDE you're using. There was a String bug in the earlier versions.

Rather than trying to use the String class (a renowned heap hogger), you should try to use normal char arrays and work the buffer management yourself.

1
  • Thx for the advice. For #2, I don't want the first day string to be saved so that's normal. I implemented #3 and it does seem that my concat fails often. Do you have any advice on fixing this?
    – Milylitre
    Jul 6, 2016 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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