Advertisement
UnaClocker

Christmas Lights Nov 29

Nov 29th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.09 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3.   #include <avr/power.h>
  4. #endif
  5.  
  6. #define PIN 16
  7.  
  8. // Parameter 1 = number of pixels in strip
  9. // Parameter 2 = Arduino pin number (most are valid)
  10. // Parameter 3 = pixel type flags, add together as needed:
  11. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  12. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  13. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  14. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  15. Adafruit_NeoPixel strip = Adafruit_NeoPixel(300, PIN, NEO_GRB + NEO_KHZ800);
  16. long randNumber;
  17.  
  18. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  19. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  20. // and minimize distance between Arduino and first pixel.  Avoid connecting
  21. // on a live circuit...if you must, connect GND first.
  22.  
  23. void setup() {
  24.  
  25.   randomSeed(analogRead(2));
  26.   strip.begin();
  27.   strip.show(); // Initialize all pixels to 'off'
  28. }
  29.  
  30. void loop() {
  31.   christmasLights(); // I wrote this pattern.
  32.   colorWipe(strip.Color(255, 0, 0), 2); // Red
  33.   unWipe(2); // I wrote this unwipe routine.
  34.   colorWipe(strip.Color(0, 255, 0), 2); // Green
  35.   unWipe(2);
  36.   colorWipe(strip.Color(0, 0, 255), 2); // Blue
  37.   unWipe(2);
  38.   // I wrote this routine below, it's kind of ugly, but functional.
  39.   colorBricks(strip.Color(random(255), random(255), random(255)),strip.Color(random(255), random(255), random(255)));
  40.   colorBricks(strip.Color(random(255), random(255), random(255)),strip.Color(random(255), random(255), random(255)));
  41.   colorBricks(strip.Color(random(255), random(255), random(255)),strip.Color(random(255), random(255), random(255)));
  42.   colorBricks(strip.Color(random(255), random(255), random(255)),strip.Color(random(255), random(255), random(255)));
  43.   theaterChase(strip.Color(127, 127, 127), 75); // White
  44.   theaterChase(strip.Color(127, 0, 0), 75); // Red
  45.   theaterChase(strip.Color(0, 127, 0), 75); // Blue
  46.  
  47.   rainbow(15);
  48.   rainbowCycle(5);
  49.   theaterChaseRainbow(60);
  50. }
  51.  
  52. void christmasLights() {
  53. for (uint16_t loopy=0; loopy<26; loopy++) {
  54.   for (uint16_t i=0; i<strip.numPixels(); i+=5) {
  55.     strip.setPixelColor(i,strip.Color(255,0,0)); // Red
  56.   }
  57.   for (uint16_t i=1; i<strip.numPixels(); i+=5) {
  58.     strip.setPixelColor(i,strip.Color(0,255,0)); // Green
  59.   }
  60.   for (uint16_t i=2; i<strip.numPixels(); i+=5) {
  61.     strip.setPixelColor(i,strip.Color(0,0,255)); // Blue
  62.   }
  63.   for (uint16_t i=3; i<strip.numPixels(); i+=5) {
  64.     strip.setPixelColor(i,strip.Color(200,100,0)); // Orange
  65.   }
  66.   for (uint16_t i=4; i<strip.numPixels(); i+=5) {
  67.     strip.setPixelColor(i,strip.Color(128,0,128)); // Red
  68.   }
  69.   strip.show();
  70.   delay(500);
  71.   for (uint16_t i=0; i<strip.numPixels(); i++) {
  72.     strip.setPixelColor(i,strip.Color(0,0,0)); // Blank
  73.   }
  74.   strip.show();
  75.   delay(500);
  76. }
  77. }
  78.  
  79.  
  80. // Fill the dots one after the other with a color
  81. void colorWipe(uint32_t c, uint8_t wait) {
  82.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  83.     strip.setPixelColor(i, c);
  84.     strip.show();
  85.     delay(wait);
  86.   }
  87. }
  88.  
  89. void colorBricks(uint32_t c1, uint32_t c2) {
  90.   uint16_t backwards=strip.numPixels();
  91.   for(uint16_t forwards=0; forwards<strip.numPixels(); forwards++) {
  92.     if (backwards<299) strip.setPixelColor(backwards+1, 0);
  93.     if (forwards>0) strip.setPixelColor(forwards-1, 0);
  94.     strip.setPixelColor(forwards, c1);
  95.     strip.setPixelColor(backwards, c2);
  96.     strip.setPixelColor(forwards+1, c1);
  97.     strip.setPixelColor(backwards-1, c2);
  98.     strip.setPixelColor(forwards+2, c1);
  99.     strip.setPixelColor(backwards-2, c2);
  100.     strip.setPixelColor(forwards+3, c1);
  101.     strip.setPixelColor(backwards-3, c2);
  102.     strip.setPixelColor(backwards-4, c2);
  103.     strip.setPixelColor(forwards+4, c1);
  104.     strip.show();
  105.     backwards--;
  106.     delay(2);
  107.   }
  108.   strip.setPixelColor(299,0);
  109. }
  110.  
  111. void unWipe(uint8_t wait) {
  112.   for(uint16_t i=strip.numPixels(); i>0; i--) {
  113.     strip.setPixelColor(i, 0);
  114.     strip.show();
  115.     delay(wait);
  116.   }
  117. }
  118. void rainbow(uint8_t wait) {
  119.   uint16_t i, j;
  120.  
  121.   for(j=0; j<256; j++) {
  122.     for(i=0; i<strip.numPixels(); i++) {
  123.       strip.setPixelColor(i, Wheel((i+j) & 255));
  124.     }
  125.     strip.show();
  126.     delay(wait);
  127.   }
  128. }
  129.  
  130. // Slightly different, this makes the rainbow equally distributed throughout
  131. void rainbowCycle(uint8_t wait) {
  132.   uint16_t i, j;
  133.  
  134.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  135.     for(i=0; i< strip.numPixels(); i++) {
  136.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  137.     }
  138.     strip.show();
  139.     delay(wait);
  140.   }
  141. }
  142.  
  143. //Theatre-style crawling lights.
  144. void theaterChase(uint32_t c, uint8_t wait) {
  145.   for (int j=0; j<30; j++) {  //do 10 cycles of chasing
  146.     for (int q=0; q < 5; q++) {
  147.       for (int i=0; i < strip.numPixels(); i=i+5) {
  148.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  149.       }
  150.       strip.show();
  151.  
  152.       delay(wait);
  153.  
  154.       for (int i=0; i < strip.numPixels(); i=i+5) {
  155.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  156.       }
  157.     }
  158.   }
  159. }
  160.  
  161. //Theatre-style crawling lights with rainbow effect
  162. void theaterChaseRainbow(uint8_t wait) {
  163.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  164.     for (int q=0; q < 5; q++) {
  165.       for (int i=0; i < strip.numPixels(); i=i+5) {
  166.         strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  167.       }
  168.       strip.show();
  169.  
  170.       delay(wait);
  171.  
  172.       for (int i=0; i < strip.numPixels(); i=i+5) {
  173.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  174.       }
  175.     }
  176.   }
  177. }
  178.  
  179. // Input a value 0 to 255 to get a color value.
  180. // The colours are a transition r - g - b - back to r.
  181. uint32_t Wheel(byte WheelPos) {
  182.   WheelPos = 255 - WheelPos;
  183.   if(WheelPos < 85) {
  184.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  185.   }
  186.   if(WheelPos < 170) {
  187.     WheelPos -= 85;
  188.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  189.   }
  190.   WheelPos -= 170;
  191.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement