Tutorial.SwitchCase History

Hide minor edits - Show changes to markup

May 02, 2012, at 04:05 PM by Scott Fitzgerald -
Changed line 42 from:
to:
November 16, 2011, at 04:17 AM by Scott Fitzgerald -
Changed line 42 from:
to:
September 30, 2011, at 03:19 AM by Scott Fitzgerald -
Changed line 42 from:
to:
September 24, 2010, at 05:58 AM by Tom Igoe -
Changed lines 51-52 from:
to:
September 24, 2010, at 05:58 AM by Tom Igoe -
Changed line 49 from:
to:
September 24, 2010, at 05:58 AM by Tom Igoe -
Changed line 49 from:
to:
September 23, 2010, at 10:40 PM by Christian Cerrito -
Changed lines 3-5 from:

Switch (case) Statement

used with sensor input

to:

Switch (case) Statement, used with sensor input

September 18, 2010, at 12:48 AM by Christian Cerrito -
Added lines 8-9:

This program 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.

Changed lines 40-41 from:

This program 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.

to:
September 18, 2010, at 12:47 AM by Christian Cerrito -
Deleted lines 7-8:

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.

Added lines 37-39:

This program 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.

September 18, 2010, at 12:46 AM by Christian Cerrito -
Changed lines 50-54 from:
to:
September 18, 2010, at 12:45 AM by Christian Cerrito -
Added lines 44-62:

See Also:

  • serial.begin()
  • analogRead()?
  • map()
  • Serial.print()

  • 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.

September 18, 2010, at 12:39 AM by Christian Cerrito -
Changed lines 36-37 from:
to:
September 18, 2010, at 12:25 AM by Christian Cerrito -
Changed lines 36-37 from:
to:
September 18, 2010, at 12:14 AM by Christian Cerrito -
Changed lines 10-18 from:
to:

Hardware Required

  • Arduino Board
  • (1) photocell, or analog sensor
  • (1) 10k ohm resistors
  • breadboard
  • hook-up wire

September 17, 2010, at 11:45 PM 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. This tutorial shows you how to use it to switch between four desired states of a photo resistor: really dark, dim, medium, and bright.

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

September 16, 2010, at 10:17 PM by Tom Igoe -
Changed line 33 from:
to:
September 16, 2010, at 01:54 AM by Christian Cerrito -
Changed line 33 from:
to:
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