Tutorial.SwitchCase2 History

Hide minor edits - Show changes to output

May 02, 2012, at 04:08 PM by Scott Fitzgerald -
May 02, 2012, at 04:05 PM by Scott Fitzgerald -
Changed line 39 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:15 AM by Scott Fitzgerald -
Changed line 39 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:15 AM by Scott Fitzgerald -
Changed line 39 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
September 23, 2010, at 10:36 PM by Christian Cerrito -
Changed lines 3-5 from:
!! Switch (case) Statement
!!! used with serial input
to:
!!!Switch (case) Statement, used with serial input
September 18, 2010, at 12:58 AM by Christian Cerrito -
Changed line 51 from:
* [[Tutorial/ifStatement | @@if()@@ statement]]
to:
* [[Tutorial/ifStatement | @@if()@@]]
September 18, 2010, at 12:58 AM by Christian Cerrito -
Added line 49:
* [[Serial/Available| @@serial.available()@@]]
September 18, 2010, at 12:56 AM by Christian Cerrito -
Changed lines 35-36 from:
%width=400px%[[Attach:forloop2_schem.png | Attach:forloop2_schem.png]]
to:
%width=400px%[[Attach:forLoop2_schem.png | Attach:forLoop2_schem.png]]
September 18, 2010, at 12:55 AM by Christian Cerrito -
Changed lines 35-36 from:
%width=400px%[[Attach:switchCase2_schem.png | Attach:switchCase2_schem.png]]
to:
%width=400px%[[Attach:forloop2_schem.png | Attach:forloop2_schem.png]]
September 18, 2010, at 12:54 AM by Christian Cerrito -
Changed lines 20-24 from:
Five LEDs are attached to digital pins 2, 3, 4, 5, and 6 through 220 ohm resistors.

To make this sketch work, open the Serial Monitor, and send the characters a, b, c, d, or e, or anything else.

to:
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 Arduino must be connected to your computer. Open the Serial Monitor, and send the characters a, b, c, d, or e, or anything else.

September 18, 2010, at 12:53 AM by Christian Cerrito -
Changed lines 6-7 from:
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 [[Refernce/SwitchCase | '''switch''']] statement. Switch allows you to choose between several discrete options.
to:
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 [[Reference/SwitchCase | '''switch''']] statement. Switch allows you to choose between several discrete options.
September 18, 2010, at 12:50 AM by Christian Cerrito -
Changed lines 6-7 from:
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 [[Refernce/Switchcase | '''switch''']] statement. Switch allows you to choose between several discrete options.
to:
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 [[Refernce/SwitchCase | '''switch''']] statement. Switch allows you to choose between several discrete options.
September 18, 2010, at 12:50 AM by Christian Cerrito -
Changed lines 6-7 from:
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 [[Refernce/Switch | '''switch''']] statement. Switch allows you to choose between several discrete options.
to:
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 [[Refernce/Switchcase | '''switch''']] statement. Switch allows you to choose between several discrete options.
September 18, 2010, at 12:49 AM by Christian Cerrito -
Changed lines 6-7 from:
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.
to:
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 [[Refernce/Switch | '''switch''']] statement. Switch allows you to choose between several discrete options.
September 16, 2010, at 10:17 PM by Tom Igoe -
September 16, 2010, at 10:14 PM by Tom Igoe -
Changed line 40 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
August 27, 2010, at 09:22 AM by Christian Cerrito -
Changed lines 51-52 from:
* [[Reference/digitalWrite]]()
to:
* [[Reference/digitalWrite | @@digitalWrite()@@]]
August 27, 2010, at 09:21 AM by Christian Cerrito -
Changed line 49 from:
* [[Reference/switchcase| @@switch() case@@]]
to:
* [[Reference/SwitchCase| @@switch() case@@]]
August 27, 2010, at 09:21 AM by Christian Cerrito -
Added lines 43-59:


!!!See Also:

* [[Serial/begin | @@serial.begin()@@ ]]
* [[Serial/read| @@serial.read()@@]]
* [[Reference/switchcase| @@switch() case@@]]
* [[Tutorial/ifStatement | @@if()@@ statement]]
* [[Reference/digitalWrite]]()

