mirror of https://github.com/noisymime/Arduino.git
Added Switch-case 2
This commit is contained in:
parent
dcacaae0bb
commit
072a86826c
|
@ -10,7 +10,7 @@
|
||||||
the LEDs off.
|
the LEDs off.
|
||||||
|
|
||||||
The circuit:
|
The circuit:
|
||||||
* 5 LEDs attached to digital pins 2 through 6
|
* 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
|
||||||
|
|
||||||
created 1 Jul 2009
|
created 1 Jul 2009
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
@ -18,41 +18,47 @@
|
||||||
http://www.arduino.cc/en/Tutorial/SwitchCase2
|
http://www.arduino.cc/en/Tutorial/SwitchCase2
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// these constants won't change:
|
|
||||||
const int sensorMin = 0; // sensor minimum, discovered through experiment
|
|
||||||
const int sensorMax = 600; // sensor maximum, discovered through experiment
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// initialize serial communication:
|
// initialize serial communication:
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
// initialize the LED pins:
|
||||||
|
for (int thisPin = 2; thisPin < 7; thisPin++) {
|
||||||
|
pinMode(thisPin, OUTPUT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// read the sensor:
|
// read the sensor:
|
||||||
int sensorReading = analogRead(0);
|
if (Serial.available() > 0) {
|
||||||
// map the sensor range to a range of four options:
|
int inByte = Serial.read();
|
||||||
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);
|
// do something different depending on the character received.
|
||||||
|
// The switch statement expects single number values for each case;
|
||||||
|
// in this exmaple, though, you're using single quotes to tell
|
||||||
|
// the controller to get the ASCII value for the character. For
|
||||||
|
// example 'a' = 97, 'b' = 98, and so forth:
|
||||||
|
|
||||||
// do something different depending on the
|
switch (inByte) {
|
||||||
// range value:
|
case 'a':
|
||||||
switch (range) {
|
digitalWrite(2, HIGH);
|
||||||
case 0: // your hand is on the sensor
|
|
||||||
Serial.println("dark");
|
|
||||||
break;
|
break;
|
||||||
case 1: // your hand is close to the sensor
|
case 'b':
|
||||||
Serial.println("dim");
|
digitalWrite(3, HIGH);
|
||||||
break;
|
break;
|
||||||
case 2: // your hand is a few inches from the sensor
|
case 'c':
|
||||||
Serial.println("medium");
|
digitalWrite(4, HIGH);
|
||||||
break;
|
break;
|
||||||
case 3: // your hand is nowhere near the sensor
|
case 'd':
|
||||||
Serial.println("bright");
|
digitalWrite(5, HIGH);
|
||||||
break;
|
break;
|
||||||
|
case 'e':
|
||||||
|
digitalWrite(6, HIGH);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// turn all the LEDs off:
|
||||||
|
for (int thisPin = 2; thisPin < 7; thisPin++) {
|
||||||
|
digitalWrite(thisPin, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue