piloteRobotWithSerial.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Switch statement with serial input
  3. Demonstrates the use of a switch statement. The switch
  4. statement allows you to choose from among a set of discrete values
  5. of a variable. It's like a series of if statements.
  6. To see this sketch in action, open the Serial monitor and send any character.
  7. The characters a, b, c, d, and e, will turn on LEDs. Any other character will turn
  8. the LEDs off.
  9. The circuit:
  10. * 5 LEDs attached to digital pins 2 through 6 through 220-ohm resistors
  11. created 1 Jul 2009
  12. by Tom Igoe
  13. This example code is in the public domain.
  14. http://www.arduino.cc/en/Tutorial/SwitchCase2
  15. */
  16. void setup() {
  17. // initialize serial communication:
  18. Serial.begin(9600);
  19. // initialize the LED pins:
  20. /*for (int thisPin = 2; thisPin < 7; thisPin++) {
  21. pinMode(thisPin, OUTPUT);
  22. }*/
  23. }
  24. void loop() {
  25. // read the sensor:
  26. if (Serial.available() > 0) {
  27. int inByte = Serial.read();
  28. // do something different depending on the character received.
  29. // The switch statement expects single number values for each case;
  30. // in this exmaple, though, you're using single quotes to tell
  31. // the controller to get the ASCII value for the character. For
  32. // example 'a' = 97, 'b' = 98, and so forth:
  33. switch (inByte) {
  34. case 'a':
  35. //digitalWrite(2, HIGH);
  36. Serial.println("a");
  37. break;
  38. case 'b':
  39. //digitalWrite(3, HIGH);
  40. Serial.println("b");
  41. break;
  42. case 'c':
  43. //digitalWrite(4, HIGH);
  44. Serial.println("c");
  45. break;
  46. case 'd':
  47. //digitalWrite(5, HIGH);
  48. Serial.println("d");
  49. break;
  50. case 'e':
  51. Serial.println("e");
  52. //digitalWrite(6, HIGH);
  53. break;
  54. default:
  55. Serial.println("f");
  56. // turn all the LEDs off:
  57. /*for (int thisPin = 2; thisPin < 7; thisPin++) {
  58. digitalWrite(thisPin, LOW);
  59. }*/
  60. }
  61. }
  62. }