You can't compare a string like command == "light" in C because that would just compare pointer addresses, not the strings themselves. You have to either use "strcmp" or "strncmp" if it's available in Arduino or you have to write a while loop comparing the two strings bytewise, something like
char *cmd, *my_str;
int is_equal;
cmd = command;
my_str = /* the string to read in */;
is_equal = 1;
// compare bytewise
while (is_equal && *cmd && *my_str)
is_equal = (*cmd++ == *my_str++);
// make sure both strings are of the same length, so that "blahx" != "blah"
if (is_equal)
is_equal == (*cmd == 0 && *my_str == 0);
if (is_equal)
// do the command
You can also put that into a function. As for reading in strings via serial, I've written a simple sketch to familiarize myself with the environment that will read in words or sentences from serial and "morse" them via a led connected to pin 13. It includes a portion of code to read in the strings which works fine even if there are long delays between the various letters. There are some initialization things missing because the whole sketch exceeds the character limit for posts on this forum, but the point is that I have this code at the beginning of my loop function and "word" will contain the read in string if a carriage return is received... If you can't make sense of the code below, I'll trim it down the most necessary and repost it...
while (newWordInd < MAX_SERIAL_LEN && serialAvailable() > 0) {
c = (char)serialRead();
if (c == -1)
break;
if (c == '.' && newWordInd > 0) {
want_repeat = 1;
continue;
} else if (c == ';' && newWordInd > 0) {
want_repeat = 1;
want_eeprom = 1;
continue;
}
if ((c == '\n' || c == '\r') && newWordInd > 0) {
new_word = 1;
do_repeat = want_repeat;
want_repeat = 0;
// trim off any trailing spaces
while(newWordInd > 0 && serialWord[newWordInd-1] == ' ')
newWordInd--;
if (newWordInd == 0)
continue;
// zero-terminate
serialWord[newWordInd] = '\0';
newWordInd = 0;
// save the word to the eeprom
if (want_eeprom) {
char *word = serialWord;
int i = 0;
while (*word && i < 511) {
eeprom_write_byte((unsigned char *)i++, *word);
word++;
}
eeprom_write_byte((unsigned char *)i, (char)0);
printNewline();
printString("\n\nARDUINO : saved in eeprom : ");
printString(serialWord);
printNewline();
printNewline();
want_eeprom = 0;
}
break;
}
// only dots directly before a newline trigger repeating...
want_repeat = 0;
// ignore spaces at beginning of word
if (c == ' ' && newWordInd == 0)
continue;
// ignore more than one space in a row
if (c == ' ' && serialWord[newWordInd-1] == ' ')
continue;
if (c != ' ') {
// convert to upper case
if (c & 32)
c -= 32;
if (c < 'A' || c > 'Z')
continue;
}
serialWord[newWordInd++] = c;
}
}
if (new_word) {
char *word = serialWord;
outputMorse(LOW);
value = LOW;
// swap buffers
serialWord = morseWord;
morseWord = word;
wordInd = 0;
letterInd = NULL;
// pause of 7 DITs before starting a new word since we might be interrupting another
// word
curDelay = 7*DIT;
// ensure that the first delay in the array will be taken...
previousMillis = millis();
new_word = 0;
do_morse = 1;
}