4
Demonstrates the use of a switch statement. The switch
5
statement allows you to choose from among a set of discrete values
6
of a variable. It's like a series of if statements.
8
To see this sketch in action, but the board and sensor in a well-lit
9
room, open the serial monitor, and and move your hand gradually
13
* photoresistor from analog in 0 to +5V
14
* 10K resistor from analog in 0 to ground
19
http://www.arduino.cc/en/Tutorial/SwitchCase
22
// these constants won't change:
23
const int sensorMin = 0; // sensor minimum, discovered through experiment
24
const int sensorMax = 600; // sensor maximum, discovered through experiment
27
// initialize serial communication:
33
int sensorReading = analogRead(0);
34
// map the sensor range to a range of four options:
35
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
37
// do something different depending on the
40
case 0: // your hand is on the sensor
41
Serial.println("dark");
43
case 1: // your hand is close to the sensor
44
Serial.println("dim");
46
case 2: // your hand is a few inches from the sensor
47
Serial.println("medium");
49
case 3: // your hand is nowhere near the sensor
50
Serial.println("bright");