Advertisement
Guest User

WS2812B Rainbow

a guest
Apr 28th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1.  
  2. #include "FastLED.h"
  3.  
  4. FASTLED_USING_NAMESPACE
  5.  
  6. #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
  7. #warning "Requires FastLED 3.1 or later; check github for latest code."
  8. #endif
  9.  
  10. #define DATA_PIN 5
  11. //#define CLK_PIN 4
  12. #define LED_TYPE WS2812B
  13. #define COLOR_ORDER GRB
  14. #define NUM_LEDS 59
  15. CRGB leds[NUM_LEDS];
  16.  
  17. #define BRIGHTNESS 198
  18. #define FRAMES_PER_SECOND 120
  19.  
  20. void setup() {
  21. delay(3000); // 3 second delay for recovery
  22.  
  23. // tell FastLED about the LED strip configuration
  24. FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  25. //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  26.  
  27. // set master brightness control
  28. FastLED.setBrightness(BRIGHTNESS);
  29. }
  30.  
  31.  
  32. // List of patterns to cycle through. Each is defined as a separate function below.
  33. typedef void (*SimplePatternList[])();
  34. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  35.  
  36. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  37. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  38.  
  39. void loop()
  40. {
  41. // Call the current pattern function once, updating the 'leds' array
  42. gPatterns[gCurrentPatternNumber]();
  43.  
  44. // send the 'leds' array out to the actual LED strip
  45. FastLED.show();
  46. // insert a delay to keep the framerate modest
  47. FastLED.delay(1000/FRAMES_PER_SECOND);
  48.  
  49. // do some periodic updates
  50. EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  51. EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  52. }
  53.  
  54. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  55.  
  56. void nextPattern()
  57. {
  58. // add one to the current pattern number, and wrap around at the end
  59. gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  60. }
  61.  
  62. void rainbow()
  63. {
  64. // FastLED's built-in rainbow generator
  65. fill_rainbow( leds, NUM_LEDS, gHue, 7);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement