Advertisement
KAren5

Untitled

Nov 13th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. // A basic everyday NeoPixel strip test program.
  2.  
  3. // NEOPIXEL BEST PRACTICES for most reliable operation:
  4. // - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
  5. // - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
  6. // - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
  7. // - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
  8. // connect GROUND (-) first, then +, then data.
  9. // - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
  10. // a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
  11. // (Skipping these may work OK on your workbench but can fail in the field)
  12.  
  13. #include <Adafruit_NeoPixel.h>
  14. #ifdef __AVR__
  15. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  16. #endif
  17.  
  18. // Which pin on the Arduino is connected to the NeoPixels?
  19. // On a Trinket or Gemma we suggest changing this to 1:
  20. #define LED_PIN 6
  21.  
  22. // How many NeoPixels are attached to the Arduino?
  23. #define LED_COUNT 60
  24.  
  25. // Declare our NeoPixel strip object:
  26. Adafruit_NeoPixel strip(LED_COUNT, LED_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.  
  37. // setup() function -- runs once at startup --------------------------------
  38.  
  39. void setup() {
  40. // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  41. // Any other board, you can remove this part (but no harm leaving it):
  42. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  43. clock_prescale_set(clock_div_1);
  44. #endif
  45. // END of Trinket-specific code.
  46.  
  47. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  48. strip.show(); // Turn OFF all pixels ASAP
  49. strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  50. }
  51.  
  52.  
  53.  
  54. // loop() function -- runs repeatedly as long as board is on ---------------
  55.  
  56. void loop() {
  57. // Fill along the length of the strip in various colors...
  58. strip.setPixelColor(0, strip.Color(random(255),random(255),random(255)));
  59. strip.show();
  60. delay(1000);
  61. strip.setPixelColor(1, strip.Color(random(255),20,50));
  62. strip.show();
  63. delay(1000);
  64. strip.setPixelColor(2, strip.Color(35,50,45));
  65. strip.show();
  66. delay(1000);
  67.  
  68. // Do a theater marquee effect in various colors...
  69. theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  70. theaterChase(strip.Color(127, 0, 0), 50); // Red, half brightness
  71. theaterChase(strip.Color( 0, 0, 127), 50); // Blue, half brightness
  72.  
  73. rainbow(10); // Flowing rainbow cycle along the whole strip
  74. theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
  75. }
  76.  
  77.  
  78. // Some functions of our own for creating animated effects -----------------
  79.  
  80. // Fill strip pixels one after another with a color. Strip is NOT cleared
  81. // first; anything there will be covered pixel by pixel. Pass in color
  82. // (as a single 'packed' 32-bit value, which you can get by calling
  83. // strip.Color(red, green, blue) as shown in the loop() function above),
  84. // and a delay time (in milliseconds) between pixels.
  85. void colorWipe(uint32_t color, int wait) {
  86. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  87. strip.setPixelColor(i, color); // Set pixel's color (in RAM)
  88. strip.show(); // Update strip to match
  89. delay(wait); // Pause for a moment
  90. }
  91. }
  92.  
  93. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  94. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  95. // between frames.
  96. void theaterChase(uint32_t color, int wait) {
  97. for(int a=0; a<10; a++) { // Repeat 10 times...
  98. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  99. strip.clear(); // Set all pixels in RAM to 0 (off)
  100. // 'c' counts up from 'b' to end of strip in steps of 3...
  101. for(int c=b; c<strip.numPixels(); c += 3) {
  102. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  103. }
  104. strip.show(); // Update strip with new contents
  105. delay(wait); // Pause for a moment
  106. }
  107. }
  108. }
  109.  
  110. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  111. void rainbow(int wait) {
  112. // Hue of first pixel runs 5 complete loops through the color wheel.
  113. // Color wheel has a range of 65536 but it's OK if we roll over, so
  114. // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  115. // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  116. for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) {
  117. for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  118. // Offset pixel hue by an amount to make one full revolution of the
  119. // color wheel (range of 65536) along the length of the strip
  120. // (strip.numPixels() steps):
  121. int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  122. // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  123. // optionally add saturation and value (brightness) (each 0 to 255).
  124. // Here we're using just the single-argument hue variant. The result
  125. // is passed through strip.gamma32() to provide 'truer' colors
  126. // before assigning to each pixel:
  127. strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  128. }
  129. strip.show(); // Update strip with new contents
  130. delay(wait); // Pause for a moment
  131. }
  132. }
  133.  
  134. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  135. void theaterChaseRainbow(int wait) {
  136. int firstPixelHue = 0; // First pixel starts at red (hue 0)
  137. for(int a=0; a<30; a++) { // Repeat 30 times...
  138. for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
  139. strip.clear(); // Set all pixels in RAM to 0 (off)
  140. // 'c' counts up from 'b' to end of strip in increments of 3...
  141. for(int c=b; c<strip.numPixels(); c += 3) {
  142. // hue of pixel 'c' is offset by an amount to make one full
  143. // revolution of the color wheel (range 65536) along the length
  144. // of the strip (strip.numPixels() steps):
  145. int hue = firstPixelHue + c * 65536L / strip.numPixels();
  146. uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  147. strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  148. }
  149. strip.show(); // Update strip with new contents
  150. delay(wait); // Pause for a moment
  151. firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement