楼主: eagler8

[教程] 【Arduino】108种传感器模块系列实验(61)---WS2812直条8位模块

[复制链接]

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2020-1-1 08:17 | 显示全部楼层
嘻嘻嘻嘻嘻 发表于 2019-12-31 21:44
每块板也就3个ws2812led灯。实现独立控制6路六块灯板的话。代码怎么搞?

最好能介绍下无人机航灯的具体应用场景,比如18颗LED的颜色分配,闪烁灯还是流水灯,如何实现航向灯的触发?等等

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2020-1-2 11:35 | 显示全部楼层
嘻嘻嘻嘻嘻 发表于 2020-1-2 09:33
不好意思,级数太低加不上好友。我做的也是应用于无人机上面的,航灯读取飞控mavlink数据触发。有空研究 ...

有相关的电原理图吗?

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-15 20:39 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  实验程序之八,NeoPixel Ring 绿色柱灯

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   实验程序之八,NeoPixel Ring 绿色柱灯
  5. */

  6. #include <Adafruit_NeoPixel.h>
  7. #ifdef __AVR__
  8. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  9. #endif

  10. // Which pin on the Arduino is connected to the NeoPixels?
  11. #define PIN        6 // On Trinket or Gemma, suggest changing this to 1

  12. // How many NeoPixels are attached to the Arduino?
  13. #define NUMPIXELS 8 // Popular NeoPixel ring size

  14. // When setting up the NeoPixel library, we tell it how many pixels,
  15. // and which pin to use to send signals. Note that for older NeoPixel
  16. // strips you might need to change the third parameter -- see the
  17. // strandtest example for more information on possible values.
  18. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

  19. #define DELAYVAL 500 // Time (in milliseconds) to pause between pixels

  20. void setup() {
  21.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  22.   // Any other board, you can remove this part (but no harm leaving it):
  23. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  24.   clock_prescale_set(clock_div_1);
  25. #endif
  26.   // END of Trinket-specific code.
  27.   pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  28. }

  29. void loop() {
  30.   pixels.clear(); // Set all pixel colors to 'off'

  31.   // The first NeoPixel in a strand is #0, second is 1, all the way up
  32.   // to the count of pixels minus one.
  33.   for (int i = 0; i < NUMPIXELS; i++) { // For each pixel...

  34.     // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
  35.     // Here we're using a moderately bright green color:
  36.     pixels.setPixelColor(i, pixels.Color(0, 150, 0));

  37.     pixels.show();   // Send the updated pixel colors to the hardware.

  38.     delay(DELAYVAL); // Pause before next pass through loop
  39.   }
  40. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-15 20:45 | 显示全部楼层
实验场景图

14.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 08:36 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目之九:一个基本的日常 NeoPixel 灯条测试程序

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目之九:一个基本的日常 NeoPixel 灯条测试程序
  5. */

  6. #include <Adafruit_NeoPixel.h>
  7. #ifdef __AVR__
  8. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  9. #endif

  10. // Which pin on the Arduino is connected to the NeoPixels?
  11. // On a Trinket or Gemma we suggest changing this to 1:
  12. #define LED_PIN    6

  13. // How many NeoPixels are attached to the Arduino?
  14. #define LED_COUNT 8

  15. // Declare our NeoPixel strip object:
  16. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  17. // Argument 1 = Number of pixels in NeoPixel strip
  18. // Argument 2 = Arduino pin number (most are valid)
  19. // Argument 3 = Pixel type flags, add together as needed:
  20. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  21. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  22. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  23. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  24. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)


  25. // setup() function -- runs once at startup --------------------------------

  26. void setup() {
  27.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  28.   // Any other board, you can remove this part (but no harm leaving it):
  29. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  30.   clock_prescale_set(clock_div_1);
  31. #endif
  32.   // END of Trinket-specific code.

  33.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  34.   strip.show();            // Turn OFF all pixels ASAP
  35.   strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  36. }


  37. // loop() function -- runs repeatedly as long as board is on ---------------

  38. void loop() {
  39.   // Fill along the length of the strip in various colors...
  40.   colorWipe(strip.Color(255,   0,   0), 50); // Red
  41.   colorWipe(strip.Color(  0, 255,   0), 50); // Green
  42.   colorWipe(strip.Color(  0,   0, 255), 50); // Blue

  43.   // Do a theater marquee effect in various colors...
  44.   theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  45.   theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  46.   theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness

  47.   rainbow(10);             // Flowing rainbow cycle along the whole strip
  48.   theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
  49. }


  50. // Some functions of our own for creating animated effects -----------------

  51. // Fill strip pixels one after another with a color. Strip is NOT cleared
  52. // first; anything there will be covered pixel by pixel. Pass in color
  53. // (as a single 'packed' 32-bit value, which you can get by calling
  54. // strip.Color(red, green, blue) as shown in the loop() function above),
  55. // and a delay time (in milliseconds) between pixels.
  56. void colorWipe(uint32_t color, int wait) {
  57.   for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  58.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  59.     strip.show();                          //  Update strip to match
  60.     delay(wait);                           //  Pause for a moment
  61.   }
  62. }

  63. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  64. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  65. // between frames.
  66. void theaterChase(uint32_t color, int wait) {
  67.   for (int a = 0; a < 10; a++) { // Repeat 10 times...
  68.     for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
  69.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  70.       // 'c' counts up from 'b' to end of strip in steps of 3...
  71.       for (int c = b; c < strip.numPixels(); c += 3) {
  72.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  73.       }
  74.       strip.show(); // Update strip with new contents
  75.       delay(wait);  // Pause for a moment
  76.     }
  77.   }
  78. }

  79. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  80. void rainbow(int wait) {
  81.   // Hue of first pixel runs 5 complete loops through the color wheel.
  82.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  83.   // just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
  84.   // means we'll make 5*65536/256 = 1280 passes through this outer loop:
  85.   for (long firstPixelHue = 0; firstPixelHue < 5 * 65536; firstPixelHue += 256) {
  86.     for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  87.       // Offset pixel hue by an amount to make one full revolution of the
  88.       // color wheel (range of 65536) along the length of the strip
  89.       // (strip.numPixels() steps):
  90.       int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  91.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  92.       // optionally add saturation and value (brightness) (each 0 to 255).
  93.       // Here we're using just the single-argument hue variant. The result
  94.       // is passed through strip.gamma32() to provide 'truer' colors
  95.       // before assigning to each pixel:
  96.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  97.     }
  98.     strip.show(); // Update strip with new contents
  99.     delay(wait);  // Pause for a moment
  100.   }
  101. }

  102. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  103. void theaterChaseRainbow(int wait) {
  104.   int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  105.   for (int a = 0; a < 30; a++) { // Repeat 30 times...
  106.     for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
  107.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  108.       // 'c' counts up from 'b' to end of strip in increments of 3...
  109.       for (int c = b; c < strip.numPixels(); c += 3) {
  110.         // hue of pixel 'c' is offset by an amount to make one full
  111.         // revolution of the color wheel (range 65536) along the length
  112.         // of the strip (strip.numPixels() steps):
  113.         int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
  114.         uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  115.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  116.       }
  117.       strip.show();                // Update strip with new contents
  118.       delay(wait);                 // Pause for a moment
  119.       firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  120.     }
  121.   }
  122. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 09:33 | 显示全部楼层
