To take it a step further, let's say you want to send commands that start with a single letter, then after the letter, the remaining digits, or chars have a purpose, if you want them to. For example, lets say you send the following command d50. The d could mean decrease speed, and the 50 could mean 50%, 50 steps, etc.
The following test sketch illustrates my point.
char inputBuffer[64 + 1];
void setup(){
Serial.begin(9600);
}
void loop(){
while(Serial.available() > 0){
Serial.readBytesUntil('\n', inputBuffer, sizeof(inputBuffer));
if(inputBuffer[0] == 'f'){
Serial.println("Letter 'f' entered");
}
// Letter "r". Everything after the "r" should be digits.
else if(inputBuffer[0] == 'r'){
Serial.print("Letter 'r' entered\t");
// Change the letter "r" to the digit 0 to keep atol() happy.
inputBuffer[0] = B110000;
Serial.print("Number entered = ");
Serial.println(atol(inputBuffer));
// Your code here that uses the number parsed using atol().
}
// Letter "s". Everything after the "s" should have a max
// length of up to 64 chars long for a 64 + 1 char buffer.
else if(inputBuffer[0] == 's'){
// MAX CHARS example with a 64 byte buffer:
// s123456789a123456789b123456789c123456789d123456789e123456789f + CR + NL
Serial.print("Letter 's' entered\tText entered = ");
// Print out the chars in the buffer.
for(int i = 1; i < strlen(inputBuffer); i++){
Serial.print(inputBuffer[i]);
}
Serial.println();
// Your code here that uses the char data after the letter "s" is entered.
}
memset(inputBuffer, 0, sizeof(inputBuffer));
}
}