* [[Tutorial/SerialCallResponse | Serial Call Response]] - send multiple variables using a call and response (handshaking) method.
* [[Tutorial/SerialCallResponseASCII | Serial Call and Response ASCII]] - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
* [[Tutorial/IfStatement | If Statement]] - how to use an if statement to change output conditions based on changing input conditions.
* [[Tutorial/ForLoop | For Loop]]: - controlling multiple LEDs with a for loop.
* [[Tutorial/Array]]: a variation on the For Loop example that demonstrates how to use an array.
* [[Tutorial/While Loop]]: how to use a while loop to calibrate a sensor while a button is being read.
* [[Tutorial/Switch Case]]: how to choose between a discrete number of values. Equivalent to multiple If statements. This example shows how to divide a sensor's range into a set of four bands and to take four different actions depending on which band the result is in.
August 27, 2010, at 09:08 AM by Christian Cerrito -
Changed lines 10-17 from:
to:
!!!Hardware Required
* Arduino Board
* (5) LEDs
* (5) 220 ohm resistors
* breadboard
* hook-up wire

Changed lines 20-22 from:
Five LEDs are attached to digital pins 2, 3, 4, 5, and 6. To make the sketch work, open the Serial Monitor, and send the characters a, b, c, d, or e, or any other character.

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

To make this sketch work, open the Serial Monitor, and send the characters a, b, c, d, or e, or anything else.

August 27, 2010, at 09:04 AM by Christian Cerrito -
Changed line 31 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
August 27, 2010, at 09:04 AM by Christian Cerrito -
Changed line 31 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
February 23, 2010, at 11:24 PM by Tom Igoe -
Changed line 31 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/SwitchCase2/SwitchCase2.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
February 23, 2010, at 11:24 PM by Tom Igoe -
Changed lines 31-49 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Switch statement with serial input
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates the use of a switch statement. The switch
%color=#7e7e7e% statement allows you to choose from among a set of discrete values
%color=#7e7e7e% of a variable. It's like a series of if statements.
%color=#7e7e7e%
%color=#7e7e7e% To see this sketch in action, open the Serial monitor and send any character.
%color=#7e7e7e% The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
%color=#7e7e7e% the LEDs off.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
%color=#7e7e7e%
%color=#7e7e7e% created 1 Jul 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/SwitchCase2
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/SwitchCase2/SwitchCase2.pde lang=arduino tabwidth=4:)
Changed lines 33-78 from:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// initialize serial communication:
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600);
%color=#7e7e7e%// initialize the LED pins:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 7; thisPin++) {
%color=#cc6600%pinMode%%(thisPin, %color=#006699%OUTPUT%%);
}
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the sensor:
%color=#cc6600%if%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() > 0) {
%color=#cc6600%int%% inByte = %color=#cc6600%Serial%%.%color=#cc6600%read%%();
%color=#7e7e7e%// do something different depending on the character received.
%color=#7e7e7e%// The switch statement expects single number values for each case;
%color=#7e7e7e%// in this exmaple, though, you're using single quotes to tell
%color=#7e7e7e%// the controller to get the ASCII value for the character. For
%color=#7e7e7e%// example 'a' = 97, 'b' = 98, and so forth:

%color=#cc6600%switch%% (inByte) {
%color=#cc6600%case%% %color=#006699%'a'%%:
%color=#cc6600%digitalWrite%%(2, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'b'%%:
%color=#cc6600%digitalWrite%%(3, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'c'%%:
%color=#cc6600%digitalWrite%%(4, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'d'%%:
%color=#cc6600%digitalWrite%%(5, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'e'%%:
%color=#cc6600%digitalWrite%%(6, %color=#006699%HIGH%%);
break;
%color=#cc6600%default%%:
%color=#7e7e7e%// turn all the LEDs off:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 7; thisPin++) {
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%LOW%%);
}
}
}
}


(:divend:)
to:
(:divend:)
August 27, 2009, at 08:48 PM by Tom Igoe -
Added lines 20-21:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]
July 05, 2009, at 07:27 PM by Tom Igoe -
Changed lines 27-30 from:

