Advertisement
Guest User

Untitled

a guest
May 1st, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. #include <RGBConverter.h>
  2. #include <FastLED.h>
  3.  
  4. String msg_raw = "";
  5. String msg_raw_col = "";
  6. int msg_led_num = 0;
  7. CRGB msg_col;
  8. bool col = false;
  9. bool led_num = false;
  10.  
  11. bool msg_complete = false;
  12.  
  13. CRGB leds[12];
  14.  
  15. void setup() {
  16. FastLED.addLeds<NEOPIXEL, 9>(leds, 12);
  17. Serial.begin(2000000);
  18. }
  19.  
  20. void loop() {
  21. if (msg_complete) {
  22. msg_complete = false;
  23. String msg = process_msg();
  24.  
  25. if (msg == "OFF")
  26. for (int i = 0; i < 12; i++)
  27. leds[i] = CRGB::Black;
  28.  
  29. else if (msg == "SOLID")
  30. for (int i = 0; i < 12; i++)
  31. leds[i] = msg_col;
  32.  
  33. else if (msg == "LED") {
  34. Serial.print(msg_led_num);
  35. leds[msg_led_num] = msg_col;
  36. }
  37.  
  38. msg_raw = "";
  39. msg_raw_col = "";
  40. col = false;
  41. led_num = false;
  42. msg_led_num = 0;
  43. FastLED.show();
  44. }
  45.  
  46. }
  47.  
  48. void serialEvent() {
  49. while (Serial.available() > 0) {
  50. char next_char = (char)Serial.read();
  51.  
  52. if (next_char == '\n')
  53. msg_complete = true;
  54.  
  55. if (!col && !led_num)
  56. msg_raw += next_char;
  57.  
  58. if (next_char == '|') {
  59. col = true;
  60. led_num = false;
  61. }
  62. else if (col)
  63. msg_raw_col += next_char;
  64.  
  65. if (next_char == '-')
  66. led_num = true;
  67. else if (led_num) {
  68. if (msg_led_num != 0)
  69. msg_led_num = 10;
  70.  
  71. msg_led_num += ((String)next_char).toInt();
  72. }
  73. }
  74. }
  75.  
  76. String process_msg() {
  77. if (msg_raw.length() < 1)
  78. return;
  79.  
  80. if (msg_raw_col.length() > 0)
  81. msg_col = CRGB(msg_raw_col.substring(0, 3).toInt(), msg_raw_col.substring(3, 6).toInt(), msg_raw_col.substring(6, 9).toInt());
  82.  
  83. return msg_raw.substring(1, msg_raw.length() - 1 );
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement