Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.74 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.  
  60.     if(newState == LOW) {      // Yes, still low
  61.       if(++mode > 9) 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.         case 9:
  91.           pacificaStart();
  92.           break
  93.       }
  94.     }
  95.   }
  96.  
  97.   // Set the last-read button state to the old state.
  98.   oldState = newState;
  99. }
  100.  
  101. // Fill strip pixels one after another with a color. Strip is NOT cleared
  102. // first; anything there will be covered pixel by pixel. Pass in color
  103. // (as a single 'packed' 32-bit value, which you can get by calling
  104. // strip.Color(red, green, blue) as shown in the loop() function above),
  105. // and a delay time (in milliseconds) between pixels.
  106. void colorWipe(uint32_t color, int wait) {
  107.   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  108.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  109.     strip.show();                          //  Update strip to match
  110.     delay(wait);                           //  Pause for a moment
  111.   }
  112. }
  113.  
  114. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  115. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  116. // between frames.
  117. void theaterChase(uint32_t color, int wait) {
  118.   for(int a=0; a<10; a++) {  // Repeat 10 times...
  119.     for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
  120.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  121.       // 'c' counts up from 'b' to end of strip in steps of 3...
  122.       for(int c=b; c<strip.numPixels(); c += 3) {
  123.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  124.       }
  125.       strip.show(); // Update strip with new contents
  126.       delay(wait);  // Pause for a moment
  127.     }
  128.   }
  129. }
  130.  
  131. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  132. void rainbow(int wait) {
  133.   // Hue of first pixel runs 3 complete loops through the color wheel.
  134.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  135.   // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
  136.   // means we'll make 3*65536/256 = 768 passes through this outer loop:
  137.   for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
  138.     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  139.       // Offset pixel hue by an amount to make one full revolution of the
  140.       // color wheel (range of 65536) along the length of the strip
  141.       // (strip.numPixels() steps):
  142.       int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  143.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  144.       // optionally add saturation and value (brightness) (each 0 to 255).
  145.       // Here we're using just the single-argument hue variant. The result
  146.       // is passed through strip.gamma32() to provide 'truer' colors
  147.       // before assigning to each pixel:
  148.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  149.     }
  150.     strip.show(); // Update strip with new contents
  151.     delay(wait);  // Pause for a moment
  152.   }
  153. }
  154.  
  155. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  156. void theaterChaseRainbow(int wait) {
  157.   int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  158.   for(int a=0; a<30; a++) {  // Repeat 30 times...
  159.     for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
  160.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  161.       // 'c' counts up from 'b' to end of strip in increments of 3...
  162.       for(int c=b; c<strip.numPixels(); c += 3) {
  163.         // hue of pixel 'c' is offset by an amount to make one full
  164.         // revolution of the color wheel (range 65536) along the length
  165.         // of the strip (strip.numPixels() steps):
  166.         int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
  167.         uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  168.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  169.       }
  170.       strip.show();                // Update strip with new contents
  171.       delay(wait);                 // Pause for a moment
  172.       firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  173.     }
  174.   }
  175. }
  176.  
  177. void pacificaStart()
  178. {
  179.   EVERY_N_MILLISECONDS( 10) {
  180.     pacifica_loop();
  181.     FastLED.show();
  182.   }
  183. }
  184.  
  185. CRGBPalette16 pacifica_palette_1 =
  186.     { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117,
  187.       0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x14554B, 0x28AA50 };
  188. CRGBPalette16 pacifica_palette_2 =
  189.     { 0x000507, 0x000409, 0x00030B, 0x00030D, 0x000210, 0x000212, 0x000114, 0x000117,
  190.       0x000019, 0x00001C, 0x000026, 0x000031, 0x00003B, 0x000046, 0x0C5F52, 0x19BE5F };
  191. CRGBPalette16 pacifica_palette_3 =
  192.     { 0x000208, 0x00030E, 0x000514, 0x00061A, 0x000820, 0x000927, 0x000B2D, 0x000C33,
  193.       0x000E39, 0x001040, 0x001450, 0x001860, 0x001C70, 0x002080, 0x1040BF, 0x2060FF };
  194.  
  195.  
  196. void pacifica_loop()
  197. {
  198.   // Increment the four "color index start" counters, one for each wave layer.
  199.   // Each is incremented at a different speed, and the speeds vary over time.
  200.   static uint16_t sCIStart1, sCIStart2, sCIStart3, sCIStart4;
  201.   static uint32_t sLastms = 0;
  202.   uint32_t ms = GET_MILLIS();
  203.   uint32_t deltams = ms - sLastms;
  204.   sLastms = ms;
  205.   uint16_t speedfactor1 = beatsin16(3, 179, 269);
  206.   uint16_t speedfactor2 = beatsin16(4, 179, 269);
  207.   uint32_t deltams1 = (deltams * speedfactor1) / 256;
  208.   uint32_t deltams2 = (deltams * speedfactor2) / 256;
  209.   uint32_t deltams21 = (deltams1 + deltams2) / 2;
  210.   sCIStart1 += (deltams1 * beatsin88(1011,10,13));
  211.   sCIStart2 -= (deltams21 * beatsin88(777,8,11));
  212.   sCIStart3 -= (deltams1 * beatsin88(501,5,7));
  213.   sCIStart4 -= (deltams2 * beatsin88(257,4,6));
  214.  
  215.   // Clear out the LED array to a dim background blue-green
  216.   fill_solid( leds, NUM_LEDS, CRGB( 2, 6, 10));
  217.  
  218.   // Render each of four layers, with different scales and speeds, that vary over time
  219.   pacifica_one_layer( pacifica_palette_1, sCIStart1, beatsin16( 3, 11 * 256, 14 * 256), beatsin8( 10, 70, 130), 0-beat16( 301) );
  220.   pacifica_one_layer( pacifica_palette_2, sCIStart2, beatsin16( 4,  6 * 256,  9 * 256), beatsin8( 17, 40,  80), beat16( 401) );
  221.   pacifica_one_layer( pacifica_palette_3, sCIStart3, 6 * 256, beatsin8( 9, 10,38), 0-beat16(503));
  222.   pacifica_one_layer( pacifica_palette_3, sCIStart4, 5 * 256, beatsin8( 8, 10,28), beat16(601));
  223.  
  224.   // Add brighter 'whitecaps' where the waves lines up more
  225.   //pacifica_add_whitecaps();
  226.  
  227.   // Deepen the blues and greens a bit
  228.   pacifica_deepen_colors();
  229. }
  230.  
  231. // Add one layer of waves into the led array
  232. void pacifica_one_layer( CRGBPalette16& p, uint16_t cistart, uint16_t wavescale, uint8_t bri, uint16_t ioff)
  233. {
  234.   uint16_t ci = cistart;
  235.   uint16_t waveangle = ioff;
  236.   uint16_t wavescale_half = (wavescale / 2) + 20;
  237.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  238.     waveangle += 250;
  239.     uint16_t s16 = sin16( waveangle ) + 32768;
  240.     uint16_t cs = scale16( s16 , wavescale_half ) + wavescale_half;
  241.     ci += cs;
  242.     uint16_t sindex16 = sin16( ci) + 32768;
  243.     uint8_t sindex8 = scale16( sindex16, 240);
  244.     CRGB c = ColorFromPalette( p, sindex8, bri, LINEARBLEND);
  245.     leds[i] += c;
  246.   }
  247. }
  248.  
  249. // Add extra 'white' to areas where the four layers of light have lined up brightly
  250. void pacifica_add_whitecaps()
  251. {
  252.   uint8_t basethreshold = beatsin8( 9, 55, 65);
  253.   uint8_t wave = beat8( 7 );
  254.  
  255.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  256.     uint8_t threshold = scale8( sin8( wave), 20) + basethreshold;
  257.     wave += 7;
  258.     uint8_t l = leds[i].getAverageLight();
  259.     if( l > threshold) {
  260.       uint8_t overage = l - threshold;
  261.       uint8_t overage2 = qadd8( overage, overage);
  262.       leds[i] += CRGB( overage, overage2, qadd8( overage2, overage2));
  263.     }
  264.   }
  265. }
  266.  
  267. // Deepen the blues and greens
  268. void pacifica_deepen_colors()
  269. {
  270.   for( uint16_t i = 0; i < NUM_LEDS; i++) {
  271.     leds[i].blue = scale8( leds[i].blue,  145);
  272.     //leds[i].green= scale8( leds[i].green, 200);
  273.     leds[i] |= CRGB( 2, 5, 7);
  274.   }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement