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. ![]()
I am using an Arduino UNO SMD, tried it with both 9600 and 115200 baud.
Thanks for any help. ![]()