Tutorial.SwitchCase2 History

Hide minor edits - Show changes to markup

June 01, 2010, at 11:15 PM by Equipo Traduccion -
Changed lines 1-17 from:

Examples > Control Structures

Switch (case) Statement

used with serial 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

to:

Ejemplos > Estructuras de control

Estructura Switch (case)

Usada con entrada serial

Una estructura 'if' te permite decidir entre dos opciones , verdadero o falso. cuando existe más de una opción, puedes usar muchos if o puedes usar la estructura switch. La estructura switch te permite elegir entre muchas opciones discretas. Este tutorial muestra como usar la estructura switch para elegir una opción dependiendo de cuatro estados diferentes en los que puede estar una foto resistencia: oscuro, poca ilumnicación, mediana iluminación y claro.

Este tutorial muestra como usar la estructura switch para encender uno entre varios LEDs dependiendo de un byte que se recibe mediante comunicación serial. Este programa espera que llegue un dato por el puerto serial, y enciende un LED diferente dependiendo si llega el carácter a, b, c, d, or e.

Circuito

Cinco LEDs se conectan a los pines digitales 2, 3, 4, 5, y 6. Para que el programa funcione, abre el Monitor Serial, y envia los caracteres a, b, c, d, o e, o cualquier otro carácter.

haz click en la imagen para aumentarla

Changed lines 20-25 from:

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

Schematic:

click the image to enlarge

to:

imagen hecha con Fritzing. Para más circuitos de ejemplo visita Fritzing project page

Esquemático:

haz click en la imagen para aumentarla

Changed line 28 from:

Code

to:

Código

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