Debian Codegraph @

examples/05.Control/switchCase/switchCase.ino

raw
1 /*
2 Switch statement
3
4 Demonstrates the use of a switch statement. The switch statement allows you
5 to choose from among a set of discrete values of a variable. It's like a
6 series of if statements.
7
8 To see this sketch in action, put the board and sensor in a well-lit room,
9 open the Serial Monitor, and move your hand gradually down over the sensor.
10
11 The circuit:
12 - photoresistor from analog in 0 to +5V
13 - 10K resistor from analog in 0 to ground
14
15 created 1 Jul 2009
16 modified 9 Apr 2012
17 by Tom Igoe
18
19 This example code is in the public domain.
20
21 https://www.arduino.cc/en/Tutorial/BuiltInExamples/SwitchCase
22 */
23
24 // these constants won't change. They are the lowest and highest readings you
25 // get from your sensor:
26 const int sensorMin = 0; // sensor minimum, discovered through experiment
27 const int sensorMax = 600; // sensor maximum, discovered through experiment
28
29 void setup() {
30 // initialize serial communication:
31 Serial.begin(9600);
32 }
33
34 void loop() {
35 // read the sensor:
36 int sensorReading = analogRead(A0);
37 // map the sensor range to a range of four options:
38 int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
39
40 // do something different depending on the range value:
41 switch (range) {
42 case 0: // your hand is on the sensor
43 Serial.println("dark");
44 break;
45 case 1: // your hand is close to the sensor
46 Serial.println("dim");
47 break;
48 case 2: // your hand is a few inches from the sensor
49 Serial.println("medium");
50 break;
51 case 3: // your hand is nowhere near the sensor
52 Serial.println("bright");
53 break;
54 }
55 delay(1); // delay in between reads for stability
56 }