Arduino switch case

Arduino switch case statements form the basis for making lots of conditional operations in a very compact way. The save you from having to write tons of chained if else statements.

The Arduino switch statement takes an expression to be evaluated and multiple Arduino case statements act on the result of the expression. There is only one Arduino switch statement, but multiple Arduino case statements.

What does Arduino switch case do?

The switch statement accepts an expression and you use multiple cases statements to test an expression against the switch expression. If they match then code after the case statement is executed.

The best way to understand it is using an example first coded using Arduino if else statements and then translated into Arduino switch case statements.

The simple example chosen below decodes characters received from the serial port. Some characters have specific actions while some characters can be ignored. Here the characters 'a' and 'r' turn on an LED while the character 'z' turns off the LED. Other characters are ignored.

Arduino if else code

#define LED LED_BUILTIN

void setup(void) {

   Serial.begin(115200);
   Serial.println("Action of if else to demonstrate switch case");

   digitalWrite(LED, LOW);
   pinMode(LED,OUTPUT);
}

void loop(void) {
int command = 0;

   if (Serial.available() ) {
      char ch = Serial.read();

      if (ch=='a') {
         command = 10;

      } else if (ch == 'r') {
         command = 20;

      } else if (ch == 'z') {
         command = 30;

      } else { // Default
         command = 0;
      }
   }

   // Action from detection of command character.

   if (command == 10 || command == 20) {
      digitalWrite(LED, HIGH);
      Serial.println("LED on");

   } else if (command == 30) {
      digitalWrite(LED, LOW);
      Serial.println("LED off");
   }
}

Entering 'a' or 'r' results in turning on the LED, while entering 'z' turns it off, and entering any other character has no effect.

Arduino switch case code

#define LED LED_BUILTIN

void setup(void) {

   Serial.begin(115200);
   Serial.println("Action of Arduino switch case replacing if else");

   digitalWrite(LED, LOW);
   pinMode(LED,OUTPUT);
}

void loop(void) {
int command = 0;

   if (Serial.available() ) {
      char ch = Serial.read();

      switch(ch) {
         case 'a' : command = 10; break;
         case 'r' : command = 20; break;
         case 'z' : command = 30; break;
         default  : command = 0;
      }
   }

   // Action from detection of command character.

   switch( command ) {
      case 10 :
      case 20 :
         digitalWrite(LED, HIGH);
         Serial.println("LED on");
         break;
      case 30 :
         digitalWrite(LED, LOW);
         Serial.println("LED off");
         break;
   }
}

You can see how much more compact the code has become (Arduino switch case allows for compactness and therefore better readability). The expression being compared to the switch expression is also clearer.

Important Observations : Arduino switch case

The break statement

Unlike all other C constructs, curly braces are not used to define the actions within a block of code for an individual case statement. Rather the code to be executed for a matching case statement:

    Starts after the colon and...
    Ends at the break statement.

Warning: 'break' - usually causes the most problems - when you forget it!

Fall through

You can miss out a break statement on purpose legitimately in C. The effect is to create an OR condition. Compare the if-else code (below) to its equivalent Arduino switch case code (also below). In the if-else code, the conditional OR expression '||' is used explicitly.


   if (command == 10 || command == 20) {
      digitalWrite(LED, HIGH);
      Serial.println("LED on");

   } else if (command == 30) {
      digitalWrite(LED, LOW);
      Serial.println("LED off");
   }

For the Arduino switch case code, the OR expression is implied by omission of the break statement. If you leave out the break statement, the flow of operation is for execution of  the code in the next case statement. This continues until a break statement is encountered.


   switch( command ) {
      case 10 :
      case 20 :
         digitalWrite(LED, HIGH);
         Serial.println("LED on");
         break;
      case 30 :
         digitalWrite(LED, LOW);
         Serial.println("LED off");
         break;
   }

In the code above, leaving out the break after the test for expression equal to 10, means the next case statement code is executed i.e. execution continues for code from expression equal to 20. So, if the value of the command variable is 10 or 20 then the LED is set high and the Serial message "LED on" is output.

Expressions : Arduino switch case

Only expressions can be evaluated in the switch and case statements i.e. numbers or boolean values. Here the character evaluates to a single number. So for instance you can not put strings in place of the expressions.

Warning: You can only evaulate expressions (resulting in a number or a boolean value) within Arduino switch cases statements.

Default Action

A special extra statement in the Arduino switch case construct is 'default'. This takes the place of the 'tested' expression and is used to catch the case when none of the other expressions match the switch expression. Here's the example used in the original code above.
      switch(ch) {
         case 'a' : command = 10; break;
         case 'r' : command = 20; break;
         case 'z' : command = 30; break;
         default  : command = 0;
      }
If the character ch is not 'a', or 'r', or 'z' then the variable command is set to 0.


New! Comments

Have your say about what you just read! Leave me a comment in the box below.



Privacy Policy | Contact | About Me

Site Map | Terms of Use