极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

12
返回列表 发新帖
楼主: eagler8

【Arduino】168种传感器模块系列实验(147)---64位WS2812点阵屏

[复制链接]
 楼主| 发表于 2020-3-1 10:14:24 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 10:37:29 | 显示全部楼层
程序之四:黑客帝国绿色流水灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   安装FastLED库,工具—管理库—搜索FastLED—安装
  5.   安装Adafruit_NeoPixel库,
  6.   下载https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

  7.   程序之四:黑客帝国绿色流水灯
  8.   实验接线
  9.   Module    UNO
  10.   VCC —— 3.3V
  11.   GND —— GND
  12.   DI  ——  D6
  13. */

  14. #include <Adafruit_NeoPixel.h>
  15. #define PIN 6
  16. #define MAX_LED 64

  17. #define ADD true
  18. #define SUB false

  19. int val = 0;
  20. boolean stat = ADD;

  21. Adafruit_NeoPixel strip = Adafruit_NeoPixel( MAX_LED, PIN, NEO_RGB + NEO_KHZ800 );

  22. void setup()
  23. {
  24.   strip.begin();
  25.   strip.show();
  26. }

  27. void loop()
  28. {
  29.   uint8_t i, a = 0;
  30.   uint32_t color = strip.Color(190, 50, 0);
  31.   while (a < 65)
  32.   {
  33.     for (i = 0; i < 64; i++)
  34.     {
  35.       if (i == a) strip.setPixelColor(i, color);
  36.       else strip.setPixelColor(i, 0);
  37.     }
  38.     strip.show();
  39.     delay(25);
  40.     a++;
  41. }
  42. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 10:48:16 | 显示全部楼层
程序之五:RGB传输测试满屏变幻彩灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  5.   安装Adafruit_NeoPixel库,
  6.   下载https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

  7.   程序之五:RGB传输测试满屏变幻彩灯
  8.   实验接线
  9.   Module    UNO
  10.   VCC —— 3.3V
  11.   GND —— GND
  12.   DI  ——  D6
  13. */


  14. #include <Adafruit_NeoPixel.h>
  15. #ifdef __AVR__
  16. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  17. #endif

  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. // How many NeoPixels are attached to the Arduino?
  22. #define LED_COUNT  60

  23. // NeoPixel brightness, 0 (min) to 255 (max)
  24. #define BRIGHTNESS 50

  25. // Declare our NeoPixel strip object:
  26. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + 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. void setup() {
  36.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  37.   // Any other board, you can remove this part (but no harm leaving it):
  38. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  39.   clock_prescale_set(clock_div_1);
  40. #endif
  41.   // END of Trinket-specific code.

  42.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  43.   strip.show();            // Turn OFF all pixels ASAP
  44.   strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  45. }

  46. void loop() {
  47.   // Fill along the length of the strip in various colors...
  48.   colorWipe(strip.Color(255,   0,   0)     , 50); // Red
  49.   colorWipe(strip.Color(  0, 255,   0)     , 50); // Green
  50.   colorWipe(strip.Color(  0,   0, 255)     , 50); // Blue
  51.   colorWipe(strip.Color(  0,   0,   0, 255), 50); // True white (not RGB white)

  52.   whiteOverRainbow(75, 5);

  53.   pulseWhite(5);

  54.   rainbowFade2White(3, 3, 1);
  55. }

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

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

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

  70.   int      head          = whiteLength - 1;
  71.   int      tail          = 0;
  72.   int      loops         = 3;
  73.   int      loopNum       = 0;
  74.   uint32_t lastTime      = millis();
  75.   uint32_t firstPixelHue = 0;

  76.   for(;;) { // Repeat forever (or until a 'break' or 'return')
  77.     for(int i=0; i<strip.numPixels(); i++) {  // For each pixel in strip...
  78.       if(((i >= tail) && (i <= head)) ||      //  If between head & tail...
  79.          ((tail > head) && ((i >= tail) || (i <= head)))) {
  80.         strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); // Set white
  81.       } else {                                             // else set rainbow
  82.         int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  83.         strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  84.       }
  85.     }

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

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

  90.     if((millis() - lastTime) > whiteSpeed) { // Time to update head/tail?
  91.       if(++head >= strip.numPixels()) {      // Advance head, wrap around
  92.         head = 0;
  93.         if(++loopNum >= loops) return;
  94.       }
  95.       if(++tail >= strip.numPixels()) {      // Advance tail, wrap around
  96.         tail = 0;
  97.       }
  98.       lastTime = millis();                   // Save time of last movement
  99.     }
  100.   }
  101. }

  102. void pulseWhite(uint8_t wait) {
  103.   for(int j=0; j<256; j++) { // Ramp up from 0 to 255
  104.     // Fill entire strip with white at gamma-corrected brightness level 'j':
  105.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  106.     strip.show();
  107.     delay(wait);
  108.   }

  109.   for(int j=255; j>=0; j--) { // Ramp down from 255 to 0
  110.     strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  111.     strip.show();
  112.     delay(wait);
  113.   }
  114. }

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

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

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

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

  128.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  129.       // optionally add saturation and value (brightness) (each 0 to 255).
  130.       // Here we're using just the three-argument variant, though the
  131.       // second value (saturation) is a constant 255.
  132.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
  133.         255 * fadeVal / fadeMax)));
  134.     }

  135.     strip.show();
  136.     delay(wait);

  137.     if(firstPixelHue < 65536) {                              // First loop,
  138.       if(fadeVal < fadeMax) fadeVal++;                       // fade in
  139.     } else if(firstPixelHue >= ((rainbowLoops-1) * 65536)) { // Last loop,
  140.       if(fadeVal > 0) fadeVal--;                             // fade out
  141.     } else {
  142.       fadeVal = fadeMax; // Interim loop, make sure fade is at max
  143.     }
  144.   }

  145.   for(int k=0; k<whiteLoops; k++) {
  146.     for(int j=0; j<256; j++) { // Ramp up 0 to 255
  147.       // Fill entire strip with white at gamma-corrected brightness level 'j':
  148.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  149.       strip.show();
  150.     }
  151.     delay(1000); // Pause 1 second
  152.     for(int j=255; j>=0; j--) { // Ramp down 255 to 0
  153.       strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
  154.       strip.show();
  155.     }
  156.   }

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

回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 11:21:30 | 显示全部楼层

程序之五:RGB传输测试满屏变幻彩灯 视频(52秒)

链接:https://v.youku.com/v_show/id_XNDU2ODQ2MDI0NA==.html




回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 11:32:00 | 显示全部楼层
【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  安装Adafruit_NeoPixel库,
  下载https://learn.adafruit.com/adafr ... ibrary-installation

  程序之六:复合流水彩虹灯
  实验接线
  Module    UNO
  VCC —— 3.3V
  GND —— GND
  DI  ——  D6

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  5.   安装Adafruit_NeoPixel库,
  6.   下载https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

  7.   程序之六:复合流水彩虹灯
  8.   实验接线
  9.   Module    UNO
  10.   VCC —— 3.3V
  11.   GND —— GND
  12.   DI  ——  D6
  13. */

  14. #include <Adafruit_NeoPixel.h>

  15. #define PIN 6
  16. #define BRIGHTNESS 64

  17. Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);

  18. void setup() {
  19.   strip.setBrightness(BRIGHTNESS);
  20.   strip.begin();
  21.   strip.show();
  22. }

  23. void loop() {
  24.   colorWipe(strip.Color(150, 0, 0), 50); // Red
  25.   colorWipe(strip.Color(0, 150, 0), 50); // Green
  26.   colorWipe(strip.Color(0, 0, 150), 50); // Blue
  27.   colorWipe(strip.Color(150, 150, 150), 50); // BlueWite
  28.   rainbowCycle(1);

  29. }

  30. void colorWipe(uint32_t c, uint8_t wait) {
  31.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  32.     strip.setPixelColor(i, c);
  33.     strip.show();
  34.     delay(wait);
  35.   }
  36. }

  37. void rainbow(uint8_t wait) {
  38.   uint16_t i, j;
  39.   for (j = 0; j < 256; j++) {
  40.     for (i = 0; i < strip.numPixels(); i++) {
  41.       strip.setPixelColor(i, Wheel((i + j) & 255 ));
  42.     }
  43.     strip.show();
  44.     delay(wait);
  45.   }
  46. }

  47. void rainbowCycle(uint8_t wait) {
  48.   uint16_t i, j;
  49.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  50.     for (i = 0; i < strip.numPixels(); i++) {
  51.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  52.     }
  53.     strip.show();
  54.     delay(wait);
  55.   }
  56. }

  57. uint32_t Wheel(byte WheelPos) {
  58.   if (WheelPos < 85) {
  59.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  60.   } else if (WheelPos < 170) {
  61.     WheelPos -= 85;
  62.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  63.   } else {
  64.     WheelPos -= 170;
  65.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  66.   }
  67. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 11:58:13 | 显示全部楼层
程序之七:复合飘逸彩虹满屏灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  5.   安装Adafruit_NeoPixel库,
  6.   下载https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

  7.   程序之七:复合飘逸彩虹满屏灯
  8.   实验接线
  9.   Module    UNO
  10.   VCC —— 3.3V
  11.   GND —— GND
  12.   DI  ——  D6
  13. */

  14. #include <Adafruit_NeoPixel.h>
  15. #ifdef __AVR__
  16.   #include <avr/power.h>
  17. #endif

  18. #define PIN 6

  19. // Parameter 1 = number of pixels in strip
  20. // Parameter 2 = Arduino pin number (most are valid)
  21. // Parameter 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. Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);

  28. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  29. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  30. // and minimize distance between Arduino and first pixel.  Avoid connecting
  31. // on a live circuit...if you must, connect GND first.

  32. void setup() {
  33.   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  34.   #if defined (__AVR_ATtiny85__)
  35.     if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  36.   #endif
  37.   // End of trinket special code

  38.   strip.begin();
  39.   strip.setBrightness(50);
  40.   strip.show(); // Initialize all pixels to 'off'
  41. }

  42. void loop() {
  43.   // Some example procedures showing how to display to the pixels:
  44.   colorWipe(strip.Color(255, 0, 0), 50); // Red
  45.   colorWipe(strip.Color(0, 255, 0), 50); // Green
  46.   colorWipe(strip.Color(0, 0, 255), 50); // Blue
  47. //colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  48.   // Send a theater pixel chase in...
  49.   theaterChase(strip.Color(127, 127, 127), 50); // White
  50.   theaterChase(strip.Color(127, 0, 0), 50); // Red
  51.   theaterChase(strip.Color(0, 0, 127), 50); // Blue

  52.   rainbow(20);
  53.   rainbowCycle(20);
  54.   theaterChaseRainbow(50);
  55. }

  56. // Fill the dots one after the other with a color
  57. void colorWipe(uint32_t c, uint8_t wait) {
  58.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  59.     strip.setPixelColor(i, c);
  60.     strip.show();
  61.     delay(wait);
  62.   }
  63. }

  64. void rainbow(uint8_t wait) {
  65.   uint16_t i, j;

  66.   for(j=0; j<256; j++) {
  67.     for(i=0; i<strip.numPixels(); i++) {
  68.       strip.setPixelColor(i, Wheel((i+j) & 255));
  69.     }
  70.     strip.show();
  71.     delay(wait);
  72.   }
  73. }

  74. // Slightly different, this makes the rainbow equally distributed throughout
  75. void rainbowCycle(uint8_t wait) {
  76.   uint16_t i, j;

  77.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  78.     for(i=0; i< strip.numPixels(); i++) {
  79.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  80.     }
  81.     strip.show();
  82.     delay(wait);
  83.   }
  84. }

  85. //Theatre-style crawling lights.
  86. void theaterChase(uint32_t c, uint8_t wait) {
  87.   for (int j=0; j<10; j++) {  //do 10 cycles of chasing
  88.     for (int q=0; q < 3; q++) {
  89.       for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  90.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  91.       }
  92.       strip.show();

  93.       delay(wait);

  94.       for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  95.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  96.       }
  97.     }
  98.   }
  99. }

  100. //Theatre-style crawling lights with rainbow effect
  101. void theaterChaseRainbow(uint8_t wait) {
  102.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  103.     for (int q=0; q < 3; q++) {
  104.       for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  105.         strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  106.       }
  107.       strip.show();

  108.       delay(wait);

  109.       for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
  110.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  111.       }
  112.     }
  113.   }
  114. }

  115. // Input a value 0 to 255 to get a color value.
  116. // The colours are a transition r - g - b - back to r.
  117. uint32_t Wheel(byte WheelPos) {
  118.   WheelPos = 255 - WheelPos;
  119.   if(WheelPos < 85) {
  120.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  121.   }
  122.   if(WheelPos < 170) {
  123.     WheelPos -= 85;
  124.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  125.   }
  126.   WheelPos -= 170;
  127.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  128. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 12:31:05 | 显示全部楼层
  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  5.   安装Adafruit_NeoPixel库,
  6.   下载https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

  7.   程序之八:复合彩虹滚动流水灯
  8.   实验接线
  9.   Module    UNO
  10.   VCC —— 3.3V
  11.   GND —— GND
  12.   DI  ——  D6
  13. */

  14. #include <Adafruit_NeoPixel.h>
  15. #ifdef __AVR__
  16. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  17. #endif

  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. // How many NeoPixels are attached to the Arduino?
  22. #define LED_COUNT 64

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


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

  34. void setup() {
  35.   // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
  36.   // Any other board, you can remove this part (but no harm leaving it):
  37. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  38.   clock_prescale_set(clock_div_1);
  39. #endif
  40.   // END of Trinket-specific code.

  41.   strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  42.   strip.show();            // Turn OFF all pixels ASAP
  43.   strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
  44. }


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

  46. void loop() {
  47.   // Fill along the length of the strip in various colors...
  48.   colorWipe(strip.Color(255,   0,   0), 50); // Red
  49.   colorWipe(strip.Color(  0, 255,   0), 50); // Green
  50.   colorWipe(strip.Color(  0,   0, 255), 50); // Blue

  51.   // Do a theater marquee effect in various colors...
  52.   theaterChase(strip.Color(127, 127, 127), 50); // White, half brightness
  53.   theaterChase(strip.Color(127,   0,   0), 50); // Red, half brightness
  54.   theaterChase(strip.Color(  0,   0, 127), 50); // Blue, half brightness

  55.   rainbow(10);             // Flowing rainbow cycle along the whole strip
  56.   theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
  57. }


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

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

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

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

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

使用道具 举报

 楼主| 发表于 2020-3-1 12:39:13 | 显示全部楼层

程序之九:按键控制进入下段彩灯程序
  实验接线
  Module    UNO
  VCC —— 3.3V
  GND —— GND
  DI  ——  D6
  ws  ——  D2

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百四十六:64位 WS2812B8*8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
  5.   安装Adafruit_NeoPixel库,
  6.   下载https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

  7.   程序之九:按键控制进入下段彩灯程序
  8.   实验接线
  9.   Module    UNO
  10.   VCC —— 3.3V
  11.   GND —— GND
  12.   DI  ——  D6
  13.   ws  ——  D2
  14. */

  15. #include <Adafruit_NeoPixel.h>
  16. #ifdef __AVR__
  17. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  18. #endif

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

  23. #define PIXEL_PIN    7  // Digital IO pin connected to the NeoPixels.

  24. #define PIXEL_COUNT 64 // Number of NeoPixels

  25. // Declare our NeoPixel strip object:
  26. Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_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. boolean oldState = HIGH;
  36. int     mode     = 0;    // Currently-active animation mode, 0-9

  37. void setup() {
  38.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  39.   strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  40.   strip.show();  // Initialize all pixels to 'off'
  41. }

  42. void loop() {
  43.   // Get current button state.
  44.   boolean newState = digitalRead(BUTTON_PIN);

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

  84.   // Set the last-read button state to the old state.
  85.   oldState = newState;
  86. }

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

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

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

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

使用道具 举报

 楼主| 发表于 2020-3-1 13:09:39 | 显示全部楼层
实验仿真编程(linkboy3.6)之一

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 13:22:12 | 显示全部楼层
实验仿真编程(linkboy3.6)之二

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-1 14:21:25 | 显示全部楼层
实验仿真编程(linkboy3.6)之三

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-2 18:12:52 | 显示全部楼层

程序之四:黑客帝国绿色流水灯








本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-4 16:35:57 | 显示全部楼层
视频——程序之六:复合流水彩虹灯

https://v.youku.com/v_show/id_XNDU2ODUwMTE2NA==.html

回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 13:18 , Processed in 0.041577 second(s), 15 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表