Simple RGB LED controller for Mac OS X
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CaseLights.ino 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. enum LoopState {
  17. LOOP_IDLE,
  18. LOOP_R,
  19. LOOP_G,
  20. LOOP_B,
  21. LOOP_NUM1,
  22. LOOP_NUM2,
  23. LOOP_U,
  24. LOOP_V
  25. };
  26. static int redPin = 10;
  27. static int greenPin = 9;
  28. static int bluePin = 11;
  29. static int uvPin = 13;
  30. static LoopState state = LOOP_IDLE;
  31. static int r = 0, g = 0, b = 0;
  32. void setup() {
  33. Serial.begin(115200);
  34. Serial.setTimeout(5000);
  35. pinMode(redPin, OUTPUT);
  36. pinMode(greenPin, OUTPUT);
  37. pinMode(bluePin, OUTPUT);
  38. pinMode(uvPin, OUTPUT);
  39. analogWrite(redPin, 0);
  40. analogWrite(greenPin, 0);
  41. analogWrite(bluePin, 0);
  42. digitalWrite(uvPin, LOW);
  43. #ifdef DEBUG
  44. Serial.println("CaseLights initialized");
  45. #endif
  46. }
  47. void loop() {
  48. if (Serial.available() > 0) {
  49. if (state == LOOP_IDLE) {
  50. int c = Serial.read();
  51. if ((c == 'R') || (c == 'r')) {
  52. state = LOOP_R;
  53. } else if ((c == 'U') || (c == 'u')) {
  54. state = LOOP_U;
  55. } else if ((c == '\r') || (c == '\n')) {
  56. #ifdef DEBUG
  57. Serial.println("Skipping newline...");
  58. #endif
  59. } else {
  60. #ifdef DEBUG
  61. Serial.print("Invalid character: ");
  62. Serial.print(c);
  63. Serial.println();
  64. #endif
  65. }
  66. } else if (state == LOOP_R) {
  67. int c = Serial.read();
  68. if ((c == 'G') || (c == 'g')) {
  69. state = LOOP_G;
  70. } else {
  71. state = LOOP_IDLE;
  72. #ifdef DEBUG
  73. Serial.print("Invalid character after R: ");
  74. Serial.print(c);
  75. Serial.println();
  76. #endif
  77. }
  78. } else if (state == LOOP_G) {
  79. int c = Serial.read();
  80. if ((c == 'B') || (c == 'b')) {
  81. state = LOOP_B;
  82. } else {
  83. state = LOOP_IDLE;
  84. #ifdef DEBUG
  85. Serial.print("Invalid character after G: ");
  86. Serial.print(c);
  87. Serial.println();
  88. #endif
  89. }
  90. } else if (state == LOOP_B) {
  91. r = Serial.parseInt();
  92. state = LOOP_NUM1;
  93. } else if (state == LOOP_NUM1) {
  94. g = Serial.parseInt();
  95. state = LOOP_NUM2;
  96. } else if (state == LOOP_NUM2) {
  97. b = Serial.parseInt();
  98. analogWrite(redPin, r);
  99. analogWrite(greenPin, g);
  100. analogWrite(bluePin, b);
  101. #ifdef DEBUG
  102. Serial.print("RGB set ");
  103. Serial.print(r);
  104. Serial.print(' ');
  105. Serial.print(g);
  106. Serial.print(' ');
  107. Serial.print(b);
  108. Serial.println();
  109. #endif
  110. state = LOOP_IDLE;
  111. } else if (state == LOOP_U) {
  112. int c = Serial.read();
  113. if ((c == 'V') || (c == 'v')) {
  114. state = LOOP_V;
  115. } else {
  116. state = LOOP_IDLE;
  117. #ifdef DEBUG
  118. Serial.print("Invalid character after U: ");
  119. Serial.print(c);
  120. Serial.println();
  121. #endif
  122. }
  123. } else if (state == LOOP_V) {
  124. int n = Serial.parseInt();
  125. if (n == 0) {
  126. digitalWrite(uvPin, LOW);
  127. #ifdef DEBUG
  128. Serial.println("UV off");
  129. #endif
  130. } else {
  131. digitalWrite(uvPin, HIGH);
  132. #ifdef DEBUG
  133. Serial.println("UV on");
  134. #endif
  135. }
  136. state = LOOP_IDLE;
  137. } else {
  138. state = LOOP_IDLE;
  139. #ifdef DEBUG
  140. Serial.print("Invalid state: ");
  141. Serial.print(state);
  142. Serial.println();
  143. #endif
  144. }
  145. }
  146. }