Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.70 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #define FASTLED_ALLOW_INTERRUPTS 0
  3. #include <FastLED.h>
  4. FASTLED_USING_NAMESPACE
  5.  
  6. #define DATA_PIN            11
  7. #define NUM_LEDS            140
  8. #define MAX_POWER_MILLIAMPS 300
  9. #define LED_TYPE            WS2812B
  10. #define COLOR_ORDER         GRB
  11.  
  12.  
  13. CRGB leds[NUM_LEDS];
  14.  
  15. #ifdef __AVR__
  16.  #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  17. #endif
  18.  
  19. #define BUTTON_PIN   13
  20.  
  21. #define PIXEL_PIN    11  // Digital IO pin connected to the NeoPixels.
  22.  
  23. #define PIXEL_COUNT 20  // Number of NeoPixels
  24.  
  25. // Declare our NeoPixel strip object:
  26. Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  27. // Argument 1 = Number of pixels in NeoPixel strip
  28. // Argument 2 = Arduino pin number (most are valid)
  29. // Argument 3 = Pixel type flags, add together as needed:
  30. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  31. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  32. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  33. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  34. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  35.  
  36. boolean oldState = HIGH;
  37. int     mode     = 0;    // Currently-active animation mode, 0-9
  38.  
  39. void setup() {
  40.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  41.   strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  42.   strip.show();  // Initialize all pixels to 'off'
  43.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
  44.         .setCorrection( TypicalLEDStrip );
  45.   FastLED.setMaxPowerInVoltsAndMilliamps( 5, MAX_POWER_MILLIAMPS);
  46.  
  47. }
  48.  
  49. void loop() {
  50.   // Get current button state.
  51.   boolean newState = digitalRead(BUTTON_PIN);
  52.  
  53.   // Check if state changed from high to low (button press).
  54.   if((newState == LOW) && (oldState == HIGH)) {
  55.     // Short delay to debounce button.
  56.     delay(15);
  57.     // Check if button is still low after debounce.
  58.     newState = digitalRead(BUTTON_PIN);
  59.     pacificaStart();
  60.     if(newState == LOW) {      // Yes, still low
  61.       if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
  62.       switch(mode) {           // Start the new animation...
  63.         case 0:
  64.           colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
  65.           break;
  66.         case 1:
  67.           colorWipe(strip.Color(255,   0,   0), 50);    // Red
  68.           break;
  69.         case 2:
  70.           colorWipe(strip.Color(  3, 252,   236), 50);    // Cyan
  71.           break;
  72.         case 3:
  73.           colorWipe(strip.Color(  0,   0, 255), 50);    // Blue
  74.           break;
  75.         case 4:
  76.           theaterChase(strip.Color(127, 127, 127), 50); // White
  77.           break;
  78.         case 5:
  79.           theaterChase(strip.Color(127,   0,   0), 50); // Red
  80.           break;
  81.         case 6:
  82.           theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  83.           break;
  84.         case 7:
  85.           rainbow(10);
  86.           break;
  87.         case 8:
  88.           theaterChaseRainbow(50);
  89.           break;
  90.       }
  91.     }
  92.   }
  93.  
  94.   // Set the last-read button state to the old state.
  95.   oldState = newState;
  96. }
  97.  
  98. // Fill strip pixels one after another with a color. Strip is NOT cleared
  99. // first; anything there will be covered pixel by pixel. Pass in color
  100. // (as a single 'packed' 32-bit value, which you can get by calling
  101. // strip.Color(red, green, blue) as shown in the loop() function above),
  102. // and a delay time (in milliseconds) between pixels.
  103. void colorWipe(uint32_t color, int wait) {
  104.   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  105.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  106.     strip.show();                          //  Update strip to match
  107.     delay(wait);                           //  Pause for a moment
  108.   }
  109. }
  110.  
  111. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  112. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  113. // between frames.
  114. void theaterChase(uint32_t color, int wait) {
  115.   for(int a=0; a<10; a++) {  // Repeat 10 times...
  116.     for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
  117.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  118.       // 'c' counts up from 'b' to end of strip in steps of 3...
  119.       for(int c=b; c<strip.numPixels(); c += 3) {
  120.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  121.       }
  122.       strip.show(); // Update strip with new contents
  123.       delay(wait);  // Pause for a moment
  124.     }
  125.   }
  126. }
  127.  
  128. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  129. void rainbow(int wait) {
  130.   // Hue of first pixel runs 3 complete loops through the color wheel.
  131.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  132.   // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
  133.   // means we'll make 3*65536/256 = 768 passes through this outer loop:
  134.   for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
  135.     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  136.       // Offset pixel hue by an amount to make one full revolution of the
  137.       // color wheel (range of 65536) along the length of the strip
  138.       // (strip.numPixels() steps):
  139.       int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  140.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  141.       // optionally add saturation and value (brightness) (each 0 to 255).
  142.       // Here we're using just the single-argument hue variant. The result
  143.       // is passed through strip.gamma32() to provide 'truer' colors
  144.       // before assigning to each pixel:
  145.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  146.     }
  147.     strip.show(); // Update strip with new contents
  148.     delay(wait);  // Pause for a moment
  149.   }
  150. }
  151.  
  152. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  153. void theaterChaseRainbow(int wait) {
  154.   int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  155.   for(int a=0; a<30; a++) {  // Repeat 30 times...
  156.     for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
  157.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  158.       // 'c' counts up from 'b' to end of strip in increments of 3...
  159.       for(int c=b; c<strip.numPixels(); c += 3) {
  160.         // hue of pixel 'c' is offset by an amount to make one full
  161.         // revolution of the color wheel (range 65536) along the length
  162.         // of the strip (strip.numPixels() steps):
  163.         int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
  164.         uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  165.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  166.       }
  167.       strip.show();                // Update strip with new contents
  168.       delay(wait);                 // Pause for a moment
  169.       firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  170.     }
  171.   }
  172. }
  173.  
  174. void pacificaStart()
  175. {
  176.   EVERY_N_MILLISECONDS( 10) {
  177.     pacifica_loop();
  178.     FastLED.show();
  179.   }
  180. }
  181.  
  182. CRGBPalette16 pacifica_palette_1 =
  183.     { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117,
  184.       0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x14554B, 0x28AA50 };
  185. CRGBPalette16 pacifica_palette_2 =
  186.     { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117,
  187.       0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x0C5F52, 0x19BE5F };
  188. CRGBPalette16 pacifica_palette_3 =
  189.     { 0x000208, 0x00030E, 0x000514, 0x00061A, 0x000820, 0x000927, 0x000B2D, 0x000C33,
  190.       0x000E39, 0x001040, 0x001450, 0x001860, 0x001C70, 0x002080, 0x1040BF, 0x2060FF };
  191.  
  192.  
  193. void pacifica_loop()
  194. {
  195.   // Increment the four "color index start" counters, one for each wave layer.
  196.   // Each is incremented at a different speed, and the speeds vary over time.
  197.   static uint16_t sCIStart1, sCIStart2, sCIStart3, sCIStart4;
  198.   static uint32_t sLastms = 0;
  199.   uint32_t ms = GET_MILLIS();
  200.   uint32_t deltams = ms - sLastms;
  201.   sLastms = ms;
  202.   uint16_t speedfactor1 = beatsin16(3, 179, 269);
  203.   uint16_t speedfactor2 = beatsin16(4, 179, 269);
  204.   uint32_t deltams1 = (deltams * speedfactor1) / 256;
  205.   uint32_t deltams2 = (deltams * speedfactor2) / 256;
  206.   uint32_t deltams21 = (deltams1 + deltams2) / 2;
  207.   sCIStart1 += (deltams1 * beatsin88(1011,10,13));
  208.   sCIStart2 -= (deltams21 * beatsin88(777,8,11));
  209.   sCIStart3 -= (deltams1 * beatsin88(501,5,7));
  210.   sCIStart4 -= (deltams2 * beatsin88(257,4,6));
  211.  
  212.   // Clear out the LED array to a dim background blue-green
  213.   fill_solid( leds, NUM_LEDS, CRGB( 2, 6, 10));
  214.  
  215.   // Render each of four layers, with different scales and speeds, that vary over time
  216.   pacifica_one_layer( pacifica_palette_1, sCIStart1, beatsin16( 3, 11 * 256, 14 * 256), beatsin8( 10, 70, 130), 0-beat16( 301) );
  217.   pacifica_one_layer( pacifica_palette_2, sCIStart2, beatsin16( 4,  6 * 256,  9 * 256), beatsin8( 17, 40,  80), beat16( 401) );
  218.   pacifica_one_layer( pacifica_palette_3, sCIStart3, 6 * 256, beatsin8( 9, 10,38), 0-beat16(503));
  219.   pacifica_one_layer( pacifica_palette_3, sCIStart4, 5 * 256, beatsin8( 8, 10,28), beat16(601));
  220.  
  221.   // Add brighter 'whitecaps' where the waves lines up more
  222.   //pacifica_add_whitecaps();
  223.  
  224.   // Deepen the blues and greens a bit
  225.   pacifica_deepen_colors();
  226. }
  227.  
  228. // Add one layer of waves into the led array
  229. void pacifica_one_layer( CRGBPalette16& p, uint16_t cistart, uint16_t wavescale, uint8_t bri, uint16_t ioff)
  230. {
  231.   uint16_t ci = cistart;
  232.   uint16_t waveangle = ioff;
  233.   uint16_t wavescale_half = (wavescale / 2) + 20;
  234.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  235.     waveangle += 250;
  236.     uint16_t s16 = sin16( waveangle ) + 32768;
  237.     uint16_t cs = scale16( s16 , wavescale_half ) + wavescale_half;
  238.     ci += cs;
  239.     uint16_t sindex16 = sin16( ci) + 32768;
  240.     uint8_t sindex8 = scale16( sindex16, 240);
  241.     CRGB c = ColorFromPalette( p, sindex8, bri, LINEARBLEND);
  242.     leds[i] += c;
  243.   }
  244. }
  245.  
  246. // Add extra 'white' to areas where the four layers of light have lined up brightly
  247. void pacifica_add_whitecaps()
  248. {
  249.   uint8_t basethreshold = beatsin8( 9, 55, 65);
  250.   uint8_t wave = beat8( 7 );
  251.  
  252.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  253.     uint8_t threshold = scale8( sin8( wave), 20) + basethreshold;
  254.     wave += 7;
  255.     uint8_t l = leds[i].getAverageLight();
  256.     if( l > threshold) {
  257.       uint8_t overage = l - threshold;
  258.       uint8_t overage2 = qadd8( overage, overage);
  259.       leds[i] += CRGB( overage, overage2, qadd8( overage2, overage2));
  260.     }
  261.   }
  262. }
  263.  
  264. // Deepen the blues and greens
  265. void pacifica_deepen_colors()
  266. {
  267.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  268.     leds[i].blue = scale8( leds[i].blue,  145);
  269.     //leds[i].green= scale8( leds[i].green, 200);
  270.     leds[i] |= CRGB( 2, 5, 7);
  271.   }
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement