Splitting Code not working properly

Hello Arduino people!

I stumbled across a strange problem regarding char Arrays. However, as my code includes and uses the Serial library, I thought asking here would be better than on StackOverflow or else.

void readKeyboard() {
  char request[64][64]; // Holds the request with Parameters
  char tmpChar; // Holds the character sent by Serial at the moment
  int  wordIndex = 0;
  int  charIndex = 0;
  
  while(Serial.available() > 0) {
    tmpChar = Serial.read();
    if(tmpChar == ' ') { // If we got a space here, this argument is complete. 
      wordIndex++;
      charIndex = 0;
    } else { // No space here. Actual argument (with the number *wordIndex*) gets filled up.
      request[wordIndex][charIndex] = tmpChar;
      charIndex++;
      request[wordIndex][charIndex] = '\0';
    }
  }

  if(wordIndex>0 || charIndex>0) {
    Serial.print(request[0]); // Simply print out the first two arguments.
    Serial.print(' ');
    Serial.print(request[1]);
  }
}

That code is called over and over again. It simply should take a command with or without arguments from the Serial line.
How I think it should work: While there is a character waiting on the Serial line, we'll read it (gets stored into tmpChar). If that tmpChar is a space (' '), the argument we just filled up is finished. If that character is not a space, we should fill up the actual argument.
However, I am missing something. :~ If I send, for example, "Whoopie Diner" over Serial to the Arduino, it should return "Whoopie Diner": argument #1 (request[0]), a space, and argument #2 (request[1]). It doesn't:

w h o o p i e e d di din dine diner diner
 diner

That's what I receive.

When I run this again with the exact same string, "Whoopie Diner", I get this:

w diner
wh diner
whooo diner
whooppie diner
whoopie pie din die dir di
 di

Meh. :roll_eyes:

I am using an Arduino UNO SMD, tried it with both 9600 and 115200 baud.

Thanks for any help. :slight_smile:

A 64x64 array eats 4096 bytes of SRAM and your Uno has 2K.

why don't you read everything in and then parse it once you get the enter key,

 while(Serial.available() > 0) {
      myChar = Serial.read(); 
      currentLine += myChar;
      if (myChar == '\n') {
          int     spacePosition;  // the position of the next comma in the string
          spacePosition = currentLine.indexOf(' ');
          wordOne =currentLine.substring(0,spacePosition);
          wordTwo =currentLine.substring(commaPosition+1, currentLine.length());
          }
          currentLine = "";
    }

econjack:
A 64x64 array eats 4096 bytes of SRAM and your Uno has 2K.

Oh my god. That is embarrassing. :frowning:
Thanks! :grin:

jasit:
why don't you read everything in and then parse it once you get the enter key,

 while(Serial.available() > 0) {

myChar = Serial.read();
      currentLine += myChar;
      if (myChar == '\n') {
          int    spacePosition;  // the position of the next comma in the string
          spacePosition = currentLine.indexOf(' ');
          wordOne =currentLine.substring(0,spacePosition);
          wordTwo =currentLine.substring(commaPosition+1, currentLine.length());
          }
          currentLine = "";
    }

Well, both my code and your code get to the same result, no? :wink:

Just for the record, thats my working code now. :wink:

void readKeyboard() {
  String request[10];
  char tmpChar;
  int  wordIndex = 0;

  while(tmpChar != '\n') {
    if(Serial.available() > 0) {
      tmpChar = Serial.read();
      if(tmpChar == ' ') {
        wordIndex++;
      } 
      else {
        request[wordIndex] += tmpChar;
      }
    }
  }

  if(wordIndex>0) {
    Serial.print(request[0]);
    Serial.print(request[1]);
    Serial.print(request[2]);
  }
}

thx for the help guys. :smiley: