Tutorial.SwitchCase History

Hide minor edits - Show changes to markup

June 01, 2010, at 11:08 PM by Equipo Traduccion -
Changed lines 3-5 from:

Switch (caso)

Usado con entrada de sensor

to:

Estructura Switch (caso)

Usada con entrada de sensor

June 01, 2010, at 11:02 PM by Equipo Traduccion -
Changed lines 4-5 from:

usada con entrada de sensor

to:

Usado con entrada de sensor

Changed lines 8-10 from:

el programa primero lee la tensión en la fotoresistencia. después utiliza la función map() para asignar de manera proporcional cuatro estados: 0, 1, 2, y 3. Finalmente, se usa la estructura de control switch para enviar al PC un mensaje dependiendo de cual valor de luminosidad se haya leído.

to:

El programa primero lee la tensión en la fotoresistencia. después utiliza la función map() para asignar de manera proporcional cuatro estados: 0, 1, 2, y 3. Finalmente, se usa la estructura de control switch para enviar al PC un mensaje dependiendo de cual valor de luminosidad se haya leído.

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

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 it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright.

The sketch first reads the photoresistor. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned.

Circuit

The photoresistor is connected to analog in pin 0 using a voltage divider circuit. A 10Kilohm resistor makes up the other side of the voltage divider, running from analog in 0 to ground. The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.

click the image to enlarge

to:

Ejempos > Estructuras de Control

Switch (caso)

usada con entrada de sensor

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.

el programa primero lee la tensión en la fotoresistencia. después utiliza la función map() para asignar de manera proporcional cuatro estados: 0, 1, 2, y 3. Finalmente, se usa la estructura de control switch para enviar al PC un mensaje dependiendo de cual valor de luminosidad se haya leído.

Circuito

La fotoresistencia se conecta a el pin 0 de entrada analógica usando un divisor de tensión. Una resistencia de 10K es la que se colocará en la otra resistencia del divisor de tensión, conectada entre la entrada analógica 0 y masa. La función analogRead() devuelve un valor en un rango de entre 0 y 600 aproximadamente si nos encontramos en una habitación razonablemente iluminada.

haz click en la imagen para aumentar

Changed lines 20-26 from:

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

Schematic:

click the image to enlarge

to:

imagen hecha usando Fritzing. Para más circuitos de ejemplo vista Fritzing project page

Esquemático:

haz click en la imagen para aumentar

Changed line 29 from:

Code

to:

Código

February 23, 2010, at 11:27 PM by Tom Igoe -
Changed line 33 from:
to:
February 23, 2010, at 11:27 PM by Tom Igoe -
Changed lines 33-52 from:
 /*
   Switch statement
  
  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, but the board and sensor in a well-lit
  room, open the serial monitor, and and move your hand gradually
  down over the sensor.
  
  The circuit:
  * photoresistor from analog in 0 to +5V
  * 10K resistor from analog in 0 to ground
  
  created 1 Jul 2009
  by Tom Igoe 
  
  http://www.arduino.cc/en/Tutorial/SwitchCase
  */
to:
Changed lines 35-68 from:
to:
August 27, 2009, at 08:48 PM by Tom Igoe -
Added lines 21-23:

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 28-31 from:

[@ /*

  Switch statement
to:
Changed lines 51-53 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:
 // these constants won't change:
 const int sensorMin = 0;      // sensor minimum, discovered through experiment
 const int sensorMax = 600;    // sensor maximum, discovered through experiment
Changed lines 55-57 from:
 To see this sketch in action, but the board and sensor in a well-lit
 room, open the serial monitor, and and move your hand gradually
 down over the sensor.
to:
 void setup() {
   // initialize serial communication:
   Serial.begin(9600);  
 }
Changed lines 60-62 from:
 The circuit:
 * photoresistor from analog in 0 to +5V
 * 10K resistor from analog in 0 to ground
to:
 void loop() {
   // read the sensor:
   int sensorReading = analogRead(0);
   // map the sensor range to a range of four options:
   int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
Changed lines 66-67 from:
 created 1 Jul 2009
 by Tom Igoe 
to:
   // do something different depending on the 
   // range value:
   switch (range) {
   case 0:    // your hand is on the sensor
     Serial.println("dark");
     break;
   case 1:    // your hand is close to the sensor
     Serial.println("dim");
     break;
   case 2:    // your hand is a few inches from the sensor
     Serial.println("medium");
     break;
   case 3:    // your hand is nowhere near the sensor
     Serial.println("bright");
     break;
   } 
Changed lines 83-119 from:
 http://www.arduino.cc/en/Tutorial/SwitchCase
 */

// these constants won't change: const int sensorMin = 0; // sensor minimum, discovered through experiment const int sensorMax = 600; // sensor maximum, discovered through experiment

void setup() {

  // initialize serial communication:
  Serial.begin(9600);  

}

void loop() {

  // read the sensor:
  int sensorReading = analogRead(0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    Serial.println("bright");
    break;
  } 

} @]

to:
July 01, 2009, at 07:59 PM by Tom Igoe -
Changed line 48 from:
 http://www.arduino.cc/en/Tutorial/Loop
to:
 http://www.arduino.cc/en/Tutorial/SwitchCase
July 01, 2009, at 07:29 PM by Tom Igoe -
Changed lines 3-4 from:

Switch (case) Statement (sensor input)

to:

Switch (case) Statement

used with sensor input

July 01, 2009, at 07:29 PM by Tom Igoe -
Added lines 1-83:

Examples > Control Structures

Switch (case) Statement (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 it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright.

The sketch first reads the photoresistor. Then it uses the map() function to map its output to one of four values: 0, 1, 2, or 3. Finally, it uses the switch() statement to print one of four messages back to the computer depending on which of the four values is returned.

Circuit

The photoresistor is connected to analog in pin 0 using a voltage divider circuit. A 10Kilohm resistor makes up the other side of the voltage divider, running from analog in 0 to ground. The analogRead() function returns a range of about 0 to 600 from this circuit in a reasonably lit indoor space.

click the image to enlarge

Schematic:

click the image to enlarge

Code

/*
  Switch statement

 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, but the board and sensor in a well-lit
 room, open the serial monitor, and and move your hand gradually
 down over the sensor.

 The circuit:
 * photoresistor from analog in 0 to +5V
 * 10K resistor from analog in 0 to ground

 created 1 Jul 2009
 by Tom Igoe 

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

// these constants won't change:
const int sensorMin = 0;      // sensor minimum, discovered through experiment
const int sensorMax = 600;    // sensor maximum, discovered through experiment

void setup() {
  // initialize serial communication:
  Serial.begin(9600);  
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(0);
  // map the sensor range to a range of four options:
  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

  // do something different depending on the 
  // range value:
  switch (range) {
  case 0:    // your hand is on the sensor
    Serial.println("dark");
    break;
  case 1:    // your hand is close to the sensor
    Serial.println("dim");
    break;
  case 2:    // your hand is a few inches from the sensor
    Serial.println("medium");
    break;
  case 3:    // your hand is nowhere near the sensor
    Serial.println("bright");
    break;
  } 

}

Share