Tutorial.SwitchCase2 History

Hide minor edits - Show changes to markup

May 02, 2012, at 04:08 PM by Scott Fitzgerald -
May 02, 2012, at 04:05 PM by Scott Fitzgerald -
Changed line 39 from:
to:
November 16, 2011, at 04:15 AM by Scott Fitzgerald -
Changed line 39 from:
to:
September 30, 2011, at 03:15 AM by Scott Fitzgerald -
Changed line 39 from:
to:
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:
to:
September 18, 2010, at 12:58 AM by Christian Cerrito -
Added line 49:
September 18, 2010, at 12:56 AM by Christian Cerrito -
Changed lines 35-36 from:
to:
September 18, 2010, at 12:55 AM by Christian Cerrito -
Changed lines 35-36 from:
to:
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 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 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 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 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 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 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 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:
to:
August 27, 2010, at 09:22 AM by Christian Cerrito -
Changed lines 51-52 from:
to:
August 27, 2010, at 09:21 AM by Christian Cerrito -
Changed line 49 from:
to:
August 27, 2010, at 09:21 AM by Christian Cerrito -
Added lines 43-59:

See Also:

  • serial.begin()
  • serial.read()
  • switch() case?
  • if() statement
  • digitalWrite()

  • Serial Call Response - send multiple variables using a call and response (handshaking) method.
  • Serial Call and Response ASCII - send multiple vairables using a call-and-response (handshaking) method, and ASCII-encoding the values before sending.
  • If Statement - how to use an if statement to change output conditions based on changing input conditions.
  • For Loop: - controlling multiple LEDs with a for loop.
  • Array: a variation on the For Loop example that demonstrates how to use an array.
  • While Loop: how to use a while loop to calibrate a sensor while a button is being read.
  • 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:
to:
August 27, 2010, at 09:04 AM by Christian Cerrito -
Changed line 31 from:
to:
February 23, 2010, at 11:24 PM by Tom Igoe -
Changed line 31 from:
to:
February 23, 2010, at 11:24 PM by Tom Igoe -
Changed lines 31-49 from:
 /*
   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
  */
to:
Changed lines 33-78 from:
to:
August 27, 2009, at 08:48 PM by Tom Igoe -
Added lines 20-21:

image developed using Fritzing. For more circuit examples, see the Fritzing project page

July 05, 2009, at 07:27 PM by Tom Igoe -
Changed lines 27-30 from:

[@ /*

  Switch statement  with serial input
to:
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:
 void setup() {
   // initialize serial communication:
   Serial.begin(9600); 
    // initialize the LED pins:
       for (int thisPin = 2; thisPin < 7; thisPin++) {
         pinMode(thisPin, 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:
 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:
Changed lines 68-69 from:
 The circuit:
 * 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
to:
     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);
       }
     } 
   }
 }
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:
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

Schematic:

click the image to enlarge

Code



Share