Guest User

LED Panels with FastLED

a guest
Jan 7th, 2024
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.24 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include "FastLED.h"
  3.  
  4. FASTLED_USING_NAMESPACE
  5.  
  6. // settings
  7. #define DATA_PIN 5
  8. #define LED_TYPE WS2812B
  9. #define COLOR_ORDER GRB
  10. #define BRIGHTNESS 100
  11. #define PANEL_SIZE 32 // number of LEDs per panel
  12. #define PANEL_COUNT 8 // number of panels
  13.  
  14. CRGBArray<PANEL_COUNT * PANEL_SIZE> ledset; // setup our LED strip array
  15. CRGBPalette16 colorset;                     // our color palette
  16. CRGBSet *panel[PANEL_COUNT];                // pointer for each panel
  17. byte colPos = 0;                            // our position in the color set
  18. byte colRng = 64;                           // color palette range 1-255 (color change between each panel, higher = less difference)
  19. int speed = 200;                            // change speed in ms
  20.  
  21. void buildpal(int); // function that will build our color palettes
  22.  
  23. void setup()
  24. {
  25.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(ledset, (PANEL_COUNT * PANEL_SIZE)).setCorrection(TypicalLEDStrip);
  26.  
  27.   // create a CRGBSet for each panel
  28.   for (byte i = 0; i < PANEL_COUNT; i++)
  29.   {
  30.     int sled = PANEL_SIZE * i;
  31.     panel[i] = new CRGBSet(ledset(sled, (sled + PANEL_SIZE) - 1));
  32.   }
  33. }
  34.  
  35. void loop()
  36. {
  37.   if (colPos == 0) // only build our gradiant every cycle through not every draw event
  38.   {
  39.     buildpal(1); // select palette
  40.   }
  41.   *panel[0] = ColorFromPalette(colorset, (colPos * (255 / colRng)), BRIGHTNESS); // set color of 1st panel
  42.  
  43.   FastLED.delay(speed); // draw to LEDs & delay
  44.  
  45.   // slide the LED array over by 1 panel (1 -> 2, 2 -> 3, 3 -> 4, etc.)
  46.   memmove(&ledset[PANEL_SIZE], &ledset[0], sizeof(CRGB) * (PANEL_SIZE) * (PANEL_COUNT - 1));
  47.  
  48.   if (++colPos == colRng) // if we are at the end of our palette, start over
  49.   {
  50.     colPos = 0;
  51.   }
  52. }
  53.  
  54. void buildpal(int gstyle) // color palettes
  55. {
  56.   switch (gstyle)
  57.   {
  58.   case 0: // rainbow
  59.     colorset = RainbowColors_p;
  60.     break;
  61.   case 1: // party
  62.     colorset = PartyColors_p;
  63.     break;
  64.   case 2: // Ocean
  65.     colorset = OceanColors_p;
  66.     break;
  67.   case 3: // example of custom palette that will flow red -> blue -> red
  68.     colorset = CRGBPalette16(CRGB::Red, CRGB::Blue, CRGB::Red);
  69.     break;
  70.   default:  // if nothing matches return just white
  71.     colorset = CRGBPalette16(CRGB::White);
  72.     break;
  73.   }
  74. }
Add Comment
Please, Sign In to add comment