2011-08-30 21:33:32 +02:00
|
|
|
/*
|
|
|
|
Switch statement
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
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.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
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.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
The circuit:
|
|
|
|
* photoresistor from analog in 0 to +5V
|
|
|
|
* 10K resistor from analog in 0 to ground
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
created 1 Jul 2009
|
2012-04-09 16:48:11 +02:00
|
|
|
modified 9 Apr 2012
|
2013-10-21 09:58:40 +02:00
|
|
|
by Tom Igoe
|
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2011-08-30 21:33:32 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/SwitchCase
|
|
|
|
*/
|
|
|
|
|
2012-04-09 16:48:11 +02:00
|
|
|
// these constants won't change. They are the
|
|
|
|
// lowest and highest readings you get from your sensor:
|
2011-08-30 21:33:32 +02:00
|
|
|
const int sensorMin = 0; // sensor minimum, discovered through experiment
|
|
|
|
const int sensorMax = 600; // sensor maximum, discovered through experiment
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
// initialize serial communication:
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.begin(9600);
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// read the sensor:
|
|
|
|
int sensorReading = analogRead(A0);
|
|
|
|
// map the sensor range to a range of four options:
|
|
|
|
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
// do something different depending on the
|
2011-08-30 21:33:32 +02:00
|
|
|
// range value:
|
|
|
|
switch (range) {
|
2013-10-21 09:58:40 +02:00
|
|
|
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;
|
|
|
|
}
|
2012-04-09 16:48:11 +02:00
|
|
|
delay(1); // delay in between reads for stability
|
2011-08-30 21:33:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|