Advertisement
olelek

Okulary karnawałowe

Jan 10th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.16 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3.   #include <avr/power.h>
  4. #endif
  5.  
  6. #define PIN 2
  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(24, PIN, NEO_GRB + NEO_KHZ800);
  16.  
  17. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  18. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  19. // and minimize distance between Arduino and first pixel.  Avoid connecting
  20. // on a live circuit...if you must, connect GND first.
  21.  
  22. void setup() {
  23.   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  24.   #if defined (__AVR_ATtiny85__)
  25.     if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  26.   #endif
  27.   // End of trinket special code
  28.  
  29.  
  30.   strip.begin();
  31.   strip.show(); // Initialize all pixels to 'off'
  32. }
  33.  
  34. void loop() {
  35.   // Some example procedures showing how to display to the pixels:
  36.   fadeIn(3);
  37.   fadeOut(3);
  38.   fadeIn(3);
  39.   fadeOut(3);
  40.   fadeIn(3);
  41.   fadeOut(3);
  42.  
  43.   fadeIn(1);
  44.   fadeOut(1);
  45.   fadeIn(1);
  46.   fadeOut(1);
  47.   fadeIn(1);
  48.   fadeOut(1);
  49.   fadeIn(1);
  50.   fadeOut(1);
  51.  
  52.   eight(strip.Color(127,127,0),50);
  53.   eight(strip.Color(0,127,0),50);
  54.   eight(strip.Color(0,127,127),50);
  55.   eight2(strip.Color(127,127,0),50);
  56.  
  57.  
  58.   strobo(strip.Color(255, 255, 255), 200, 40);
  59.   strobo2(strip.Color(127, 127, 127), 100, 20);
  60.  
  61.   colorWipe(strip.Color(255, 0, 0), 100); // Red
  62.   colorWipe(strip.Color(0, 255, 0), 100); // Green
  63.   colorWipe(strip.Color(0, 0, 255), 100); // Blue
  64.   // Send a theater pixel chase in...
  65.   theaterChase2(strip.Color(127, 127, 127), 100, 6, 20); // White
  66.   theaterChase2(strip.Color(127, 0, 0), 50, 4, 20); // Red
  67.   theaterChase2(strip.Color(0, 0, 127), 50, 3, 20); // Blue
  68.  
  69.   //rainbow(20);
  70.   //rainbowCycle(20);
  71.   theaterChaseRainbow(50);
  72. }
  73.  
  74. // Fill the dots one after the other with a color
  75. void colorWipe(uint32_t c, uint8_t wait) {
  76.   for(uint16_t i=0; i<strip.numPixels()/2; i++) {
  77.     strip.setPixelColor(i, c);
  78.     strip.setPixelColor(i+strip.numPixels()/2, c);
  79.     strip.show();
  80.     delay(wait);
  81.   }
  82. }
  83.  
  84. void fadeIn(uint8_t wait)
  85. {
  86.   for(uint8_t i=0; i<127; ++i)
  87.   {
  88.     for(uint8_t j=0;j<strip.numPixels();++j)
  89.       strip.setPixelColor(j, strip.Color(i,i,i));
  90.     strip.show();
  91.     delay(wait);
  92.   }
  93. }
  94. void fadeOut(uint8_t wait)
  95. {
  96.   for(uint8_t i=127; i>0x0; --i)
  97.   {
  98.     for(uint8_t j=0;j<strip.numPixels();++j)
  99.       strip.setPixelColor(j, strip.Color(i,i,i));
  100.     strip.show();
  101.     delay(wait);
  102.   }
  103. }
  104.  
  105.  
  106. void eight(uint32_t c, uint8_t wait)
  107. {
  108.   const uint8_t sequence[] = {1,2,3,4,5,6,13,24,23,22,21,20,19,18,17,16,15,14,7,8,9,10,11,12};
  109.   for(uint8_t i=0;i<strip.numPixels();++i){strip.setPixelColor(i, 0);}
  110.   for(uint8_t i=0; i<sizeof(sequence);++i)
  111.   {
  112.     strip.setPixelColor(sequence[i]-1, c);
  113.     strip.show();
  114.     delay(wait);
  115.     strip.setPixelColor(sequence[i]-1, 0);
  116.   }
  117. }
  118.  
  119. void eight2(uint32_t c, uint8_t wait)
  120. {
  121.   const uint8_t sequence[] = {1,2,3,4,5,6,13,24,23,22,21,20,19,18,17,16,15,14,7,8,9,10,11,12};
  122.   for(uint8_t i=0;i<strip.numPixels();++i){strip.setPixelColor(i, 0);}
  123.   for(uint8_t i=0; i<sizeof(sequence);++i)
  124.   {
  125.     strip.setPixelColor(sequence[i]-1, c);
  126.     strip.show();
  127.     delay(wait);
  128.   }
  129. }
  130.  
  131. void strobo(uint32_t c, uint8_t wait, uint8_t cycles) {
  132.   for(uint8_t j=0; j<cycles; ++j)
  133.   {
  134.     for(uint16_t i=0; i<strip.numPixels(); i++) {
  135.       strip.setPixelColor(i, c);
  136.     }
  137.     strip.show();
  138.     delay(wait/10);
  139.     for(uint16_t i=0; i<strip.numPixels(); i++) {
  140.       strip.setPixelColor(i, 0);
  141.     }
  142.     strip.show();
  143.     delay(wait);
  144.   }
  145. }
  146.  
  147. void strobo2(uint32_t c, uint8_t wait, uint8_t cycles) {
  148.   for(uint8_t j=0; j<cycles; ++j)
  149.   {
  150.     for(uint16_t i=0; i<strip.numPixels()/2; i++) {
  151.       strip.setPixelColor(i, c);
  152.     }
  153.     for(uint16_t i=0; i<strip.numPixels()/2; i++) {
  154.       strip.setPixelColor(i+strip.numPixels()/2, 0);
  155.     }
  156.     strip.show();
  157.     delay(wait);
  158.     for(uint16_t i=0; i<strip.numPixels()/2; i++) {
  159.       strip.setPixelColor(i, 0);
  160.     }
  161.     for(uint16_t i=0; i<strip.numPixels()/2; i++) {
  162.       strip.setPixelColor(i+strip.numPixels()/2, c);
  163.     }
  164.     strip.show();
  165.     delay(wait);
  166.   }
  167. }
  168.  
  169. void rainbow(uint8_t wait) {
  170.   uint16_t i, j;
  171.  
  172.   for(j=0; j<256; j++) {
  173.     for(i=0; i<strip.numPixels(); i++) {
  174.       strip.setPixelColor(i, Wheel((i+j) & 255));
  175.     }
  176.     strip.show();
  177.     delay(wait);
  178.   }
  179. }
  180.  
  181. // Slightly different, this makes the rainbow equally distributed throughout
  182. void rainbowCycle(uint8_t wait) {
  183.   uint16_t i, j;
  184.  
  185.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  186.     for(i=0; i< strip.numPixels(); i++) {
  187.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  188.     }
  189.     strip.show();
  190.     delay(wait);
  191.   }
  192. }
  193.  
  194. //Theatre-style crawling lights.
  195. void theaterChase(uint32_t c, uint8_t wait) {
  196.   for (int j=0; j<10; j++) {  //do 10 cycles of chasing
  197.     for (int q=0; q < 3; q++) {
  198.       for (int i=0; i < strip.numPixels(); i=i+3) {
  199.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  200.       }
  201.       strip.show();
  202.  
  203.       delay(wait);
  204.  
  205.       for (int i=0; i < strip.numPixels(); i=i+3) {
  206.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  207.       }
  208.     }
  209.   }
  210. }
  211. //Theatre-style crawling lights.
  212. void theaterChase2(uint32_t c, uint8_t wait, uint8_t every, uint8_t cycles) {
  213.   for (int j=0; j<cycles; j++) {  //do 10 cycles of chasing
  214.     for (int q=0; q < every; q++) {
  215.       for (int i=0; i < strip.numPixels(); i=i+every) {
  216.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  217.       }
  218.       strip.show();
  219.  
  220.       delay(wait);
  221.  
  222.       for (int i=0; i < strip.numPixels(); i=i+every) {
  223.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  224.       }
  225.     }
  226.   }
  227. }
  228. //Theatre-style crawling lights with rainbow effect
  229. void theaterChaseRainbow(uint8_t wait) {
  230.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  231.     for (int q=0; q < 3; q++) {
  232.       for (int i=0; i < strip.numPixels(); i=i+3) {
  233.         strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  234.       }
  235.       strip.show();
  236.  
  237.       delay(wait);
  238.  
  239.       for (int i=0; i < strip.numPixels(); i=i+3) {
  240.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  241.       }
  242.     }
  243.   }
  244. }
  245.  
  246. // Input a value 0 to 255 to get a color value.
  247. // The colours are a transition r - g - b - back to r.
  248. uint32_t Wheel(byte WheelPos) {
  249.   WheelPos = 255 - WheelPos;
  250.   if(WheelPos < 85) {
  251.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  252.   }
  253.   if(WheelPos < 170) {
  254.     WheelPos -= 85;
  255.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  256.   }
  257.   WheelPos -= 170;
  258.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement