Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Using Switch Statements in Arduino

in Arduino Basics

In the if statement tutorial, we explained how you could control a program’s flow by testing conditions in your code. So, Like if statements, switch statements control the program’s flow by allowing the programmer to specify the code that should be executed in various conditions. When a case statement is found whose value matches that of the variable, the code in that statement is executed.

After the statement is executed, we write the keyword “break” at the end of each case. The break statement tells the Arduino to stop the current switch statement and move on to another part of the program.

To clarify how this is done, we will use an Arduino Board and Serial communication to provide examples of how to use a switch statement in an Arduino Sketch.

Expressions, data-types, in switch statements must have an integral or unenumerated type such as integer and char. You cannot use a switch statement for a string of characters (array of characters). If you would like to use a string of characters in a conditional statement, you should use the IF statement. It is possible to convert a string to be used in a switch statement, but that involves some harder programming that is not suitable for this tutorial’s purpose.

Generic code switch statement

switch (variable) {
case label1:
  // statements
  break;
case label2:
  // statements
  break;
default:
  // statements
  break;
}

The variable is evaluated and compared with the values of each case.

If there is a match, the statements after the matching label are executed until the break is encountered.

If there is no match, the default statements are executed.

The break statement is an important one since it tells the program to end the statement. If you do not use a break statement, all statements after the matching label are executed.

The syntax default can be used to execute code if no value of the cases matches the expression value.

Example 1, Counter with switch

Before we will dive into an example, let us first look at the generic code for and if …. else statement.

/*
   Switch sketch with counter
   the code has a counter that executes a
   switch statement
*/

int Counter = 0;

void setup()
{
  // start serial communication
  Serial.begin(9600);

} // close void setup()

void loop()
{
  // print counter number
  Serial.println(Counter);

  switch (Counter) {
case 1:
      Serial.print("The counter reached number 1");
      Counter++;
      break;
case 10:
      Serial.print("The counter reached number 10");
      Counter++;
      break;
case 15:
      Serial.print("The counter reached number 15");
      Counter = 0;
      break;
default:
      Serial.print("The counter reached number:");
      Counter++;
      break;
  } //closing switch statement

}// closing void loop()

Code Explanation

For the global variables, we choose one integer variables that we declare as Counter. We initialize that variable to be zero to make sure that the counting starts from 0.

int Counter = 0;

Since we want to see what we are doing, we start Serial communication at the beginning of the sketch.

// start serial communication
  Serial.begin(9600);

By enabling serial communication, we can print the value of the counter to the Serial monitor.

// print counter number
  Serial.println(Counter);

We arrived at the switch statement and the case statements. So when the counter is ZERO, the default message of the switch statement is printed. When the counter is 1, the statements below is executed.  After the text is printed, we add 1 to the counter by using the command counter++. To exit the case, the break; command is used.

switch (Counter) {
case 1:
      Serial.print("The counter reached number 1");
      Counter++;
      break;

The next two cases that we will encounter are if the variable Counter reaches the number 10 or 15. In between the default, the message is printed. One important note is that after the Counter variable reached number 15, the counter is set to 0. This means that the counter variable will hold the value 0. If you do not set the variable Counter to zero, the counter keeps counting.

case 10:
      Serial.print("The counter reached number 10");
      Counter++;
      break;
case 15:
      Serial.print("The counter reached number 15");
      Counter = 0;
      break;
default:
      Serial.print("The counter reached number:");
      Counter++;
      break;
Previous Post
Understanding Arrays in Arduino
Next Post
Understanding character strings in arduino

3 Comments. Leave new

  • Jacquelynn Karlsen
    8 April 2020 19:09

    Remarkable! Its truly remarkable piece of writing, I have got much clear idea on the topic of from this paragraph.

  • Earn Online
    12 October 2020 13:54

    I’m really impressed along with your writing talents and also with the format in your weblog. Is this a paid topic or did you modify it yourself? Anyway stay up the nice high quality writing, it’s uncommon to look a great weblog like this one nowadays.

You must be logged in to post a comment.
Menu