1. #include <Adafruit_NeoPixel.h>
    2. #define PIN 2
    3. #define NUMPIXELS 24
    4. #define BRIGHTNESS 50
    5. #define DELAYVAL 500
    6. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
    7. int MODE = 0; // 模式
    8. void setup() {
    9. strip.setBrightness(BRIGHTNESS);
    10. // 初始化Adafruit_NeoPixel库
    11. strip.begin();
    12. // 初始化时关闭所有LED
    13. strip.show();
    14. }
    15. void loop() {
    16. switch(MODE){
    17. case 0: RunningWater(); break;
    18. case 1: colorWipe(strip.Color(150, 150, 150), 50); break;// BlueWite
    19. case 2: rainbowCycle(1);
    20. case 3: rainbow(50);
    21. case 4: rainbowCycle(1);
    22. }
    23. MODE++;
    24. if(MODE == 4){
    25. MODE = 0;
    26. }
    27. }
    28. /**
    29. * 模式: 颜色擦除
    30. */
    31. void colorWipe(uint32_t c, uint8_t wait) {
    32. strip.clear();
    33. for (uint16_t i = 0; i < strip.numPixels(); i++) {
    34. strip.setPixelColor(i, c);
    35. strip.show();
    36. delay(wait);
    37. }
    38. }
    39. /**
    40. * 彩虹呼吸
    41. */
    42. void rainbow(uint8_t wait) {
    43. uint16_t i, j;
    44. for (j = 0; j < 256; j++) {
    45. for (i = 0; i < strip.numPixels(); i++) {
    46. strip.setPixelColor(i, Wheel((i + j) & 255 ));
    47. }
    48. strip.show();
    49. delay(wait);
    50. }
    51. }
    52. /**
    53. * 彩虹呼吸-旋转
    54. */
    55. void rainbowCycle(uint8_t wait) {
    56. uint16_t i, j;
    57. for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
    58. for (i = 0; i < strip.numPixels(); i++) {
    59. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    60. }
    61. strip.show();
    62. delay(wait);
    63. }
    64. }
    65. /**
    66. * 走马灯
    67. */
    68. void theaterChase(uint32_t c, uint8_t wait) {
    69. for (int j=0; j<10; j++) { //do 10 cycles of chasing
    70. for (int q=0; q < 3; q++) {
    71. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
    72. strip.setPixelColor(i+q, c); //turn every third pixel on
    73. }
    74. strip.show();
    75. delay(wait);
    76. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
    77. strip.setPixelColor(i+q, 0); //turn every third pixel off
    78. }
    79. }
    80. }
    81. }
    82. /**
    83. * 走马灯-彩虹
    84. */
    85. void theaterChaseRainbow(uint8_t wait) {
    86. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
    87. for (int q=0; q < 3; q++) {
    88. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
    89. strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
    90. }
    91. strip.show();
    92. delay(wait);
    93. for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
    94. strip.setPixelColor(i+q, 0); //turn every third pixel off
    95. }
    96. }
    97. }
    98. }
    99. uint32_t Wheel(byte WheelPos) {
    100. if (WheelPos < 85) {
    101. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
    102. } else if (WheelPos < 170) {
    103. WheelPos -= 85;
    104. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
    105. } else {
    106. WheelPos -= 170;
    107. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
    108. }
    109. }