Switch (case) Statement, used with serial input

A second switch-case example, showing how to take different actions based on the characters received in the serial port.

An if statement allows you to choose between two discrete options, TRUE or FALSE. When there are more than two options, you can use multiple if statements, or you can use the switch statement. Switch allows you to choose between several discrete options.

This tutorial shows you how to use switch to turn on one of several different LEDs based on a byte of data received serially. The sketch listens for serial input, and turns on a different LED for the characters a, b, c, d, or e.

Hardware Required

  • Arduino Board

  • 5 LEDs

  • 5 220 ohm resistors

  • hook-up wires

  • breadboard

Circuit

Five LEDs are attached to digital pins 2, 3, 4, 5, and 6 in series through 220 ohm resistors.

To make this sketch work, your board must be connected to your computer. In the Arduino IDE open the serial monitor and send the characters a, b, c, d, or e to lit up the corresponding LED, or anything else to switch them off.

circuit

Schematic

schematic

Code

1/*
2
3 Switch statement with serial input
4
5 Demonstrates the use of a switch statement. The switch statement allows you
6
7 to choose from among a set of discrete values of a variable. It's like a
8
9 series of if statements.
10
11 To see this sketch in action, open the Serial monitor and send any character.
12
13 The characters a, b, c, d, and e, will turn on LEDs. Any other character will
14
15 turn the LEDs off.
16
17 The circuit:
18
19 - five LEDs attached to digital pins 2 through 6 through 220 ohm resistors
20
21 created 1 Jul 2009
22
23 by Tom Igoe
24
25 This example code is in the public domain.
26
27 https://www.arduino.cc/en/Tutorial/SwitchCase2
28
29*/
30
31void setup() {
32
33 // initialize serial communication:
34
35 Serial.begin(9600);
36
37 // initialize the LED pins:
38
39 for (int thisPin = 2; thisPin < 7; thisPin++) {
40
41 pinMode(thisPin, OUTPUT);
42
43 }
44}
45
46void loop() {
47
48 // read the sensor:
49
50 if (Serial.available() > 0) {
51
52 int inByte = Serial.read();
53
54 // do something different depending on the character received.
55
56 // The switch statement expects single number values for each case; in this
57
58 // example, though, you're using single quotes to tell the controller to get
59
60 // the ASCII value for the character. For example 'a' = 97, 'b' = 98,
61
62 // and so forth:
63
64 switch (inByte) {
65
66 case 'a':
67
68 digitalWrite(2, HIGH);
69
70 break;
71
72 case 'b':
73
74 digitalWrite(3, HIGH);
75
76 break;
77
78 case 'c':
79
80 digitalWrite(4, HIGH);
81
82 break;
83
84 case 'd':
85
86 digitalWrite(5, HIGH);
87
88 break;
89
90 case 'e':
91
92 digitalWrite(6, HIGH);
93
94 break;
95
96 default:
97
98 // turn all the LEDs off:
99
100 for (int thisPin = 2; thisPin < 7; thisPin++) {
101
102 digitalWrite(thisPin, LOW);
103
104 }
105
106 }
107
108 }
109}

Learn more

You can find more basic tutorials in the built-in examples section.

You can also explore the language reference, a detailed collection of the Arduino programming language.

Last revision 2015/08/11 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.