Advertisement
computermuseo

test strip led ws2812b arduino

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