source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/examples/05.Control/switchCase/switchCase.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 3 years ago

Adding new version

File size: 1.6 KB
Line 
1/*
2  Switch statement
3
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.
7
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
10 down over the sensor.
11
12 The circuit:
13 * photoresistor from analog in 0 to +5V
14 * 10K resistor from analog in 0 to ground
15
16 created 1 Jul 2009
17 modified 9 Apr 2012
18 by Tom Igoe
19
20 This example code is in the public domain.
21
22 http://www.arduino.cc/en/Tutorial/SwitchCase
23 */
24
25// these constants won't change. They are the
26// lowest and highest readings you get from your sensor:
27const int sensorMin = 0;      // sensor minimum, discovered through experiment
28const int sensorMax = 600;    // sensor maximum, discovered through experiment
29
30void setup() {
31  // initialize serial communication:
32  Serial.begin(9600);
33}
34
35void loop() {
36  // read the sensor:
37  int sensorReading = analogRead(A0);
38  // map the sensor range to a range of four options:
39  int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
40
41  // do something different depending on the
42  // range value:
43  switch (range) {
44    case 0:    // your hand is on the sensor
45      Serial.println("dark");
46      break;
47    case 1:    // your hand is close to the sensor
48      Serial.println("dim");
49      break;
50    case 2:    // your hand is a few inches from the sensor
51      Serial.println("medium");
52      break;
53    case 3:    // your hand is nowhere near the sensor
54      Serial.println("bright");
55      break;
56  }
57  delay(1);        // delay in between reads for stability
58}
59
60
61
62
63
Note: See TracBrowser for help on using the repository browser.