123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /*
- Switch statement with serial input
- 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.
- To see this sketch in action, open the Serial monitor and send any character.
- The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
- the LEDs off.
- The circuit:
- * 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
- created 1 Jul 2009
- by Tom Igoe
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/SwitchCase2
- */
- void setup() {
- // initialize serial communication:
- Serial.begin(9600);
- // initialize the LED pins:
- /*for (int thisPin = 2; thisPin < 7; thisPin++) {
- pinMode(thisPin, OUTPUT);
- }*/
- }
- void loop() {
- // read the sensor:
- if (Serial.available() > 0) {
- int inByte = Serial.read();
- // 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:
- switch (inByte) {
- case 'a':
- //digitalWrite(2, HIGH);
- Serial.println("a");
- break;
- case 'b':
- //digitalWrite(3, HIGH);
- Serial.println("b");
- break;
- case 'c':
- //digitalWrite(4, HIGH);
- Serial.println("c");
- break;
- case 'd':
- //digitalWrite(5, HIGH);
- Serial.println("d");
- break;
- case 'e':
- Serial.println("e");
- //digitalWrite(6, HIGH);
- break;
- default:
- Serial.println("f");
- // turn all the LEDs off:
- /*for (int thisPin = 2; thisPin < 7; thisPin++) {
- digitalWrite(thisPin, LOW);
- }*/
- }
- }
- }
|