jcarruthers

Serial to FastLED

Jul 4th, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. //setup LEDs
  4. #define NUM_LEDS 8
  5. #define DATA_PIN 15
  6. CRGB leds[NUM_LEDS];
  7.  
  8. //setup software serial buffer
  9. char buff[NUM_LEDS*3];
  10. int buffPos = 0;
  11. char buffChar;
  12.  
  13. void setup ()
  14. {
  15.   Serial.begin (115200);
  16.   FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  17. }
  18.  
  19. void loop()
  20. {
  21.   while (Serial.available() > 0) {
  22.     buffChar = Serial.read();
  23.     if (buffPos < NUM_LEDS*3) {
  24.       buff[buffPos] = buffChar;
  25.       buffPos++;
  26.       if (buffPos == (NUM_LEDS*3)) {
  27.         //clear leds and overwrite with buffer
  28.         memset(leds, 0, sizeof(leds));  
  29.         memcpy(leds, buff, sizeof(leds));
  30.         //clear buffer and reset position
  31.         memset(buff, 0, sizeof(buff));
  32.         buffPos = 0;
  33.       }
  34.     }
  35.   }
  36.   //update LEDs
  37.   FastLED.show();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment