MrJoestrr

FastLED Serial Interpreter

Nov 28th, 2023
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN1     10
  4. #define LED_PIN2     11
  5. #define LED_PIN3     12
  6. #define NUM_LEDS     50
  7. #define LED_TYPE     WS2811
  8. #define COLOR_ORDER  GRB
  9. CRGB leds1[NUM_LEDS];
  10. CRGB leds2[NUM_LEDS];
  11. CRGB leds3[NUM_LEDS];
  12.  
  13. void setup(){
  14.   Serial.begin(115200);
  15.   FastLED.addLeds<LED_TYPE, LED_PIN1, COLOR_ORDER>(leds1, NUM_LEDS);
  16.   FastLED.addLeds<LED_TYPE, LED_PIN2, COLOR_ORDER>(leds2, NUM_LEDS);
  17.   FastLED.addLeds<LED_TYPE, LED_PIN3, COLOR_ORDER>(leds3, NUM_LEDS);
  18. }
  19.  
  20. void loop(){
  21.   int i= 0;
  22.  
  23.   while(1){
  24.  
  25.     // sync with header
  26.     // first character
  27.     while(!Serial.available());
  28.       if(Serial.read() != '>') {
  29.         continue;
  30.         }
  31.     //second character
  32.     while(!Serial.available());
  33.       if(Serial.read() != '>') {
  34.         continue;
  35.         }
  36.  
  37.     // get data for each led
  38.     while(i<NUM_LEDS){
  39.       //read data in batches of 3 (1 led at a time)
  40.       if (Serial.available() >= 3){
  41.  
  42.         leds1[i].g = Serial.read();
  43.         leds1[i].r = Serial.read();
  44.         leds1[i].b = Serial.read();
  45.  
  46.  
  47.         i++; // increment only if data has been read
  48.       }
  49.     }
  50.     i=0;
  51.  
  52.     while(i<NUM_LEDS){
  53.       //read data in batches of 3 (1 led at a time)
  54.       if (Serial.available() >= 3){
  55.  
  56.         leds2[i].g = Serial.read();
  57.         leds2[i].r = Serial.read();
  58.         leds2[i].b = Serial.read();
  59.  
  60.  
  61.         i++; // increment only if data has been read
  62.       }
  63.     }
  64.     i=0;
  65.  
  66.     while(i<NUM_LEDS){
  67.       //read data in batches of 3 (1 led at a time)
  68.       if (Serial.available() >= 3){
  69.  
  70.         leds3[i].g = Serial.read();
  71.         leds3[i].r = Serial.read();
  72.         leds3[i].b = Serial.read();
  73.  
  74.  
  75.         i++; // increment only if data has been read
  76.       }
  77.     }
  78.  
  79.      
  80.     FastLED.show();
  81.   }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment