Simple RGB LED controller for Mac OS X
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CaseLights.ino 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * CaseLights
  3. *
  4. * Arduino RGB LED Controller with Serial interface.
  5. *
  6. * Two commands are supported, ending with a new-line:
  7. *
  8. * RGB r g b
  9. *
  10. * UV s
  11. *
  12. * The RGB command sets the PWM output for the LEDs.
  13. * The UV command turns the UV lights on or off (s can be 0 or 1).
  14. */
  15. //#define DEBUG
  16. static int redPin = 10;
  17. static int greenPin = 9;
  18. static int bluePin = 11;
  19. static int uvPin = 13;
  20. void setup() {
  21. Serial.begin(115200);
  22. pinMode(redPin, OUTPUT);
  23. pinMode(greenPin, OUTPUT);
  24. pinMode(bluePin, OUTPUT);
  25. pinMode(uvPin, OUTPUT);
  26. analogWrite(redPin, 0);
  27. analogWrite(greenPin, 0);
  28. analogWrite(bluePin, 0);
  29. digitalWrite(uvPin, LOW);
  30. #ifdef DEBUG
  31. Serial.println("CaseLights initialized");
  32. #endif
  33. }
  34. void loop() {
  35. if (Serial.available() > 0) {
  36. int c = Serial.read();
  37. if (c == 'R') {
  38. c = Serial.read();
  39. if (c == 'G') {
  40. c = Serial.read();
  41. if (c == 'B') {
  42. int r = Serial.parseInt();
  43. int g = Serial.parseInt();
  44. int b = Serial.parseInt();
  45. analogWrite(redPin, r);
  46. analogWrite(greenPin, g);
  47. analogWrite(bluePin, b);
  48. #ifdef DEBUG
  49. Serial.print("RGB set ");
  50. Serial.print(r);
  51. Serial.print(' ');
  52. Serial.print(g);
  53. Serial.print(' ');
  54. Serial.print(b);
  55. Serial.println();
  56. #endif
  57. } else {
  58. #ifdef DEBUG
  59. Serial.print("Invalid character after G: ");
  60. Serial.print(c);
  61. Serial.println();
  62. #endif
  63. }
  64. } else {
  65. #ifdef DEBUG
  66. Serial.print("Invalid character after R: ");
  67. Serial.print(c);
  68. Serial.println();
  69. #endif
  70. }
  71. } else if (c == 'U') {
  72. c = Serial.read();
  73. if (c == 'V') {
  74. c = Serial.parseInt();
  75. if (c == 0) {
  76. digitalWrite(uvPin, LOW);
  77. #ifdef DEBUG
  78. Serial.println("UV off");
  79. #endif
  80. } else if (c == 1) {
  81. digitalWrite(uvPin, HIGH);
  82. #ifdef DEBUG
  83. Serial.println("UV on");
  84. #endif
  85. } else {
  86. #ifdef DEBUG
  87. Serial.print("Invalid character for UV: ");
  88. Serial.print(c);
  89. Serial.println();
  90. #endif
  91. }
  92. } else {
  93. #ifdef DEBUG
  94. Serial.print("Invalid character after U: ");
  95. Serial.print(c);
  96. Serial.println();
  97. #endif
  98. }
  99. } else if ((c == '\n') || (c == '\r')) {
  100. #ifdef DEBUG
  101. Serial.println("Skipping new-line or carriage-return...");
  102. #endif
  103. } else {
  104. #ifdef DEBUG
  105. Serial.print("Invalid character: ");
  106. Serial.print(c);
  107. Serial.println();
  108. #endif
  109. }
  110. }
  111. }