Advertisement
Guest User

Untitled

a guest
Aug 30th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.52 KB | None | 0 0
  1. #include <Adafruit_CircuitPlayground.h>
  2. #include <Adafruit_Circuit_Playground.h>
  3. #include <FastLED.h>
  4.  
  5. FASTLED_USING_NAMESPACE
  6.  
  7. bool slideSwitch;
  8.  
  9. #define leftButtonPressed    CircuitPlayground.leftButton()
  10. #define rightButtonPressed   CircuitPlayground.rightButton()
  11. #define DATA_PIN    A1
  12. #define LED_TYPE    WS2812B
  13. #define COLOR_ORDER GRB
  14. #define NUM_LEDS    15
  15. CRGB leds[NUM_LEDS];
  16.  
  17. #define BRIGHTNESS          50
  18. #define FRAMES_PER_SECOND  120
  19.  
  20. void setup() {
  21.   delay(1000); // 1 second delay for recovery
  22.  
  23.   CircuitPlayground.begin();
  24.  
  25.   // tell FastLED about the LED strip configuration
  26.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  27.  
  28.   // set master brightness control
  29.   FastLED.setBrightness(BRIGHTNESS);
  30.  
  31. }
  32.  
  33.  
  34. // List of patterns to cycle through.  Each is defined as a separate function below.
  35. typedef void (*SimplePatternList[])();
  36. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm, demo };
  37.  
  38. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  39. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  40.  
  41. void loop()
  42. {
  43.   // Call the current pattern function once, updating the 'leds' array
  44.   gPatterns[gCurrentPatternNumber]();
  45.  
  46.   // send the 'leds' array out to the actual LED strip
  47.   FastLED.show();  
  48.   // insert a delay to keep the framerate modest
  49.   FastLED.delay(1000/FRAMES_PER_SECOND);
  50.  
  51.   if (rightButtonPressed)
  52.     {gHue = (gHue + 1); // step the "base color" through the rainbow with each press
  53.     }
  54.   if (leftButtonPressed)
  55.     {nextPattern(); // change patterns on left button press
  56.     delay(500);
  57.     }
  58. }
  59.  
  60. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  61.  
  62. void nextPattern()
  63. {
  64.   // add one to the current pattern number, and wrap around at the end
  65.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  66. }
  67.  
  68. void rainbow()
  69. {
  70.   // FastLED's built-in rainbow generator
  71.   fill_rainbow( leds, NUM_LEDS, gHue, 50);
  72. }
  73.  
  74. void rainbowWithGlitter()
  75. {
  76.   // built-in FastLED rainbow, plus some random sparkly glitter
  77.   rainbow();
  78.   addGlitter(80);
  79. }
  80.  
  81. void addGlitter( fract8 chanceOfGlitter)
  82. {
  83.   if( random8() < chanceOfGlitter) {
  84.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  85.   }
  86. }
  87.  
  88. void confetti()
  89. {
  90.   // random colored speckles that blink in and fade smoothly
  91.   fadeToBlackBy( leds, NUM_LEDS, 10);
  92.   int pos = random16(NUM_LEDS);
  93.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  94. }
  95.  
  96. void sinelon()
  97. {
  98.   // a colored dot sweeping back and forth, with fading trails
  99.   fadeToBlackBy( leds, NUM_LEDS, 25);
  100.   int pos = beatsin16( 40, 0, NUM_LEDS-1 );
  101.   leds[pos] += CHSV( gHue, 255, 192);
  102. }
  103.  
  104. void juggle()
  105. {
  106.   // eight colored dots, weaving in and out of sync with each other
  107.   fadeToBlackBy( leds, NUM_LEDS, 20);
  108.   byte dothue = 0;
  109.   for( int i = 0; i < 8; i++) {
  110.     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  111.     dothue += 32;
  112.   }
  113. }
  114.  
  115. void bpm()
  116. {
  117.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  118.   uint8_t BeatsPerMinute = 128;
  119.   CRGBPalette16 palette = RainbowColors_p;
  120.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  121.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  122.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  123.   }
  124. }
  125.  
  126. void demo()
  127. {
  128.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  129.   rainbow();
  130.   rainbowWithGlitter();
  131.   confetti();
  132.   sinelon();
  133.   juggle();
  134.   bpm();
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement