Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $269

Donations Received

X

Arduino switch and break Statements

Created on: 31 December 2014

Part 13 of the Arduino Programming Course

The switch statement is similar to using if with multiple else-if constructs. switch is used in conjunction with break which will also be explained in this part of the course.

Using switch instead of multiple else-if constructs is easier to read and has more flexibility.

switch Statement Example

The following Arduino sketch shows the switch statement being used in conjunction with the break statement.

Load the sketch to the Arduino and then start the Serial Monitor window. Sending 1 from the serial monitor window to the Arduino will switch the on-board LED on and sending 2 will switch the LED off.

Sending 3 will show the menu of options that the sketch operates on. Sending any other character will bring up a default message showing that the option chosen is invalid.

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);  // LED on pin 13 of UNO
}

char rx_byte = 0;

void loop() {
  if (Serial.available() > 0) {    // is a character available?
    rx_byte = Serial.read();
  
    switch (rx_byte) {
      case '1':
        digitalWrite(13, HIGH);
        Serial.println("LED is ON");
      break;
      
      case '2':
        digitalWrite(13, LOW);
        Serial.println("LED is OFF");
      break;
      
      case '3':
        Serial.println("------- MENU -------");
        Serial.println("1. Switch LED on.");
        Serial.println("2. Switch LED off.");
        Serial.println("3. This menu.");
        Serial.println("--------------------");
      break;
      
      default:
        Serial.println("Invalid option");
      break;
    } // end: switch (rx_byte)
  } // end: if (Serial.available() > 0)
}

How the sketch works will be explained later on this page, but first we must look at the structure of the switch statement and how the break statement works.

This video shows the above sketch in operation.

Can't see the video? View on YouTube →

Amazon.com

Amazon.co.uk

Structure of the switch Statement

The image below shows the structure of a switch statement.

Structure of an Arduino switch statement

Structure of an Arduino switch Statement

The switch statement has a variable (switch_var in the above image or rx_byte in the example sketch) which can be an integer (int) or character (char) variable.

The switch variable will be tested against the value in each case to see if they match. When a case is found that matches, the statements below the case will be run until the break keyword is reached. This will break the program flow out of the body of the switch statement and execution of the sketch will continue below the closing brace of the switch statement.

If no matching case is found, then the code under the default keyword will be run until its break statement is found.

How the Example Sketch Works

In the example sketch, the switch statement is placed inside an if statement in the main loop. The switch statement will then only run if a new character is received from the Serial Monitor window.

When a character is received from the Serial Monitor window, the switch statement will check for a matching case value. If the character '1' is received, then the LED is switched on and a message displayed in the Serial Monitor window.

If '2' is received, the LED is switched off. '3' displays a menu of the options available in the sketch.

If any character is sent that does not match the characters in any of the case statements, then the code in the default part of the switch body is run which displays a default message.

The break Statement

The break statement is used in the example sketch to break out of the body of the switch statement.

break can also be used to break out of any loop such as a while or for loop. As an example, a certain condition can be tested for in a loop using an if statement and if the statement evaluates to true, the break statement can be run to break out of the loop.

← Go back to Part 12Go to Part 14 →