【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)

  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目之九:一个基本的日常 NeoPixel 灯条测试程序(视频50秒)

  实验场景图

https://v.youku.com/v_show/id_XN ... hcb.playlsit.page.1




签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 09:38 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目之十:显示 RGBW 的 WHITE 通道的测试使用

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目之十:显示 RGBW 的 WHITE 通道的测试使用
  5. */

  6. #include <Adafruit_NeoPixel.h>
  7. #ifdef __AVR__
  8. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  9. #endif

  10. // Which pin on the Arduino is connected to the NeoPixels?
  11. // On a Trinket or Gemma we suggest changing this to 1:
  12. #define LED_PIN     6

  13. // How many NeoPixels are attached to the Arduino?
  14. #define LED_COUNT  8

  15. // NeoPixel brightness, 0 (min) to 255 (max)
  16. #define BRIGHTNESS 50 // Set BRIGHTNESS to about 1/5 (max = 255)

  17. // Declare our NeoPixel strip object:
  18. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
  19. // Argument 1 = Number of pixels in NeoPixel strip
  20. // Argument 2 = Arduino pin number (most are valid)
  21. // Argument 3 = Pixel type flags, add together as needed:
  22. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  23. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  24. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  25. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  26. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

  27. void setup() {
  28.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  29.   // Any other board, you can remove this part (but no harm leaving it):
  30. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  31.   clock_prescale_set(clock_div_1);
  32. #endif
  33.   // END of Trinket-specific code.

  34.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  35.   strip.show();            // Turn OFF all pixels ASAP
  36.   strip.setBrightness(BRIGHTNESS);
  37. }

  38. void loop() {
  39.   // Fill along the length of the strip in various colors...
  40.   colorWipe(strip.Color(255,   0,   0)     , 50); // Red
  41.   colorWipe(strip.Color(  0, 255,   0)     , 50); // Green
  42.   colorWipe(strip.Color(  0,   0, 255)     , 50); // Blue
  43.   colorWipe(strip.Color(  0,   0,   0, 255), 50); // True white (not RGB white)

  44.   whiteOverRainbow(75, 5);

  45.   pulseWhite(5);

  46.   rainbowFade2White(3, 3, 1);
  47. }

  48. // Fill strip pixels one after another with a color. Strip is NOT cleared
  49. // first; anything there will be covered pixel by pixel. Pass in color
  50. // (as a single 'packed' 32-bit value, which you can get by calling
  51. // strip.Color(red, green, blue) as shown in the loop() function above),
  52. // and a delay time (in milliseconds) between pixels.
  53. void colorWipe(uint32_t color, int wait) {
  54.   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  55.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  56.     strip.show();                          //  Update strip to match
  57.     delay(wait);                           //  Pause for a moment
  58.   }
  59. }

  60. void whiteOverRainbow(int whiteSpeed, int whiteLength) {

  61.   if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;

  62.   int      head          = whiteLength - 1;
  63.   int      tail          = 0;
  64.   int      loops         = 3;
  65.   int      loopNum       = 0;
  66.   uint32_t lastTime      = millis();
  67.   uint32_t firstPixelHue = 0;

  68.   for(;;) { // Repeat forever (or until a 'break' or 'return')
  69.     for(int i=0; i<strip.numPixels(); i++) {  // For each pixel in strip...
  70.       if(((i >= tail) && (i <= head)) ||      //  If between head & tail...
  71.          ((tail > head) && ((i >= tail) || (i <= head)))) {
  72.         strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
  73.       } else {                                             // else set rainbow
  74.         int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  75.         strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  76.       }
  77.     }

  78.     strip.show(); // Update strip with new contents
  79.     // There's no delay here, it just runs full-tilt until the timer and
  80.     // counter combination below runs out.

  81.     firstPixelHue += 40; // Advance just a little along the color wheel

  82.     if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
  83.       if(++head >= strip.numPixels()) {      // Advance head, wrap around
  84.         head = 0;
  85.         if(++loopNum >= loops) return;
  86.       }
  87.       if(++tail >= strip.numPixels()) {      // Advance tail, wrap around
  88.         tail = 0;
  89.       }
  90.       lastTime = millis();                   // Save time of last movement
  91.     }
  92.   }
  93. }

  94. void pulseWhite(uint8_t wait) {
  95.   for(int j=0; j<256; j++) { // Ramp up from 0 to 255
  96.     // Fill entire strip with white at gamma-corrected brightness level 'j':
  97.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  98.     strip.show();
  99.     delay(wait);
  100.   }

  101.   for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
  102.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  103.     strip.show();
  104.     delay(wait);
  105.   }
  106. }

  107. void rainbowFade2White(int wait, int rainbowLoops, int whiteLoops) {
  108.   int fadeVal=0, fadeMax=100;

  109.   // Hue of first pixel runs 'rainbowLoops' complete loops through the color
  110.   // wheel. Color wheel has a range of 65536 but it's OK if we roll over, so
  111.   // just count from 0 to rainbowLoops*65536, using steps of 256 so we
  112.   // advance around the wheel at a decent clip.
  113.   for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536;
  114.     firstPixelHue += 256) {

  115.     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...

  116.       // Offset pixel hue by an amount to make one full revolution of the
  117.       // color wheel (range of 65536) along the length of the strip
  118.       // (strip.numPixels() steps):
  119.       uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());

  120.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  121.       // optionally add saturation and value (brightness) (each 0 to 255).
  122.       // Here we're using just the three-argument variant, though the
  123.       // second value (saturation) is a constant 255.
  124.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
  125.         255 * fadeVal / fadeMax)));
  126.     }

  127.     strip.show();
  128.     delay(wait);

  129.     if(firstPixelHue < 65536) {                              // First loop,
  130.       if(fadeVal < fadeMax) fadeVal++;                       // fade in
  131.     } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
  132.       if(fadeVal > 0) fadeVal--;                             // fade out
  133.     } else {
  134.       fadeVal = fadeMax; // Interim loop, make sure fade is at max
  135.     }
  136.   }

  137.   for(int k=0; k<whiteLoops; k++) {
  138.     for(int j=0; j<256; j++) { // Ramp up 0 to 255
  139.       // Fill entire strip with white at gamma-corrected brightness level 'j':
  140.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  141.       strip.show();
  142.     }
  143.     delay(1000); // Pause 1 second
  144.     for(int j=255; j>=0; j--) { // Ramp down 255 to 0
  145.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  146.       strip.show();
  147.     }
  148.   }

  149.   delay(500); // Pause 1/2 second
  150. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 09:42 | 显示全部楼层
  实验场景图

15.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 09:44 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十一:使用按钮控制更换动态颜色

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目十一:使用按钮控制更换动态颜色
  5. */

  6. #include <Adafruit_NeoPixel.h>
  7. #ifdef __AVR__
  8. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  9. #endif

  10. // Digital IO pin connected to the button. This will be driven with a
  11. // pull-up resistor so the switch pulls the pin to ground momentarily.
  12. // On a high -> low transition the button press logic will execute.
  13. #define BUTTON_PIN   2

  14. #define PIXEL_PIN    6  // Digital IO pin connected to the NeoPixels.

  15. #define PIXEL_COUNT 8  // Number of NeoPixels

  16. // Declare our NeoPixel strip object:
  17. Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  18. // Argument 1 = Number of pixels in NeoPixel strip
  19. // Argument 2 = Arduino pin number (most are valid)
  20. // Argument 3 = Pixel type flags, add together as needed:
  21. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  22. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  23. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  24. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  25. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

  26. boolean oldState = HIGH;
  27. int     mode     = 0;    // Currently-active animation mode, 0-9

  28. void setup() {
  29.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  30.   strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  31.   strip.show();  // Initialize all pixels to 'off'
  32. }

  33. void loop() {
  34.   // Get current button state.
  35.   boolean newState = digitalRead(BUTTON_PIN);

  36.   // Check if state changed from high to low (button press).
  37.   if((newState == LOW) && (oldState == HIGH)) {
  38.     // Short delay to debounce button.
  39.     delay(20);
  40.     // Check if button is still low after debounce.
  41.     newState = digitalRead(BUTTON_PIN);
  42.     if(newState == LOW) {      // Yes, still low
  43.       if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
  44.       switch(mode) {           // Start the new animation...
  45.         case 0:
  46.           colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
  47.           break;
  48.         case 1:
  49.           colorWipe(strip.Color(255,   0,   0), 50);    // Red
  50.           break;
  51.         case 2:
  52.           colorWipe(strip.Color(  0, 255,   0), 50);    // Green
  53.           break;
  54.         case 3:
  55.           colorWipe(strip.Color(  0,   0, 255), 50);    // Blue
  56.           break;
  57.         case 4:
  58.           theaterChase(strip.Color(127, 127, 127), 50); // White
  59.           break;
  60.         case 5:
  61.           theaterChase(strip.Color(127,   0,   0), 50); // Red
  62.           break;
  63.         case 6:
  64.           theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  65.           break;
  66.         case 7:
  67.           rainbow(10);
  68.           break;
  69.         case 8:
  70.           theaterChaseRainbow(50);
  71.           break;
  72.       }
  73.     }
  74.   }

  75.   // Set the last-read button state to the old state.
  76.   oldState = newState;
  77. }

  78. // Fill strip pixels one after another with a color. Strip is NOT cleared
  79. // first; anything there will be covered pixel by pixel. Pass in color
  80. // (as a single 'packed' 32-bit value, which you can get by calling
  81. // strip.Color(red, green, blue) as shown in the loop() function above),
  82. // and a delay time (in milliseconds) between pixels.
  83. void colorWipe(uint32_t color, int wait) {
  84.   for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  85.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  86.     strip.show();                          //  Update strip to match
  87.     delay(wait);                           //  Pause for a moment
  88.   }
  89. }

  90. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  91. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  92. // between frames.
  93. void theaterChase(uint32_t color, int wait) {
  94.   for(int a=0; a<10; a++) {  // Repeat 10 times...
  95.     for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
  96.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  97.       // 'c' counts up from 'b' to end of strip in steps of 3...
  98.       for(int c=b; c<strip.numPixels(); c += 3) {
  99.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  100.       }
  101.       strip.show(); // Update strip with new contents
  102.       delay(wait);  // Pause for a moment
  103.     }
  104.   }
  105. }

  106. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  107. void rainbow(int wait) {
  108.   // Hue of first pixel runs 3 complete loops through the color wheel.
  109.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  110.   // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
  111.   // means we'll make 3*65536/256 = 768 passes through this outer loop:
  112.   for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
  113.     for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
  114.       // Offset pixel hue by an amount to make one full revolution of the
  115.       // color wheel (range of 65536) along the length of the strip
  116.       // (strip.numPixels() steps):
  117.       int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  118.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  119.       // optionally add saturation and value (brightness) (each 0 to 255).
  120.       // Here we're using just the single-argument hue variant. The result
  121.       // is passed through strip.gamma32() to provide 'truer' colors
  122.       // before assigning to each pixel:
  123.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  124.     }
  125.     strip.show(); // Update strip with new contents
  126.     delay(wait);  // Pause for a moment
  127.   }
  128. }

  129. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  130. void theaterChaseRainbow(int wait) {
  131.   int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  132.   for(int a=0; a<30; a++) {  // Repeat 30 times...
  133.     for(int b=0; b<3; b++) { //  'b' counts from 0 to 2...
  134.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  135.       // 'c' counts up from 'b' to end of strip in increments of 3...
  136.       for(int c=b; c<strip.numPixels(); c += 3) {
  137.         // hue of pixel 'c' is offset by an amount to make one full
  138.         // revolution of the color wheel (range 65536) along the length
  139.         // of the strip (strip.numPixels() steps):
  140.         int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
  141.         uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  142.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  143.       }
  144.       strip.show();                // Update strip with new contents
  145.       delay(wait);                 // Pause for a moment
  146.       firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  147.     }
  148.   }
  149. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 09:57 | 显示全部楼层
  实验场景图

16.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐

原价299元【语音开发板套件】限时免费领!
原价299元【语音开发板套
教你让OLED动起来!多重字符串版!
教你让OLED动起来!多重字
大家都知道:arduino单片机是单线程的 而上次教程中的多段字符串的运行速度必须一致
TTGO T8 1.7.1使用TFT_eSPI库驱动2.4寸ILI9341屏幕显示板载SD内图片
TTGO T8 1.7.1使用TFT_eSP
TTGO T8 1.7.1采用Espressif官方ESP32-WROVER模块制作,4MB闪存和8MB PSRAM,支持TF
TTGO TFT屏幕Arduino使用的小结
TTGO TFT屏幕Arduino使用
TTGO TFT屏幕是一款ESP32和1.44寸LCD屏幕组合的产品,屏幕由ST7789驱动。ESP32自带520
分享我的第一个点灯程序——家庭控制中心
分享我的第一个点灯程序—
2020年,在B站上初识ESP8266,被ESP8266的低门槛、高性价比深深吸引,2020年6月20日,
Copyright   ©2015-2016  Arduino中文社区  Powered by©Discuz!   
快速回复 返回顶部 返回列表