[@
/*
Switch statement with serial input
to:
(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Switch statement with serial input
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates the use of a switch statement. The switch
%color=#7e7e7e% statement allows you to choose from among a set of discrete values
%color=#7e7e7e% of a variable. It's like a series of if statements.
%color=#7e7e7e%
%color=#7e7e7e% To see this sketch in action, open the Serial monitor and send any character.
%color=#7e7e7e% The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
%color=#7e7e7e% the LEDs off.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
%color=#7e7e7e%
%color=#7e7e7e% created 1 Jul 2009
%color=#7e7e7e% by Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://www.arduino.cc/en/Tutorial/SwitchCase2
%color=#7e7e7e% */
Changed lines 49-51 from:
Demonstrates the use of a switch statement. The switch
statement allows you to choose from among a set of discrete values
of a variable. It's like a series of if statements.
to:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// initialize serial communication:
%color=#cc6600%Serial%%.%color=#cc6600%begin%%(9600);
%color=#7e7e7e%// initialize the LED pins:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 7; thisPin++) {
%color=#cc6600%pinMode%%(thisPin, %color=#006699%OUTPUT%%);
}
}
Changed lines 58-60 from:
To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
the LEDs off.
to:
%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the sensor:
%color=#cc6600%if%% (%color=#cc6600%Serial%%.%color=#cc6600%available%%() > 0) {
%color=#cc6600%int%% inByte = %color=#cc6600%Serial%%.%color=#cc6600%read%%();
%color=#7e7e7e%// do something different depending on the character received.
%color=#7e7e7e%// The switch statement expects single number values for each case;
%color=#7e7e7e%// in this exmaple, though, you're using single quotes to tell
%color=#7e7e7e%// the controller to get the ASCII value for the character. For
%color=#7e7e7e%// example 'a' = 97, 'b' = 98, and so forth:
Changed lines 68-69 from:
The circuit:
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
to:
%color=#cc6600%switch%% (inByte) {
%color=#cc6600%case%% %color=#006699%'a'%%:
%color=#cc6600%digitalWrite%%(2, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'b'%%:
%color=#cc6600%digitalWrite%%(3, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'c'%%:
%color=#cc6600%digitalWrite%%(4, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'d'%%:
%color=#cc6600%digitalWrite%%(5, %color=#006699%HIGH%%);
break;
%color=#cc6600%case%% %color=#006699%'e'%%:
%color=#cc6600%digitalWrite%%(6, %color=#006699%HIGH%%);
break;
%color=#cc6600%default%%:
%color=#7e7e7e%// turn all the LEDs off:
%color=#cc6600%for%% (%color=#cc6600%int%% thisPin = 2; thisPin < 7; thisPin++) {
%color=#cc6600%digitalWrite%%(thisPin, %color=#006699%LOW%%);
}
}
}
}
Deleted lines 92-93:
created 1 Jul 2009
by Tom Igoe
Changed lines 94-140 from:
http://www.arduino.cc/en/Tutorial/SwitchCase2
*/

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:

switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}
}
@]
to:
(:divend:)
July 01, 2009, at 08:17 PM by Tom Igoe -
Changed line 41 from:
* 5 LEDs attached to digital pins 2 through 6
to:
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
Deleted line 91:
July 01, 2009, at 08:13 PM by Tom Igoe -
Changed lines 29-92 from:
to:
/*
Switch statement with serial input

Demonstrates the use of a switch statement. The switch
statement allows you to choose from among a set of discrete values
of a variable. It's like a series of if statements.

To see this sketch in action, open the Serial monitor and send any character.
The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
the LEDs off.

The circuit:
* 5 LEDs attached to digital pins 2 through 6

created 1 Jul 2009
by Tom Igoe

http://www.arduino.cc/en/Tutorial/SwitchCase2
*/

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:

switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
digitalWrite(5, HIGH);
break;
case 'e':
digitalWrite(6, HIGH);
break;
default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}
}
July 01, 2009, at 07:58 PM by Tom Igoe -
Changed lines 4-5 from:
!!! used with sensor input
to:
!!! used with serial input
July 01, 2009, at 07:55 PM by Tom Igoe -
Added lines 1-30:
''Examples > Control Structures''

!! Switch (case) Statement
!!! used with sensor input

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.


!!!Circuit

Five LEDs are attached to digital pins 2, 3, 4, 5, and 6. To make the sketch work, open the Serial Monitor, and send the characters a, b, c, d, or e, or any other character.


[-click the image to enlarge-]

%width=400px%[[Attach:switchCase2_bb.png | Attach:switchCase2_bb.png]]

'''Schematic:'''

[-click the image to enlarge-]

%width=400px%[[Attach:switchCase2_schem.png | Attach:switchCase2_schem.png]]

!!!Code

[@

@]

Share