Guest User

Untitled

a guest
Jul 9th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. // 888b 888 8888888b. d8b 888
  2. // 8888b 888 888 Y88b Y8P 888
  3. // 88888b 888 888 888 888
  4. // 888Y88b 888 .d88b. .d88b. 888 d88P 888 888 888 .d88b. 888
  5. // 888 Y88b888 d8P Y8b d88""88b 8888888P" 888 `Y8bd8P' d8P Y8b 888
  6. // 888 Y88888 88888888 888 888 888 888 X88K 88888888 888
  7. // 888 Y8888 Y8b. Y88..88P 888 888 .d8""8b. Y8b. 888
  8. // 888 Y888 "Y8888 "Y88P" 888 888 888 888 "Y8888 888
  9. //
  10. // NeoPixel-KnightRider
  11. //
  12. // A highly configurable Knight Rider (larson display) routine for your NeoPixels
  13. // (WS2812 RGB LED)
  14. //-----------------------------------------------------------------------------
  15. // The MIT License (MIT)
  16. //
  17. // Copyright (c) 2013 Technobly - [email protected] - August 13th 2013
  18. //
  19. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  20. // this software and associated documentation files (the "Software"), to deal in
  21. // the Software without restriction, including without limitation the rights to
  22. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  23. // the Software, and to permit persons to whom the Software is furnished to do so,
  24. // subject to the following conditions:
  25. //
  26. // The above copyright notice and this permission notice shall be included in all
  27. // copies or substantial portions of the Software.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  31. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  32. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  33. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. //
  36. //-----------------------------------------------------------------------------
  37. // ASCII GEN http://patorjk.com/software/taag/#p=display&f=Colossal&t=NeoPixel
  38. //
  39. //-----------------------------------------------------------------------------
  40.  
  41. #include <Adafruit_NeoPixel.h>
  42.  
  43. // SETUP YOUR OUTPUT PIN AND NUMBER OF PIXELS
  44. #define PIN A5
  45. #define NUM_PIXELS 6
  46. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
  47.  
  48. void setup() {
  49. strip.begin();
  50. clearStrip(); // Initialize all pixels to 'off'
  51. delay(1000);
  52. }
  53.  
  54. void loop() {
  55. knightRider(1, 100, 6, 0x0000FF); // Cycles, Speed, Width, RGB Color (original orange-red)
  56. knightRider(1, 100, 6, 0xFF0000); // Cycles, Speed, Width, RGB Color (purple)
  57. //knightRider(3, 32, 2, 0xFFFFFF); // Cycles, Speed, Width, RGB Color (blue)
  58. //knightRider(3, 32, 5, 0xFF0000); // Cycles, Speed, Width, RGB Color (red)
  59. //knightRider(3, 32, 6, 0x00FF00); // Cycles, Speed, Width, RGB Color (green)
  60. //knightRider(3, 32, 7, 0xFFFF00); // Cycles, Speed, Width, RGB Color (yellow)
  61. //knightRider(3, 32, 8, 0x00FFFF); // Cycles, Speed, Width, RGB Color (cyan)
  62. //knightRider(3, 32, 2, 0xFFFFFF); // Cycles, Speed, Width, RGB Color (white)
  63. clearStrip();
  64. delay(20);
  65.  
  66.  
  67. }
  68.  
  69. // Cycles - one cycle is scanning through all pixels left then right (or right then left)
  70. // Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
  71. // Width - how wide the trail effect is on the fading out LEDs. The original display used
  72. // light bulbs, so they have a persistance when turning off. This creates a trail.
  73. // Effective range is 2 - 8, 4 is default for 16 pixels. Play with this.
  74. // Color - 32-bit packed RGB color value. All pixels will be this color.
  75. // knightRider(cycles, speed, width, color);
  76. void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
  77. uint32_t old_val[NUM_PIXELS]; // up to 256 lights!
  78. // Larson time baby!
  79. for(int i = 0; i < cycles; i++){
  80. for (int count = 1; count<NUM_PIXELS; count++) {
  81. strip.setPixelColor(count, color);
  82. old_val[count] = color;
  83. for(int x = count; x>0; x--) {
  84. old_val[x-1] = dimColor(old_val[x-1], width);
  85. strip.setPixelColor(x-1, old_val[x-1]);
  86. }
  87. strip.show();
  88. delay(speed);
  89. }
  90. for (int count = NUM_PIXELS-1; count>=0; count--) {
  91. strip.setPixelColor(count, color);
  92. old_val[count] = color;
  93. for(int x = count; x<=NUM_PIXELS ;x++) {
  94. old_val[x-1] = dimColor(old_val[x-1], width);
  95. strip.setPixelColor(x+1, old_val[x+1]);
  96. }
  97. strip.show();
  98. delay(speed);
  99. }
  100. }
  101. }
  102.  
  103. void clearStrip() {
  104. for( int i = 0; i<NUM_PIXELS; i++){
  105. strip.setPixelColor(i, 0x000000); strip.show();
  106. }
  107. }
  108.  
  109. uint32_t dimColor(uint32_t color, uint8_t width) {
  110. return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
  111. }
  112.  
  113. // Using a counter and for() loop, input a value 0 to 251 to get a color value.
  114. // The colors transition like: red - org - ylw - grn - cyn - blue - vio - mag - back to red.
  115. // Entering 255 will give you white, if you need it.
  116. uint32_t colorWheel(byte WheelPos) {
  117. byte state = WheelPos / 21;
  118. switch(state) {
  119. case 0: return strip.Color(255, 0, 255 - ((((WheelPos % 21) + 1) * 6) + 127)); break;
  120. case 1: return strip.Color(255, ((WheelPos % 21) + 1) * 6, 0); break;
  121. case 2: return strip.Color(255, (((WheelPos % 21) + 1) * 6) + 127, 0); break;
  122. case 3: return strip.Color(255 - (((WheelPos % 21) + 1) * 6), 255, 0); break;
  123. case 4: return strip.Color(255 - (((WheelPos % 21) + 1) * 6) + 127, 255, 0); break;
  124. case 5: return strip.Color(0, 255, ((WheelPos % 21) + 1) * 6); break;
  125. case 6: return strip.Color(0, 255, (((WheelPos % 21) + 1) * 6) + 127); break;
  126. case 7: return strip.Color(0, 255 - (((WheelPos % 21) + 1) * 6), 255); break;
  127. case 8: return strip.Color(0, 255 - ((((WheelPos % 21) + 1) * 6) + 127), 255); break;
  128. case 9: return strip.Color(((WheelPos % 21) + 1) * 6, 0, 255); break;
  129. case 10: return strip.Color((((WheelPos % 21) + 1) * 6) + 127, 0, 255); break;
  130. case 11: return strip.Color(255, 0, 255 - (((WheelPos % 21) + 1) * 6)); break;
  131. default: return strip.Color(0, 0, 0); break;
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment