Advertisement
Guest User

strandtest

a guest
Feb 27th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <avr/power.h>
  3.  
  4. #define PIN 3
  5.  
  6. // Parameter 1 = number of pixels in strip
  7. // Parameter 2 = Arduino pin number (most are valid)
  8. // Parameter 3 = pixel type flags, add together as needed:
  9. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  10. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  11. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  12. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  13. Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, PIN, NEO_GRB + NEO_KHZ800);
  14.  
  15. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  16. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  17. // and minimize distance between Arduino and first pixel. Avoid connecting
  18. // on a live circuit...if you must, connect GND first.
  19.  
  20. void setup() {
  21. // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  22.  
  23. // End of trinket special code
  24.  
  25.  
  26. strip.begin();
  27. strip.show(); // Initialize all pixels to 'off'
  28. }
  29.  
  30. void loop() {
  31. // Some example procedures showing how to display to the pixels:
  32. colorWipe(strip.Color(255, 0, 0), 50); // Red
  33. colorWipe(strip.Color(0, 255, 0), 50); // Green
  34. colorWipe(strip.Color(0, 0, 255), 50); // Blue
  35. // Send a theater pixel chase in...
  36. theaterChase(strip.Color(127, 127, 127), 50); // White
  37. theaterChase(strip.Color(127, 0, 0), 50); // Red
  38. theaterChase(strip.Color( 0, 0, 127), 50); // Blue
  39.  
  40. rainbow(20);
  41. rainbowCycle(20);
  42. theaterChaseRainbow(50);
  43. }
  44.  
  45. // Fill the dots one after the other with a color
  46. void colorWipe(uint32_t c, uint8_t wait) {
  47. for(uint16_t i=0; i<strip.numPixels(); i++) {
  48. strip.setPixelColor(i, c);
  49. strip.show();
  50. delay(wait);
  51. }
  52. }
  53.  
  54. void rainbow(uint8_t wait) {
  55. uint16_t i, j;
  56.  
  57. for(j=0; j<256; j++) {
  58. for(i=0; i<strip.numPixels(); i++) {
  59. strip.setPixelColor(i, Wheel((i+j) & 255));
  60. }
  61. strip.show();
  62. delay(wait);
  63. }
  64. }
  65.  
  66. // Slightly different, this makes the rainbow equally distributed throughout
  67. void rainbowCycle(uint8_t wait) {
  68. uint16_t i, j;
  69.  
  70. for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  71. for(i=0; i< strip.numPixels(); i++) {
  72. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  73. }
  74. strip.show();
  75. delay(wait);
  76. }
  77. }
  78.  
  79. //Theatre-style crawling lights.
  80. void theaterChase(uint32_t c, uint8_t wait) {
  81. for (int j=0; j<10; j++) { //do 10 cycles of chasing
  82. for (int q=0; q < 3; q++) {
  83. for (int i=0; i < strip.numPixels(); i=i+3) {
  84. strip.setPixelColor(i+q, c); //turn every third pixel on
  85. }
  86. strip.show();
  87.  
  88. delay(wait);
  89.  
  90. for (int i=0; i < strip.numPixels(); i=i+3) {
  91. strip.setPixelColor(i+q, 0); //turn every third pixel off
  92. }
  93. }
  94. }
  95. }
  96.  
  97. //Theatre-style crawling lights with rainbow effect
  98. void theaterChaseRainbow(uint8_t wait) {
  99. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  100. for (int q=0; q < 3; q++) {
  101. for (int i=0; i < strip.numPixels(); i=i+3) {
  102. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  103. }
  104. strip.show();
  105.  
  106. delay(wait);
  107.  
  108. for (int i=0; i < strip.numPixels(); i=i+3) {
  109. strip.setPixelColor(i+q, 0); //turn every third pixel off
  110. }
  111. }
  112. }
  113. }
  114.  
  115. // Input a value 0 to 255 to get a color value.
  116. // The colours are a transition r - g - b - back to r.
  117. uint32_t Wheel(byte WheelPos) {
  118. WheelPos = 255 - WheelPos;
  119. if(WheelPos < 85) {
  120. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  121. } else if(WheelPos < 170) {
  122. WheelPos -= 85;
  123. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  124. } else {
  125. WheelPos -= 170;
  126. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement