Show minor edits - Show changes to markup
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/05.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/5.Control/switchCase2/switchCase2.ino lang=arduino tabwidth=4:)
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.
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.
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.
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.
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.
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.
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.
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
serial.begin()
serial.read()
switch() case?
if() statement
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.
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.
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/5.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/4.Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/SwitchCase2/SwitchCase2.pde lang=arduino tabwidth=4:)
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/switchCase2/switchCase2.pde lang=arduino tabwidth=4:)
/* 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 through 220-ohm resistors created 1 Jul 2009 by Tom Igoe http://www.arduino.cc/en/Tutorial/SwitchCase2 */
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Control/SwitchCase2/SwitchCase2.pde lang=arduino tabwidth=4:)
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); } } } }
(:divend:)
(:divend:)
image developed using Fritzing. For more circuit examples, see the Fritzing project page
[@ /*
Switch statement with serial input
(:div class=code :)
/* 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 through 220-ohm resistors created 1 Jul 2009 by Tom Igoe http://www.arduino.cc/en/Tutorial/SwitchCase2 */
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.
void setup() { // initialize serial communication: Serial.begin(9600); // initialize the LED pins: for (int thisPin = 2; thisPin < 7; thisPin++) { pinMode(thisPin, OUTPUT); } }
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.
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:
The circuit:
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
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); } } } }
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);
}
}
}
} @]
(:divend:)
* 5 LEDs attached to digital pins 2 through 6
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
/*
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);
}
}
}
}
Examples > Control Structures
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.
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
Schematic:
click the image to enlarge