Hide minor edits - Show changes to markup
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/switchCase/switchCase.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/05.Control/switchCase/switchCase.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/new-extension/build/shared/examples/5.Control/switchCase/switchCase.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/switchCase/switchCase.ino lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/switchCase/switchCase.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/new-extension/build/shared/examples/5.Control/switchCase/switchCase.ino lang=arduino tabwidth=4:)
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.
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.
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.
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.
Serial.print()
serial.begin()
analogRead()?
map()
Serial.print()
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.
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.
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/switchCase/switchCase.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/switchCase/switchCase.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/Control/switchCase/switchCase.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/5.Control/switchCase/switchCase.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/Control/SwitchCase/SwitchCase.pde lang=arduino tabwidth=4:)
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/Control/switchCase/switchCase.pde lang=arduino tabwidth=4:)
/* 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 */
(:source http://github.com/arduino/Arduino/raw/master/build/shared/examples/Control/SwitchCase/SwitchCase.pde lang=arduino tabwidth=4:)
// 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; }
}
(:divend:)
(:divend:)
image developed using Fritzing. For more circuit examples, see the Fritzing project page
[@ /*
Switch statement
(:div class=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/SwitchCase */
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.
// these constants won't change: const int sensorMin = 0; // sensor minimum, discovered through experiment const int sensorMax = 600; // sensor maximum, discovered through experiment
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.
void setup() { // initialize serial communication: Serial.begin(9600); }
The circuit: * photoresistor from analog in 0 to +5V * 10K resistor from analog in 0 to ground
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);
created 1 Jul 2009 by Tom Igoe
// 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; }
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;
}
} @]
}
(:divend:)
http://www.arduino.cc/en/Tutorial/Loop
http://www.arduino.cc/en/Tutorial/SwitchCase
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 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.
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
/*
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;
}
}