Arduino – Switch Statements

The switch statement, in Arduino programming, is very similar to the if statement that I wrote about previously. The main similarity is that you’re testing a statement, if it equals true, certain code is ran.

The difference is versatility and ease of use. When looking at a switch statement, it appears to be much more complicated than an if statement. It’s not! Actually it is much more simple to use, and produces a lot less code.

You will thank yourself later for making the switch, the switch statement switch.

What is it?

Without showing you any code right away, I’ll explain what it is in human language.

A switch statement takes a variable as an argument, and evaluates it. The actual switch part comes after. Now that it knows what it’s dealing with, it looks at the possible matches that you’ve preselected. When it finds a match, it runs the code until it comes across the word “break”. The break makes it skip the whole rest of the switch statement.

If there is no match, it runs the default code. The default code can be blank, and doesn’t have to contain anything juicy. A lot of the time, my default code just outputs to serial saying there was no match.

Uses

An example, where you would use a switch statement in the real world, could be in actual smart home light switches. Maybe you have a function that toggles them off and on when your cell phone app communicates with the arduino.

You could have the communication set a variable based on what button was pushed. Then the variable gets passed in to the switch statement. The switch statement sees the variables value of “living room” and associates it with what it knows, which may be switch number 3. Then inside your statement it can set variables and call functions based on switch 3.

And then you have light on, light off. This statement can reduce a lot of code, where you may otherwise have to write a function or if statement multiple times.

Or you may decide to put the switch statement inside the function, which would produce the same result as the function being inside the switch.

Usage

This begins with a variable that you’re going to test. This variable can be anything, an int, a string, char, etc. You start the switch statement with “switch (var)”. The “var” is where you’d place the name of your variable.

Much like other things you come across while programming for Arduino, you place the code to be run in curly braces. { and }

The difference here, from the other statements, is that the conditions that you are testing reside inside the curly braces. Normally your arguments would go inside the parenthesis, not today!

The way you compare what your variables value is, looks like this: “case condition1:”. You’d replace condition1 with the value you’re testing against your variable. For example, if you’re trying to see if a string says “taco”, you’d replace condition1 with the word taco. It would say “case taco:”.

Ending each code block after you define your case is very important. You end it with break;. If you do not break out of your code, either the compile will fail, or you’ll end up with unexpected results. This is the only time you do want to break your code, instead of fixing it.

 

The last part of this is the default portion. You define the default code to be run by calling “default:”. Any code between the “default:” and the “break;” is run if none of the other things you defined turned out to be true. Sometimes there are errors that cause the variable not to be set right, sometimes you planned on using default for multiple cases. Whatever the case is that you’re using default, you will know. I’ll say again, that you do not have to use the default section. It does need to be there, but it can be simply “default: break;” if you want your switch ignored when nothing is true.

Examples

This example will be assuming the ability to read based on the age of the person. We’ll start by defining the age variable:

int age = 0;

Then we’ll initialize Serial 0, so we can output to the pc:

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

In the loop we tell switch what variable we’re testing, and what each condition will do. In this case we’re just outputting different words to the serial monitor. After the switch statement is over we do age++, this simply increments the int by 1. You can increment any int this way, and if you have it being done in loop, it’ll just keep on adding. After we have a 5 second delay, before the loop starts over again.

void loop() {

switch(age) {
 case 1:
 Serial.println("You're only 1, how can you read this!");
 break;
 case 2:
 Serial.println("You're only 2, you can talk, but surely still can't read.");
 break;
 case 3:
 Serial.println("You are 3, I'd be pretty impressed if you could read.");
 break;
 case 4:
 Serial.println("You're 4, you may be able to read, but I still don't believe you.");
 break;
 case 5:
 Serial.println("You're 5, I guess it's time to start reading.");
 break;
 default:
 Serial.println("You are either older than 5, or younger than 1. Do whatever you want, I guess...");
 break;
}
age++;
delay(5000);//wait 5 seconds
}

I’ll paste the entire code now, if you’re copying and pasting it, you can use this one:

int age = 0;

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

void loop() {

switch(age) {
 case 1:
 Serial.println("You're only 1, how can you read this!");
 break;
 case 2:
 Serial.println("You're only 2, you can talk, but surely still can't read.");
 break;
 case 3:
 Serial.println("You are 3, I'd be pretty impressed if you could read.");
 break;
 case 4:
 Serial.println("You're 4, you may be able to read, but I still don't believe you.");
 break;
 case 5:
 Serial.println("You're 5, I guess it's time to start reading.");
 break;
 default:
 Serial.println("You are either older than 5, or younger than 1. Do whatever you want, I guess...");
 break;
}
age++;
delay(5000);//wait 5 seconds
}

Let’s see what happens when we run the code!

switch output

Exactly what we were hoping for! It counts from zero to a bazillion, and correctly chooses what switch condition to use.

Let me know if you have any questions below! More than happy to answer them.

Join the newsletter

Subscribe

Subscribe to get our latest content by email.

We won't send you spam. Unsubscribe at any time. Powered by ConvertKit

Leave a Reply

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