~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to examples/Control/switchCase/switchCase.pde

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 by Tom Igoe 
 
18
 
 
19
 http://www.arduino.cc/en/Tutorial/SwitchCase
 
20
 */
 
21
 
 
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
 
25
 
 
26
void setup() {
 
27
  // initialize serial communication:
 
28
  Serial.begin(9600);  
 
29
}
 
30
 
 
31
void loop() {
 
32
  // read the sensor:
 
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);
 
36
 
 
37
  // do something different depending on the 
 
38
  // range value:
 
39
  switch (range) {
 
40
  case 0:    // your hand is on the sensor
 
41
    Serial.println("dark");
 
42
    break;
 
43
  case 1:    // your hand is close to the sensor
 
44
    Serial.println("dim");
 
45
    break;
 
46
  case 2:    // your hand is a few inches from the sensor
 
47
    Serial.println("medium");
 
48
    break;
 
49
  case 3:    // your hand is nowhere near the sensor
 
50
    Serial.println("bright");
 
51
    break;
 
52
  } 
 
53
 
 
54
}
 
55
 
 
56
 
 
57
 
 
58
 
 
59