Advertisement
computermuseo

knight rider scanner ws2812b

Mar 23rd, 2017
2,502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. // SETUP YOUR OUTPUT PIN AND NUMBER OF PIXELS
  4. #define PIN 6
  5. #define NUM_PIXELS 30
  6.  
  7. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_PIXELS, PIN, NEO_GRB + NEO_KHZ800);
  8.  
  9. void setup() {
  10. strip.begin();
  11. clearStrip(); // Initialize all pixels to 'off'
  12. delay(1000);
  13. }
  14.  
  15. void loop() {
  16. knightRider(3, 32, 4, 0xFF1000); // Cycles, Speed, Width, RGB Color (original orange-red)
  17. knightRider(3, 32, 3, 0xFF00FF); // Cycles, Speed, Width, RGB Color (purple)
  18. knightRider(3, 32, 2, 0x0000FF); // Cycles, Speed, Width, RGB Color (blue)
  19. knightRider(3, 32, 5, 0xFF0000); // Cycles, Speed, Width, RGB Color (red)
  20. knightRider(3, 32, 6, 0x00FF00); // Cycles, Speed, Width, RGB Color (green)
  21. knightRider(3, 32, 7, 0xFFFF00); // Cycles, Speed, Width, RGB Color (yellow)
  22. knightRider(3, 32, 8, 0x00FFFF); // Cycles, Speed, Width, RGB Color (cyan)
  23. knightRider(3, 32, 2, 0xFFFFFF); // Cycles, Speed, Width, RGB Color (white)
  24. clearStrip();
  25. delay(2000);
  26.  
  27. // Iterate through a whole rainbow of colors
  28. for(byte j=0; j<252; j+=7) {
  29. knightRider(1, 16, 2, colorWheel(j)); // Cycles, Speed, Width, RGB Color
  30. }
  31. clearStrip();
  32. delay(2000);
  33. }
  34.  
  35. // Cycles - one cycle is scanning through all pixels left then right (or right then left)
  36. // Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
  37. // Width - how wide the trail effect is on the fading out LEDs. The original display used
  38. // light bulbs, so they have a persistance when turning off. This creates a trail.
  39. // Effective range is 2 - 8, 4 is default for 16 pixels. Play with this.
  40. // Color - 32-bit packed RGB color value. All pixels will be this color.
  41. // knightRider(cycles, speed, width, color);
  42. void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
  43. uint32_t old_val[NUM_PIXELS]; // up to 256 lights!
  44. // Larson time baby!
  45. for(int i = 0; i < cycles; i++){
  46. for (int count = 1; count<NUM_PIXELS; count++) {
  47. strip.setPixelColor(count, color);
  48. old_val[count] = color;
  49. for(int x = count; x>0; x--) {
  50. old_val[x-1] = dimColor(old_val[x-1], width);
  51. strip.setPixelColor(x-1, old_val[x-1]);
  52. }
  53. strip.show();
  54. delay(speed);
  55. }
  56. for (int count = NUM_PIXELS-1; count>=0; count--) {
  57. strip.setPixelColor(count, color);
  58. old_val[count] = color;
  59. for(int x = count; x<=NUM_PIXELS ;x++) {
  60. old_val[x-1] = dimColor(old_val[x-1], width);
  61. strip.setPixelColor(x+1, old_val[x+1]);
  62. }
  63. strip.show();
  64. delay(speed);
  65. }
  66. }
  67. }
  68.  
  69. void clearStrip() {
  70. for( int i = 0; i<NUM_PIXELS; i++){
  71. strip.setPixelColor(i, 0x000000); strip.show();
  72. }
  73. }
  74.  
  75. uint32_t dimColor(uint32_t color, uint8_t width) {
  76. return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
  77. }
  78.  
  79. // Using a counter and for() loop, input a value 0 to 251 to get a color value.
  80. // The colors transition like: red - org - ylw - grn - cyn - blue - vio - mag - back to red.
  81. // Entering 255 will give you white, if you need it.
  82. uint32_t colorWheel(byte WheelPos) {
  83. byte state = WheelPos / 21;
  84. switch(state) {
  85. case 0: return strip.Color(255, 0, 255 - ((((WheelPos % 21) + 1) * 6) + 127)); break;
  86. case 1: return strip.Color(255, ((WheelPos % 21) + 1) * 6, 0); break;
  87. case 2: return strip.Color(255, (((WheelPos % 21) + 1) * 6) + 127, 0); break;
  88. case 3: return strip.Color(255 - (((WheelPos % 21) + 1) * 6), 255, 0); break;
  89. case 4: return strip.Color(255 - (((WheelPos % 21) + 1) * 6) + 127, 255, 0); break;
  90. case 5: return strip.Color(0, 255, ((WheelPos % 21) + 1) * 6); break;
  91. case 6: return strip.Color(0, 255, (((WheelPos % 21) + 1) * 6) + 127); break;
  92. case 7: return strip.Color(0, 255 - (((WheelPos % 21) + 1) * 6), 255); break;
  93. case 8: return strip.Color(0, 255 - ((((WheelPos % 21) + 1) * 6) + 127), 255); break;
  94. case 9: return strip.Color(((WheelPos % 21) + 1) * 6, 0, 255); break;
  95. case 10: return strip.Color((((WheelPos % 21) + 1) * 6) + 127, 0, 255); break;
  96. case 11: return strip.Color(255, 0, 255 - (((WheelPos % 21) + 1) * 6)); break;
  97. default: return strip.Color(0, 0, 0); break;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement