Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. #define NUM_LEDS (60)
  4. #define LED_DATA_PIN 4
  5. #define NUM_BYTES (NUM_LEDS*3) // 3 colors
  6.  
  7. #define BRIGHTNESS 255
  8. #define UPDATES_PER_SECOND 60
  9.  
  10. #define TIMEOUT 3000
  11.  
  12. #define MODE_ANIMATION 0
  13. #define MODE_AMBILIGHT 1
  14. #define MODE_BLACK 2
  15. uint8_t mode = MODE_ANIMATION;
  16.  
  17. byte MESSAGE_PREAMBLE[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
  18. uint8_t PREAMBLE_LENGTH = 10;
  19. uint8_t current_preamble_position = 0;
  20.  
  21. unsigned long last_serial_available = -1L;
  22.  
  23. int led_counter = 0;
  24. int byte_counter = 0;
  25.  
  26. CRGB leds[NUM_LEDS];
  27. byte buffer[NUM_BYTES];
  28.  
  29. // Filler animation attributes
  30. CRGBPalette16 currentPalette = RainbowColors_p;
  31. TBlendType currentBlending = LINEARBLEND;
  32. uint8_t startIndex = 0;
  33.  
  34. void setup();
  35. void loop();
  36. void processIncomingData();
  37. bool waitForPreamble(int timeout);
  38. void fillLEDsFromPaletteColors();
  39. void showBlack();
  40.  
  41. void setup()
  42. {
  43. Serial.begin(1000000); // 1000000
  44. FastLED.clear(true);
  45. FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, NUM_LEDS);
  46. FastLED.setBrightness(BRIGHTNESS);
  47. }
  48.  
  49. void loop()
  50. {
  51. switch (mode) {
  52. case MODE_ANIMATION:
  53. fillLEDsFromPaletteColors();
  54. break;
  55.  
  56. case MODE_AMBILIGHT:
  57. processIncomingData();
  58. break;
  59.  
  60. case MODE_BLACK:
  61. showBlack();
  62. break;
  63. }
  64.  
  65. }
  66.  
  67. void processIncomingData()
  68. {
  69. if (waitForPreamble(TIMEOUT))
  70. {
  71. Serial.readBytes(buffer, NUM_BYTES);
  72.  
  73. while (byte_counter < NUM_BYTES)
  74. {
  75. byte blue = buffer[byte_counter++];
  76. byte green = buffer[byte_counter++];
  77. byte red = buffer[byte_counter++];
  78.  
  79. leds[led_counter++] = CRGB(red, green, blue);
  80. }
  81.  
  82. FastLED.show();
  83.  
  84. // flush the serial buffer to avoid flickering
  85. while(Serial.available()) { Serial.read(); }
  86.  
  87. byte_counter = 0;
  88. led_counter = 0;
  89. }
  90. else
  91. {
  92. //if we get here, there must have been data before(so the user already knows, it works!)
  93. //simply go to black!
  94. mode = MODE_BLACK;
  95. }
  96. }
  97.  
  98. bool waitForPreamble(int timeout)
  99. {
  100. last_serial_available = millis();
  101. current_preamble_position = 0;
  102. while (current_preamble_position < PREAMBLE_LENGTH)
  103. {
  104. if (Serial.available() > 0)
  105. {
  106. last_serial_available = millis();
  107.  
  108. if (Serial.read() == MESSAGE_PREAMBLE[current_preamble_position])
  109. {
  110. current_preamble_position++;
  111. }
  112. else
  113. {
  114. current_preamble_position = 0;
  115. }
  116. }
  117.  
  118. if (millis() - last_serial_available > timeout)
  119. {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125.  
  126. void fillLEDsFromPaletteColors()
  127. {
  128. startIndex++;
  129.  
  130. uint8_t colorIndex = startIndex;
  131. for( int i = 0; i < NUM_LEDS; i++) {
  132. leds[i] = ColorFromPalette(currentPalette, colorIndex, BRIGHTNESS, currentBlending);
  133. colorIndex += 3;
  134. }
  135.  
  136. FastLED.delay(3000 / UPDATES_PER_SECOND);
  137.  
  138. if (Serial.available() > 0)
  139. {
  140. mode = MODE_AMBILIGHT;
  141. }
  142. }
  143.  
  144. void showBlack()
  145. {
  146. for( int i = 0; i < NUM_LEDS; i++)
  147. {
  148. leds[i] = CRGB(0,0,0);
  149. }
  150. FastLED.delay(3000 / UPDATES_PER_SECOND);
  151.  
  152. if (Serial.available() > 0)
  153. {
  154. mode = MODE_AMBILIGHT;
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement