Learn to code

ARDUINO & CODES

Previous Post
Understanding Arrays in Arduino
Next Post
Sending information to the serial monitor using serial print() with Arduino

Using Switch Statements in Arduino

in Arduino Basics

In the tutorial: Arduino Tutorial: 2.6 Understanding If / Else statement, we controlled two LEDs by asking the user in the Serial Monitor for a choice. If these choices are tremendous we have to make a lot of IF statements. There is a way to make this easier. We can use switch case statements.

Like if statements, switch statements controls the flow of the program 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 a keyword named break at the end of each case. The break statement tells the Arduino to stop the switch statement and move on to other parts of the program.

We will use the same diagram as we used in: Arduino Tutorial: 2.7 Understanding Arrays in Arduino. However, we will write new code for it with a switch statement. 

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 9
220 ohm resistor 220 ohm resistor × 4

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Generic Code Switch Statement

switch (input) {
  case 1:
    //do something when var equals 1
    break;
  case 2:
    //do something when var equals 2
    break;
  default:
    // if nothing else matches, do the default
    // default is optional
    break;
}

In the above example, you can see the general code for a switch statement.

Execution of the switch statement begins by evaluating the expression inside the brackets. The expressions could be an integer or a character. This expression value is then matched with each case value. You can have as much as case statements that you would like. If the first case statement is not matched with the expression value, the program moves to the next value and so on. When the program matches a case value with the expression, the program will execute that particular code attached to the case.

Notice that in the last set of lines the word of default is used. When the word default is used within a switch statement it will be executed if no value of the cases matches the expression value.

Besides default, there is also the word break. The break statement is used at the end of each case value to break from the block of curly brackets. In other words, the break statement causes the program to exit from the switch statement.

Let us apply this and make a sketch that in which a user can input, through the serial monitor. Remember, we can only receive numbers and characters through this method. So if we would like to to use a string such as “red” as input to control the LED we need to make IF statements.

Code Example

// Sketch: Blinking two LEDs by user
int orangePin = 2; //set orange LED pin to 2
int greenPin = 7 ; //set green LED pin to 7
int redPin = 10; //set red LED pin to 10
int bluePin = 11; //set blue LED pin to 11
void setup() {
  // put your setup code here, to run once:
  pinMode(bluePin, OUTPUT); //Set redPin to be an output
  pinMode(redPin, OUTPUT); //Set greenPin to be an output
  pinMode(greenPin, OUTPUT); //Set redPin to be an output
  pinMode(orangePin, OUTPUT); //Set greenPin to be an output
  Serial.begin(9600); //Turn on Serial port
}
void loop() {
  Serial.println("What color do you want to blink up, red or blue?"); //Prompt user for color
  while (Serial.available() == 0) { } //Wait for input
  int colorChoice = Serial.read();
  switch (colorChoice) {
    case '1':
      digitalWrite(redPin, HIGH);
      delay(1000);
      digitalWrite(redPin, LOW);
      delay(1000);
      break;
    case '2':
      digitalWrite(bluePin, HIGH);
      delay(1000);
      digitalWrite(bluePin, LOW);
      delay(1000);
      break;
    case '3':
      digitalWrite(greenPin, HIGH);
      delay(1000);
      digitalWrite(greenPin, LOW);
      delay(1000);
      break;
    case '4':
      digitalWrite(greenPin, HIGH);
      delay(1000);
      digitalWrite(greenPin, LOW);
      delay(1000);
      break;
    default:
      Serial.println("That is not a valid color choice, please try again");
      Serial.println("");
  }
}

Code Explanation

The first thing, as usual, we need to declare our variables. We need to declare our LEDs.

In the setup() part of our sketch, we need to set the pins to output and begin Serial Communication.

As we arrive in the void loop () part, things are getting more interesting. First of all, we need to ask the user for input.

Serial.println("What color do you want to blink up, 1,2,3 or 4?"); //Prompt user for color

The second part is to wait for input from the Serial monitor. Remember, since we are reading numbers we need to declare an integer variable called colorChoice.

while (Serial.available() == 0) { } //Wait for input
  int colorChoice = Serial.read();

After the choice of the user through the Serial Monitor, the number should be tested in the switch statement. Meaning. that between the parenthesis the number of the input is compared with the different cases. If a case is equal to colorChoice the lines, in that case, are executed.

switch (colorChoice) {
    case '1':
      digitalWrite(redPin, HIGH);
      delay(1000);
      digitalWrite(redPin, LOW);
      delay(1000);
      break;

If the input of the user is not valid, we using the default and print statement to let the user know that their input is not valid.

default:
      Serial.println("That is not a valid color choice, please try again");
      Serial.println("");

1 Comment. 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.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu