case statements

How can you Serial.read multiple letters?
I want to make it read case statements that are whole words but when i try it cuts to my default and prints my error message for each letter in the word. Is there a way to make it understand a whole word at a time? :confused:

And your code looks like............

What do your "whole words" look like? Strings, strings, or enumerated constants?

Attach your code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]

A switch case only compares the values you pass like an int or a char. It won't compare a full string
You will need to use nested if () {} else {} statements

For building a string from a serial input you have multiple options, reading one char at a time and accumulating in a inputSerialBuffer until you find your input end of line (or have exceeded your buffer capacity then deal with error) or use higher level functions using the String class such as [url=https://www.arduino.cc/en/Serial/ReadStringUntil]readStringUntil[/url]() which is not encouraged because String class leads to memory issues depending on how the rest of your program is structured and uses memory.

My recommendation would be to go with reading char one by one, adding into inputSerialBuffer,

Assuming your commands have fixed length or test for an end of command char - Sample,code below, not tested I Just typed that in so be careful when using it - PLEASE proofread it.

You need global variables

#define BUFFER_LENGTH 9
char inputSerialBuffer[BUFFER_LENGTH];
 
// BUFFER_LENGTH needs to be greater than COMMAND_LENGTH by 1 char to add a trailing zero
#define MAX_COMMAND_LENGTH 8 // expecting command1, command2 so 8 char

// if you have variable command length, then define which char ends the command
#define END_OF_COMMAND_CHAR '

Then in you main loop

  // clearing any previous command stored in the buffer
memset(inputSerialBuffer, 0, sizeof(char) * BUFFER_LENGTH); // Zeros at all positions

int i = 0;

    While(1) {
        if (Serial.available() ) { 
             inputSerialBuffer[i] = (char) Serial.read();
             if ((i == MAX_COMMAND_LENGTH) || (inputSerialBuffer[i] == END_OF_COMMAND_CHAR)) break;
             i++;
        } // end if
    } // end while

In theory once you have received your end of line character or the,number of expected chars you need to add a 0 at the end of inputSerialBuffer to have a well formed C string and use strcmp, but here as I put zeros everywhere before capturing the command and I know BUFFER_LENGTH is greater than COMMAND_LENGTH I know I have at least one trailing zero and can use C functions working on well formed char strings with a trailing zero.

Note that the END_OF_COMMAND_CHAR is part of the string returned in the case above

Then you can compare with your commands

if ( !strcmp(inputSerialBuffer, "command1") ) {
        execCommand1();
      } else if ( !strcmp(inputSerialBuffer, "com$") ) {
        execCom();
      } else {
        errorCommand();
      }

Note the
** **!** **
Before the strcmp that's because the function is defined as this:

int strcmp(const char *str1, const char *str2)

Where

str1 -- This is the first string to be compared.
str2 -- This is the second string to be compared.

And the function return value is as follows:

if Return value < 0 then str1 is less than str2 (alphabetic ASCII ordering)
if Return value > 0 then str2 is less than str1.(alphabetic ASCII ordering)
if Return value = 0 then str1 is equal to str2. (ASCII Comparaison character by character, so capital letters and lowercase being different)

Hope this helps


Then in you main loop

§DISCOURSE_HOISTED_CODE_5§


In theory once you have received your end of line character or the,number of expected chars you need to add a 0 at the end of inputSerialBuffer to have a well formed C string and use strcmp, but here as I put zeros everywhere before capturing the command and I know BUFFER_LENGTH is greater than COMMAND_LENGTH I know I have at least one trailing zero and can use C functions working on well formed char strings with a trailing zero.

Note that the END_OF_COMMAND_CHAR is part of the string returned in the case above 

Then you can compare with your commands

§DISCOURSE_HOISTED_CODE_6§


Note the 
**```**
**§_DISCOURSE_HOISTED_CODE_7_§**
**```**
Before the `§_DISCOURSE_HOISTED_CODE_8_§` that's because the function is defined as this:

§DISCOURSE_HOISTED_CODE_9§


Where

`§_DISCOURSE_HOISTED_CODE_10_§` -- This is the first string to be compared.
`§_DISCOURSE_HOISTED_CODE_11_§` -- This is the second string to be compared.

And the function return value is as follows:

if Return value < 0 then `§_DISCOURSE_HOISTED_CODE_12_§` is less than `§_DISCOURSE_HOISTED_CODE_13_§` (alphabetic ASCII ordering)
if Return value > 0 then `§_DISCOURSE_HOISTED_CODE_14_§` is less than `§_DISCOURSE_HOISTED_CODE_15_§`.(alphabetic ASCII ordering)
if Return value = 0 then `§_DISCOURSE_HOISTED_CODE_16_§` is equal to `§_DISCOURSE_HOISTED_CODE_17_§`. (ASCII Comparaison character by character, so capital letters and lowercase being different)

Hope this helps

Using the the buffer thing might work I'll try it tonight. Thank you for the detailed explanation of how that works :slight_smile: