数码之家

标题: 【Arduino】168种传感器模块系列实验(147)---64位WS2812点阵屏 [打印本页]

作者: eagler8    时间: 2020-2-29 16:01
标题: 【Arduino】168种传感器模块系列实验(147)---64位WS2812点阵屏
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手试试做实验,不管成功与否,都会记录下来---小小的进步或是搞不定的问题,希望能够抛砖引玉。

【Arduino】168种传感器模块系列实验(资料+代码+图形+仿真)
实验一百四十七:64位 WS2812B 8x8 RGB 5050 LED模块 ws2812s像素点阵屏


[attach]624725[/attach]

作者: eagler8    时间: 2020-2-29 16:05
WS2812B
是一个集控制电路与发光电路于一体的智能外控LED光源。其外型与一个5050LED灯珠相同,每个元件即为一个像素点。像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路,还包含有高精度的内部振荡器和12V高压可编程定电流控制部分,有效保证了像素点光的颜色高度一致。

数据协议采用单线归零码的通讯方式,像素点在上电复位以后,DIN端接受从控制器传输过来的数据,首先送过来的24bit数据被第一个像素点提取后,送到像素点内部的数据锁存器,剩余的数据经过内部整形处理电路整形放大后通过DO端口开始转发输出给下一个级联的像素点,每经过一个像素点的传输,信号减少24bit。像素点采用自动整形转发技术,使得该像素点的级联个数不受信号传送的限制,仅仅受限信号传输速度要求。

LED具有低电压驱动,环保节能,亮度高,散射角度大,一致性好,超低功率,超长寿命等优点。将控制电路集成于LED上面,电路变得更加简单,体积小,安装更加简便。
[attach]624727[/attach]

作者: eagler8    时间: 2020-2-29 16:17
[attach]624756[/attach]

作者: eagler8    时间: 2020-2-29 16:44
[attach]624797[/attach]



作者: eagler8    时间: 2020-2-29 16:46
[attach]624804[/attach]

作者: eagler8    时间: 2020-2-29 16:48
[attach]624805[/attach]

作者: eagler8    时间: 2020-3-1 08:52
[attach]625592[/attach]

WS2812B主要特点
智能反接保护,电源反接不会损坏IC。
IC控制电路与LED点光源公用一个电源。
控制电路与RGB芯片集成在一个5050封装的元器件中,构成一个完整的外控像素点。
内置信号整形电路,任何一个像素点收到信号后经过波形整形再输出,保证线路波形畸变不会累加。
内置上电复位和掉电复位电路。
每个像素点的三基色颜色可实现256级亮度显示,完成16777216种颜色的全真色彩显示,扫描频率不低于400Hz/s。
串行级联接口,能通过一根信号线完成数据的接收与解码。
任意两点传传输距离在不超过5米时无需增加任何电路。
当刷新速率30帧/秒时,级联数不小于1024点。
数据发送速度可达800Kbps。
光的颜色高度一致,性价比高。

主要应用领域
LED全彩发光字灯串,LED全彩模组, LED全彩软灯条硬灯条,LED护栏管。
LED点光源,LED像素屏,LED异形屏,各种电子产品,电器设备跑马灯。


作者: eagler8    时间: 2020-3-1 09:12
[attach]625625[/attach]

名称:WS2812全彩矩阵LED模块(64位灯珠)
尺寸:6.5*6.5cm
芯片:WS2812B(内置于LED)
LED:5050封装RGB全彩高亮
电压:5V
端口:数字
平台:Arduino 单片机
控制方式:内置控制芯片,只需一个IO口即可控制


作者: eagler8    时间: 2020-3-1 09:22
模块电原理图

[attach]625626[/attach]

作者: eagler8    时间: 2020-3-1 09:33
实验涉及到的几个WS2812B相关库
安装FastLED库,工具—管理库—搜索FastLED—安装
安装NeoPixel库,工具—管理库—搜索NeoPixel—安装
安装Adafruit_NeoPixel库,
下载https://learn.adafruit.com/adafr ... ibrary-installation

[attach]625627[/attach]


作者: eagler8    时间: 2020-3-1 09:40
[attach]625629[/attach]

作者: eagler8    时间: 2020-3-1 09:45

  程序之一:简单蓝色流水灯
  实验接线
  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. #ifdef __AVR__
  16. #include <avr/power.h> //16兆赫Adafruit饰品所需
  17. #endif

  18. // Arduino上的哪个插脚与NeoPixels相连?
  19. #define PIN        6

  20. // Arduino上有多少个LED?
  21. #define NUMPIXELS 64

  22. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

  23. #define DELAYVAL 30 //像素之间暂停的时间(毫秒)

  24. void setup() {
  25. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  26.   clock_prescale_set(clock_div_1);

  27. #endif
  28.   // 特定代码结束

  29.   pixels.begin(); // 初始化neopxel strip对象(必需)
  30. }

  31. void loop() {
  32.   pixels.clear(); //将所有像素颜色设置为“关闭”

  33.   //一串中的第一个新混合物是0,第二个是1,一直往上
  34.   //像素数减1
  35.   for (int i = 0; i < NUMPIXELS; i++) { // 对于每个像素......

  36.     //Color()接受RGB值,从0,0,0到255,255,255
  37.     //这里我们使用的是中等明亮的蓝色:
  38.     pixels.setPixelColor(i, pixels.Color(0, 0, 150));

  39.     pixels.show();   // 将更新的像素颜色发送到硬件

  40.     delay(DELAYVAL); // 在下一个通过循环之前暂停
  41.   }
  42. }
复制代码



作者: eagler8    时间: 2020-3-1 09:50
[attach]625634[/attach]

作者: eagler8    时间: 2020-3-1 10:03
程序之二:粉色单灯流水灯

  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 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();           //初始化Adafruit_NeoPixel;
  25.   strip.show();           //显示所有LED为关状态;
  26. }

  27. void loop()
  28. {
  29.   uint8_t i,a=0;                                       
  30.   uint32_t color = strip.Color(0, 150, 150);         //选择所显示的颜色
  31.   while(a<65)
  32.   {
  33.       for(i=0;i<64;i++)
  34.       {
  35.         if(i==a) strip.setPixelColor(i, color);     //第几个LED点亮;
  36.         else strip.setPixelColor(i, 0);             //使其他LED全灭;
  37.       }
  38.        strip.show();                                //是LED显示所选的颜色;
  39.        delay(50);                                   //延时50ms;
  40.        a++;                                         
  41.   }
  42. }
复制代码



作者: eagler8    时间: 2020-3-1 10:09

程序之三:四色交替流水灯

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

  5.   程序之三:四色交替流水灯
  6.   实验接线
  7.   Module    UNO
  8.   VCC —— 3.3V
  9.   GND —— GND
  10.   DI  ——  D6
  11. */

  12. #include <FastLED.h>
  13. #define LED_PIN     6
  14. #define NUM_LEDS    64
  15. CRGB leds[NUM_LEDS];

  16. void setup() {
  17.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  18. }
  19. void loop() {
  20.   for (int i = 0; i <= 63; i++) {
  21.     leds[i] = CRGB ( 0, 0, 200);
  22.     FastLED.show();
  23.     delay(33);
  24.   }
  25.   for (int i = 63; i >= 0; i--) {
  26.     leds[i] = CRGB ( 89, 0, 0);
  27.     FastLED.show();
  28.     delay(33);
  29.   }
  30.   for (int i = 0; i <= 63; i++) {
  31.     leds[i] = CRGB ( 0, 89, 0);
  32.     FastLED.show();
  33.     delay(33);
  34.   }
  35.    for (int i = 63; i >= 0; i--) {
  36.     leds[i] = CRGB ( 89, 0, 200);
  37.     FastLED.show();
  38.     delay(33);
  39.   }
  40. }
复制代码



作者: eagler8    时间: 2020-3-1 10:12
[attach]625651[/attach]

作者: eagler8    时间: 2020-3-1 10:35
程序之四:黑客帝国绿色流水灯

  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. }
复制代码



作者: eagler8    时间: 2020-3-1 10:44
程序之五: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. }
复制代码






作者: eagler8    时间: 2020-3-1 11:16
程序之五:RGB传输测试满屏变幻彩灯 视频(52秒)

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

作者: eagler8    时间: 2020-3-1 11:30
【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. }
复制代码



作者: eagler8    时间: 2020-3-1 11:57
程序之七:复合飘逸彩虹满屏灯

  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. }
复制代码



作者: eagler8    时间: 2020-3-1 12:28
  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. }
复制代码



作者: eagler8    时间: 2020-3-1 12:33
程序之九:按键控制进入下段彩灯程序
  实验接线
  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. }
复制代码



作者: eagler8    时间: 2020-3-1 13:07
实验仿真编程(linkboy3.6)之一

[attach]625890[/attach]

作者: eagler8    时间: 2020-3-1 13:20
实验仿真编程(linkboy3.6)之二

[attach]625898[/attach]

作者: eagler8    时间: 2020-3-1 14:16
实验仿真编程(linkboy3.6)之三

[attach]626014[/attach]

作者: eagler8    时间: 2020-3-2 18:09
程序之四:黑客帝国绿色流水灯

[attach]627827[/attach]


作者: eagler8    时间: 2020-3-4 16:39
视频——程序之六:复合流水彩虹灯
https://v.youku.com/v_show/id_XNDU2ODUwMTE2NA==.html





作者: eagler8    时间: 2022-7-18 13:18
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十四:法式流水火花屏

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   项目四十四:法式流水火花屏
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  30
  9. #define LED_TYPE    WS2812B
  10. #define COLOR_ORDER GRB
  11. const uint8_t kMatrixWidth  = 8;
  12. const uint8_t kMatrixHeight = 8;
  13. #define WIDTH kMatrixWidth
  14. #define HEIGHT kMatrixHeight
  15. #define ROWS kMatrixWidth
  16. #define COLS kMatrixHeight
  17. #define LED_ROWS kMatrixWidth
  18. #define LED_COLS kMatrixHeight
  19. CRGB leds[kMatrixWidth * kMatrixHeight];
  20. const bool    kMatrixSerpentineLayout = true;
  21. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  22. #define enlargedOBJECT_MAX_COUNT WIDTH*2
  23. #define SPEED_ADJ (float)NUM_LEDS/512

  24. //speed control
  25. byte speed = 127; // 1-255

  26. //scale control
  27. byte scale = 150; //1-255

  28. void setup() {
  29.   delay(3000);
  30.   LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  31.   LEDS.setBrightness(BRIGHTNESS);
  32. }

  33. static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};
  34. extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};
  35. extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};
  36. extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};
  37. extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};
  38. extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};
  39. extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};
  40. extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
  41. extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};
  42. extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink};

  43. static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max) {
  44.   return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
  45. }

  46. #define SPARKS_AM WIDTH
  47. float FADE_KOEF = 10;
  48. float SpeedK = .98;
  49. float SpeedDecX = .01;
  50. float SpeedDecY = 0;
  51. #define Board 1
  52. #define GravityX 0
  53. #define GravityY 1

  54. int sparksPos[2][SPARKS_AM];
  55. float sparksSpeed[2][SPARKS_AM];
  56. float sparksFade[SPARKS_AM];
  57. byte sparksColor[SPARKS_AM];

  58. int genPos[2];
  59. int gravityPos[2];
  60. bool run = true;

  61. bool loadingFlag = true;
  62. void reg(byte id) {
  63.   sparksPos[0][id] = genPos[0];
  64.   sparksPos[1][id] = genPos[1];
  65.   sparksSpeed[0][id] = random(-10, 10);
  66.   sparksSpeed[1][id] = random(-10, 10);
  67.   sparksFade[id] = 255;
  68.   sparksColor[id] = random();
  69. }

  70. void physics(byte id) {
  71.   if (SpeedK) {
  72.     if (GravityX) {
  73.       if (gravityPos[0] < sparksPos[0][id])
  74.         sparksSpeed[0][id] -= SpeedK;
  75.       else
  76.         sparksSpeed[0][id] += SpeedK;
  77.     }
  78.     if (GravityY) {
  79.       if (gravityPos[1] < sparksPos[1][id])
  80.         sparksSpeed[1][id] -= SpeedK;
  81.       else
  82.         sparksSpeed[1][id] += SpeedK;
  83.     }
  84.   }
  85.   sparksFade[id] -= (255. / (float)((HEIGHT + WIDTH) * FADE_KOEF));
  86.   if (SpeedDecX || sparksSpeed[0][id]) {
  87.     if (sparksSpeed[0][id] > 0)
  88.       sparksSpeed[0][id] -= SpeedDecX;
  89.     else
  90.       sparksSpeed[0][id] += SpeedDecX;
  91.     if (abs(sparksSpeed[0][id]) <= SpeedDecX)
  92.       sparksSpeed[0][id] = 0;
  93.   }
  94.   if (SpeedDecY || sparksSpeed[1][id]) {
  95.     if (sparksSpeed[1][id] > 0)
  96.       sparksSpeed[1][id] -= SpeedDecY;
  97.     else
  98.       sparksSpeed[1][id] += SpeedDecY;
  99.     if (abs(sparksSpeed[1][id]) <= SpeedDecY)
  100.       sparksSpeed[1][id] = 0;
  101.   }
  102.   if (Board) {
  103.     if (sparksPos[0][id] < 0 || sparksPos[0][id] >= WIDTH * 10) sparksSpeed[0][id] = -sparksSpeed[0][id];
  104.     if (sparksPos[1][id] < 0) sparksSpeed[1][id] = -sparksSpeed[1][id];
  105.   }
  106.   sparksPos[0][id] += constrain(sparksSpeed[0][id], -10, 10);
  107.   sparksPos[1][id] += constrain(sparksSpeed[1][id], -10, 10);
  108. }

  109. void wu_pixel(uint32_t x, uint32_t y, CRGB * col) { //awesome wu_pixel procedure by reddit u/sutaburosu
  110.   // extract the fractional parts and derive their inverses
  111.   uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy;
  112.   // calculate the intensities for each affected pixel
  113. #define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
  114.   uint8_t wu[4] = {
  115.     WU_WEIGHT(ix, iy),
  116.     WU_WEIGHT(xx, iy),
  117.     WU_WEIGHT(ix, yy),
  118.     WU_WEIGHT(xx, yy)
  119.   };
  120.   // multiply the intensities by the colour, and saturating-add them to the pixels
  121.   for (uint8_t i = 0; i < 4; i++) {
  122.     uint16_t xy = XY((x >> 8) + (i & 1), (y >> 8) + ((i >> 1) & 1));
  123.     leds[xy].r = qadd8(leds[xy].r, col -> r * wu[i] >> 8);
  124.     leds[xy].g = qadd8(leds[xy].g, col -> g * wu[i] >> 8);
  125.     leds[xy].b = qadd8(leds[xy].b, col -> b * wu[i] >> 8);
  126.   }
  127. }

  128. void render(byte id, CRGB Col) {
  129.   if (loadingFlag) {
  130.     for (byte i = 0; i < SPARKS_AM; i++) {
  131.       reg(i);
  132.       for (byte a = 0; a < i; a++) {
  133.         physics(a);
  134.       }
  135.     }
  136.     loadingFlag = false;
  137.   }
  138.   physics(id);
  139.   if (sparksPos[1][id] < ((HEIGHT - 1) * 10) and sparksPos[1][id] >= 0)
  140.     if (sparksPos[0][id] < ((WIDTH - 1) * 10) and sparksPos[0][id] >= 0) {
  141.       CRGB color = Col;
  142.       wu_pixel(sparksPos[0][id] * 25.6, sparksPos[1][id] * 25.6, & color);
  143.     }
  144. }

  145. void setGenPos(int x, int y) {
  146.   genPos[0] = x;
  147.   genPos[1] = y;
  148. }

  149. void setGravityPos(int x, int y) {
  150.   gravityPos[0] = x;
  151.   gravityPos[1] = y;
  152. }

  153. void setRegenRule(byte id, bool b) {
  154.   if (b) reg(id);
  155. }

  156. void draw() {
  157.   fadeToBlackBy(leds, NUM_LEDS, 20);
  158.   setGenPos(beatsin16(10, 0, WIDTH * 10), beatsin16(10, 0, HEIGHT * 10, 0, 16384));
  159.   setGravityPos(0, 0);
  160.   for (byte i = 0; i < SPARKS_AM; i++) {
  161.     setRegenRule(i, (sparksFade[i] <= 35) ? 1 : 0);
  162.     render(i, CHSV(sparksColor[i], 255, constrain(sparksFade[i], 32, 255)));
  163.   }
  164.   delay(16);
  165. }

  166. void loop() {

  167.   draw();
  168.   LEDS.show();
  169. }

  170. uint16_t XY( uint8_t x, uint8_t y)
  171. {
  172.   uint16_t i;
  173.   if ( kMatrixSerpentineLayout == false) {
  174.     i = (y * kMatrixWidth) + x;
  175.   }
  176.   if ( kMatrixSerpentineLayout == true) {
  177.     if ( y & 0x01) {
  178.       // Odd rows run backwards
  179.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  180.       i = (y * kMatrixWidth) + reverseX;
  181.     } else {
  182.       // Even rows run forwards
  183.       i = (y * kMatrixWidth) + x;
  184.     }
  185.   }
  186.   return i;
  187. }
复制代码



作者: eagler8    时间: 2022-7-18 13:35
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十五:绿色火焰

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   项目四十五:绿色火焰
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  30
  9. #define LED_TYPE    WS2812B
  10. #define COLOR_ORDER GRB
  11. const uint8_t kMatrixWidth  = 8;
  12. const uint8_t kMatrixHeight = 8;
  13. #define WIDTH kMatrixWidth
  14. #define HEIGHT kMatrixHeight
  15. #define ROWS kMatrixWidth
  16. #define COLS kMatrixHeight
  17. CRGB leds[kMatrixWidth * kMatrixHeight];
  18. const bool    kMatrixSerpentineLayout = true;
  19. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  20. #define enlargedOBJECT_MAX_COUNT WIDTH*2
  21. #define SPEED_ADJ (float)NUM_LEDS/512

  22. //speed control
  23. byte speed = 127; // 1-255

  24. //scale control
  25. byte scale = 150; //1-255

  26. //control magma bursts
  27. const byte deltaValue = 6U;
  28. const byte deltaHue = 8U;

  29. void setup()
  30. {
  31.   delay(3000);
  32.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  33.   LEDS.setBrightness(BRIGHTNESS);  

  34. }
  35. static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};                     
  36. extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};   
  37. extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};                    
  38. extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};            
  39. extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};
  40. extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};   
  41. extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};
  42. extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
  43. extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};  
  44. extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink};


  45. static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max){
  46.     return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
  47. }

  48. float randomf(float min, float max)
  49. {
  50.   return fmap(random(1024), 0, 1023, min, max);
  51. }

  52. void drawPixelXYF(float x, float y, CRGB color)
  53. {
  54.   if (x < 0 || y < 0 || x > ((float)WIDTH - 1) || y > ((float)HEIGHT - 1)) return;
  55.   uint8_t xx = (x - (int)x) * 255, yy = (y - (int)y) * 255, ix = 255 - xx, iy = 255 - yy;
  56.   // calculate the intensities for each affected pixel
  57.   #define WU_WEIGHT(a,b) ((uint8_t) (((a)*(b)+(a)+(b))>>8))
  58.   uint8_t wu[4] = {WU_WEIGHT(ix, iy), WU_WEIGHT(xx, iy),
  59.                    WU_WEIGHT(ix, yy), WU_WEIGHT(xx, yy)};
  60.   // multiply the intensities by the colour, and saturating-add them to the pixels
  61.   for (uint8_t i = 0; i < 4; i++) {
  62.     int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);
  63.     CRGB clr = leds[XY(xn, yn)];
  64.     clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);
  65.     clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);
  66.     clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);
  67.     leds[XY(xn, yn)] = clr;
  68.   }
  69. }


  70. // (c) Сотнег (SottNick) 2021
  71. class EffectMagma {
  72. private:   
  73.     float ff_y, ff_z;        
  74.     uint8_t step, ObjectNUM = WIDTH;
  75.     uint8_t shiftHue[HEIGHT];
  76.     float trackingObjectPosX[enlargedOBJECT_MAX_COUNT];
  77.     float trackingObjectPosY[enlargedOBJECT_MAX_COUNT];
  78.     uint8_t trackingObjectHue[enlargedOBJECT_MAX_COUNT];
  79.     float trackingObjectSpeedX[enlargedOBJECT_MAX_COUNT];
  80.     float trackingObjectShift[enlargedOBJECT_MAX_COUNT];
  81.     float speedfactor;

  82.     void regen();
  83.     void LeapersMove_leaper(uint8_t l);
  84.     void LeapersRestart_leaper(uint8_t l);

  85. public:
  86.     void load();
  87.     bool run();
  88. };


  89. void EffectMagma::load() {
  90.   speedfactor = fmap(speed, 1, 255, SPEED_ADJ*0.1, SPEED_ADJ);
  91.   ObjectNUM = map(scale, 1, 255, WIDTH, enlargedOBJECT_MAX_COUNT);
  92.   regen();
  93. }


  94. void EffectMagma::regen() {
  95.   for (uint8_t j = 0; j < HEIGHT; j++) {
  96.     shiftHue[j] = map(j, 0, HEIGHT+HEIGHT/4, 255, 0); // init colorfade table
  97.   }


  98.   for (uint8_t i = 0 ; i < enlargedOBJECT_MAX_COUNT ; i++) {
  99.     LeapersRestart_leaper(i);  
  100.     trackingObjectHue[i] = 50U;
  101.   }
  102. }
  103. bool EffectMagma::run() {
  104.   fadeToBlackBy(leds, NUM_LEDS, 25);

  105.   for (uint8_t i = 0; i < ObjectNUM; i++) {
  106.     LeapersMove_leaper(i);
  107.     drawPixelXYF(trackingObjectPosX[i], trackingObjectPosY[i], ColorFromPalette(CopperFireColors_p, trackingObjectHue[i], 255));
  108.   }

  109.   for (uint8_t i = 0; i < WIDTH; i++) {
  110.     for (uint8_t j = 0; j < HEIGHT; j++) {
  111.       leds[XY(i, HEIGHT-1 - j)] += ColorFromPalette(RubidiumFireColors_p, qsub8(inoise8(i * deltaValue, (j + ff_y + random8(2)) * deltaHue, ff_z), shiftHue[j]), 255U);
  112.     }
  113.   }

  114.   ff_y += speedfactor * 2;
  115.   ff_z += speedfactor;
  116.   blur2d(leds, WIDTH, HEIGHT,4 );
  117.   return true;
  118. }

  119. void EffectMagma::LeapersMove_leaper(uint8_t l) {
  120. #define GRAVITY            0.1
  121.   trackingObjectShift[l] -= GRAVITY * speedfactor;

  122.   trackingObjectPosX[l] += trackingObjectSpeedX[l] * speedfactor;
  123.   trackingObjectPosY[l] += trackingObjectShift[l] * speedfactor;

  124.   // bounce off the ceiling?
  125.   if (trackingObjectPosY[l] > HEIGHT + HEIGHT/4) {
  126.     trackingObjectShift[l] = -trackingObjectShift[l];
  127.   }
  128.   
  129.   // settled on the floor?
  130.   if (trackingObjectPosY[l] <= (HEIGHT/8-1)) {
  131.     LeapersRestart_leaper(l);
  132.   }

  133.   // bounce off the sides of the screen?
  134.   if (trackingObjectPosX[l] < 0 || trackingObjectPosX[l] > WIDTH-1) {
  135.     LeapersRestart_leaper(l);
  136.   }

  137. }

  138. void EffectMagma::LeapersRestart_leaper(uint8_t l) {
  139.   randomSeed(millis());
  140.   // leap up and to the side with some random component
  141.   trackingObjectSpeedX[l] = randomf(-0.75, 0.75);
  142.   trackingObjectShift[l] = randomf(0.50, 0.85);
  143.   trackingObjectPosX[l] = randomf(0, WIDTH);
  144.   trackingObjectPosY[l] = randomf(0, (float)HEIGHT/4-1);

  145.   // for variety, sometimes go 100% faster
  146.   if (random8() < 12) {
  147.     trackingObjectShift[l] += trackingObjectShift[l] * 2;
  148.   }

  149. }

  150. bool load = true;
  151. EffectMagma eff;


  152. void draw() {
  153.   if (load) {
  154.     eff.load();
  155.     load = false;
  156.   }
  157.   eff.run();
  158.   //FastLED.delay(16);
  159. }

  160. void loop(){

  161. draw();
  162. LEDS.show();
  163. }
  164. uint16_t XY(uint8_t x, uint8_t y)
  165. {
  166.   uint16_t i;
  167.   if (kMatrixSerpentineLayout == false)
  168.   {
  169.     i = (y * kMatrixWidth) + x;
  170.   }
  171.   if (kMatrixSerpentineLayout == true)
  172.   {
  173.     if (y & 0x01)
  174.     {
  175.       // Odd rows run backwards
  176.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  177.       i = (y * kMatrixWidth) + reverseX;
  178.     }
  179.     else
  180.     {
  181.       // Even rows run forwards
  182.       i = (y * kMatrixWidth) + x;
  183.     }
  184.   }
  185.   return i;
  186. }
复制代码



作者: eagler8    时间: 2022-7-18 13:47
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十六:多彩向日葵

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   项目四十六:多彩向日葵
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  30
  9. #define LED_TYPE    WS2812B
  10. #define COLOR_ORDER GRB
  11. const uint8_t kMatrixWidth  = 8;
  12. const uint8_t kMatrixHeight = 8;
  13. #define WIDTH kMatrixWidth
  14. #define HEIGHT kMatrixHeight
  15. #define ROWS kMatrixWidth
  16. #define COLS kMatrixHeight
  17. #define LED_ROWS kMatrixWidth
  18. #define LED_COLS kMatrixHeight
  19. CRGB leds[kMatrixWidth * kMatrixHeight];
  20. const bool    kMatrixSerpentineLayout = true;
  21. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  22. #define enlargedOBJECT_MAX_COUNT WIDTH*2
  23. #define SPEED_ADJ (float)NUM_LEDS/512

  24. //speed control
  25. byte speed = 127; // 1-255

  26. //scale control
  27. byte scale = 150; //1-255
  28. const byte deltaValue = 6U;
  29. const byte deltaHue = 8U;

  30. void setup()
  31. {
  32.   delay(3000);
  33.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  34.   LEDS.setBrightness(BRIGHTNESS);  

  35. }
  36. static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};                     
  37. extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};   
  38. extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};                    
  39. extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};                  
  40. extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};   
  41. extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};      
  42. extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};   
  43. extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
  44. extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};
  45. extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink};


  46. static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max){
  47.     return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
  48. }

  49. #define CenterX ((LED_COLS / 2) - 0.5)
  50. #define CenterY ((LED_ROWS / 2) - 0.5)
  51. const byte maxDim = max(LED_COLS, LED_ROWS);
  52. byte effect= 1;
  53. bool change = true;

  54. void drawPixelXYF(float x, float y,
  55.   const CRGB & color) {
  56.   // extract the fractional parts and derive their inverses
  57.   uint8_t xx = (x - (int) x) * 255, yy = (y - (int) y) * 255, ix = 255 - xx, iy = 255 - yy;
  58.   // calculate the intensities for each affected pixel
  59.   #define WU_WEIGHT(a, b)((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
  60.   uint8_t wu[4] = {
  61.     WU_WEIGHT(ix, iy),
  62.     WU_WEIGHT(xx, iy),
  63.     WU_WEIGHT(ix, yy),
  64.     WU_WEIGHT(xx, yy)
  65.   };
  66.   // multiply the intensities by the colour, and saturating-add them to the pixels
  67.   for (uint8_t i = 0; i < 4; i++) {
  68.     int16_t xn = x + (i & 1), yn = y + ((i >> 1) & 1);
  69.     CRGB clr = leds[XY(xn, yn)];
  70.     clr.r = qadd8(clr.r, (color.r * wu[i]) >> 8);
  71.     clr.g = qadd8(clr.g, (color.g * wu[i]) >> 8);
  72.     clr.b = qadd8(clr.b, (color.b * wu[i]) >> 8);
  73.     leds[XY(xn, yn)] = clr;
  74.   }
  75.   #undef WU_WEIGHT
  76. }

  77. void draw() {
  78.   FastLED.clear(); //fadeToBlackBy(leds, NUM_LEDS, 16);
  79.   unsigned long t = millis()/20;
  80.   for(float i = 1; i <maxDim / 2; i+=0.25) {
  81.     double angle = radians(t * (maxDim/2-i));
  82.     drawPixelXYF(CenterX + sin(angle) * i, CenterY + cos(angle) * i, ColorFromPalette(PartyColors_p, (i * 20) + (t / 20)));
  83.     switch(effect){
  84.     case 1: case 3: drawPixelXYF(CenterX + cos(angle) * i, CenterY + sin(angle) * i, ColorFromPalette(PartyColors_p, (i * 20) + (t / 20)));break;
  85.   }}
  86.   switch(effect){
  87.     case 2: case 3: blur2d(leds, LED_COLS, LED_ROWS, 16); break;
  88.   }
  89.   delay(16);
  90. }

  91. void loop(){

  92. draw();
  93. LEDS.show();
  94. }
  95. uint16_t XY( uint8_t x, uint8_t y)
  96. {
  97.   uint16_t i;
  98.   if( kMatrixSerpentineLayout == false) {
  99.     i = (y * kMatrixWidth) + x;
  100.   }
  101.   if( kMatrixSerpentineLayout == true) {
  102.     if( y & 0x01) {
  103.       // Odd rows run backwards
  104.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  105.       i = (y * kMatrixWidth) + reverseX;
  106.     } else {
  107.       // Even rows run forwards
  108.       i = (y * kMatrixWidth) + x;
  109.     }
  110.   }
  111.   return i;
  112. }
复制代码



作者: eagler8    时间: 2022-7-18 13:52
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十七:多彩沙漏

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   项目四十七:多彩沙漏
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  30
  9. #define LED_TYPE    WS2812B
  10. #define COLOR_ORDER GRB
  11. const uint8_t kMatrixWidth  = 8;
  12. const uint8_t kMatrixHeight = 8;
  13. #define WIDTH kMatrixWidth
  14. #define HEIGHT kMatrixHeight
  15. #define ROWS kMatrixWidth
  16. #define COLS kMatrixHeight
  17. #define LED_ROWS kMatrixWidth
  18. #define LED_COLS kMatrixHeight
  19. const bool    kMatrixSerpentineLayout = true;
  20. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  21. #define N_LEDS NUM_LEDS
  22. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  23. CRGB leds[kMatrixWidth * kMatrixHeight];
  24. uint16_t offset = 0;
  25. void setup()

  26. {
  27.   delay(3000);
  28.   LEDS.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds,NUM_LEDS);
  29.   LEDS.setBrightness(BRIGHTNESS);  
  30. }


  31. void randomdot() {
  32.   offset++;
  33.   //byte a= LED_COLS/2; //
  34.   byte a = random8(LED_COLS / 4) + LED_COLS * 3 / 8; //
  35.   if (!random8(4)) leds[XY(a, LED_ROWS - 1)].setHue(offset * 15); // 0 or 1
  36.   if (offset > 250) {
  37.     offset = 10;
  38.   }
  39.       
  40. }

  41. void updatesand() {
  42.   int index, indexXadd1Y, indexXsub1Y, indexXYadd1;
  43.   for (int y = 0; y < LED_ROWS - 1; y++) {
  44.     for (int x = 1; x < LED_COLS - 1; x++) {
  45.       index = XY(x, y);
  46.       indexXadd1Y = XY(x + 1, y);
  47.       indexXsub1Y = XY(x - 1, y);
  48.       indexXYadd1 = XY(x, y + 1);
  49.       if (!leds[index] && !leds[indexXYadd1]) continue;
  50.       if (!leds[index] && leds[indexXYadd1]) {
  51.         leds[index] = leds[indexXYadd1];
  52.         leds[indexXYadd1] = 0;
  53.       }
  54.       if (leds[index] && leds[indexXYadd1] && !leds[indexXsub1Y] && !leds[indexXadd1Y]) {
  55.         if (random8(4)) {
  56.           leds[indexXsub1Y] = leds[indexXYadd1];
  57.           leds[indexXYadd1] = 0;
  58.       } else {
  59.           leds[indexXadd1Y] = leds[indexXYadd1];
  60.           leds[indexXYadd1] = 0;
  61.         }
  62.       }
  63.       if (leds[index] && leds[indexXYadd1] && !leds[indexXsub1Y] && leds[indexXadd1Y]) {
  64.         leds[indexXsub1Y] = leds[indexXYadd1];
  65.         leds[indexXYadd1] = 0;
  66.       }
  67.       if (leds[index] && leds[indexXYadd1] && leds[indexXsub1Y] && !leds[indexXadd1Y]) {
  68.         leds[indexXadd1Y] = leds[indexXYadd1];
  69.         leds[indexXYadd1] = 0;
  70.       }
  71.     }
  72.   }
  73. }

  74. void randomdel() {
  75.   for (int i = 0; i < N_LEDS; i++) {
  76.     if (!random8(20)) leds[i] = 0;
  77.   }
  78.   //leds[XY(0, 0)] = 0;
  79. }

  80. void falldown() {
  81.   for (int y = 0; y < LED_ROWS - 1; y++) {
  82.     for (int x = 0; x < LED_COLS; x++) {
  83.       if (!leds[XY(x, y)] && leds[XY(x, y + 1)]) {
  84.         leds[XY(x, y)] = leds[XY(x, y + 1)];
  85.         leds[XY(x, y + 1)] = 0;
  86.       }
  87.     }
  88.   }
  89. }

  90. void draw() {
  91.   EVERY_N_MILLISECONDS(1) {
  92.     updatesand();
  93.     randomdot();
  94.   }
  95.   // Level controled by LED_ROWS/3   
  96.   if ((uint32_t) leds[XY(0, LED_ROWS / 3)] > 0) {
  97.     EVERY_N_MILLISECONDS(2000) {
  98.       randomdel();
  99.       falldown();
  100.       falldown();
  101.       falldown();
  102.     }
  103.   }
  104. }
  105. void loop() {

  106.   draw();
  107.   LEDS.show();

  108. }
  109. uint16_t XY(uint8_t x, uint8_t y)
  110. {
  111.   uint16_t i;
  112.   if (kMatrixSerpentineLayout == false)
  113.   {
  114.     i = (y * kMatrixWidth) + x;
  115.   }
  116.   if (kMatrixSerpentineLayout == true)
  117.   {
  118.     if (y & 0x01)
  119.     {
  120.       // Odd rows run backwards
  121.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  122.       i = (y * kMatrixWidth) + reverseX;
  123.     }
  124.     else
  125.     {
  126.       // Even rows run forwards
  127.       i = (y * kMatrixWidth) + x;
  128.     }
  129.   }
  130.   return i;
  131. }
复制代码



作者: eagler8    时间: 2022-7-18 14:00
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  项目四十八:鎏金岁月

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验一百四十六:64位WS2812B 8 * 8 xRGB 5050 LED模块 ws2812s像素点阵屏
  4.   项目四十八:鎏金岁月
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  30
  9. #define LED_TYPE    WS2812B
  10. #define COLOR_ORDER GRB
  11. const uint8_t kMatrixWidth  = 8;
  12. const uint8_t kMatrixHeight = 8;
  13. #define WIDTH kMatrixWidth
  14. #define HEIGHT kMatrixHeight
  15. #define ROWS kMatrixWidth
  16. #define COLS kMatrixHeight
  17. #define LED_ROWS kMatrixWidth
  18. #define LED_COLS kMatrixHeight
  19. CRGB leds[kMatrixWidth * kMatrixHeight];
  20. const bool kMatrixSerpentineLayout = true;
  21. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  22. #define enlargedOBJECT_MAX_COUNT WIDTH * 2
  23. #define SPEED_ADJ (float)NUM_LEDS / 512

  24. //speed control
  25. byte speed = 127; // 1-255

  26. //scale control
  27. byte scale = 150; //1-255

  28. void setup()
  29. {
  30.   delay(3000);
  31.   LEDS.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  32.   LEDS.setBrightness(BRIGHTNESS);
  33. }
  34. static const TProgmemRGBPalette16 MagmaColor_p FL_PROGMEM = {CRGB::Black, 0x240000, 0x480000, 0x660000, 0x9a1100, 0xc32500, 0xd12a00, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400, 0xffffff};
  35. extern const TProgmemRGBPalette16 WoodFireColors_p FL_PROGMEM = {CRGB::Black, 0x330e00, 0x661c00, 0x992900, 0xcc3700, CRGB::OrangeRed, 0xff5800, 0xff6b00, 0xff7f00, 0xff9200, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold};
  36. extern const TProgmemRGBPalette16 NormalFire_p FL_PROGMEM = {CRGB::Black, 0x330000, 0x660000, 0x990000, 0xcc0000, CRGB::Red, 0xff0c00, 0xff1800, 0xff2400, 0xff3000, 0xff3c00, 0xff4800, 0xff5400, 0xff6000, 0xff6c00, 0xff7800};
  37. extern const TProgmemRGBPalette16 NormalFire2_p FL_PROGMEM = {CRGB::Black, 0x560000, 0x6b0000, 0x820000, 0x9a0011, CRGB::FireBrick, 0xc22520, 0xd12a1c, 0xe12f17, 0xf0350f, 0xff3c00, 0xff6400, 0xff8300, 0xffa000, 0xffba00, 0xffd400};
  38. extern const TProgmemRGBPalette16 LithiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x240707, 0x470e0e, 0x6b1414, 0x8e1b1b, CRGB::FireBrick, 0xc14244, 0xd16166, 0xe08187, 0xf0a0a9, CRGB::Pink, 0xff9ec0, 0xff7bb5, 0xff59a9, 0xff369e, CRGB::DeepPink};
  39. extern const TProgmemRGBPalette16 SodiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x332100, 0x664200, 0x996300, 0xcc8400, CRGB::Orange, 0xffaf00, 0xffb900, 0xffc300, 0xffcd00, CRGB::Gold, 0xf8cd06, 0xf0c30d, 0xe9b913, 0xe1af1a, CRGB::Goldenrod};
  40. extern const TProgmemRGBPalette16 CopperFireColors_p FL_PROGMEM = {CRGB::Black, 0x001a00, 0x003300, 0x004d00, 0x006600, CRGB::Green, 0x239909, 0x45b313, 0x68cc1c, 0x8ae626, CRGB::GreenYellow, 0x94f530, 0x7ceb30, 0x63e131, 0x4bd731, CRGB::LimeGreen};
  41. extern const TProgmemRGBPalette16 AlcoholFireColors_p FL_PROGMEM = {CRGB::Black, 0x000033, 0x000066, 0x000099, 0x0000cc, CRGB::Blue, 0x0026ff, 0x004cff, 0x0073ff, 0x0099ff, CRGB::DeepSkyBlue, 0x1bc2fe, 0x36c5fd, 0x51c8fc, 0x6ccbfb, CRGB::LightSkyBlue};
  42. extern const TProgmemRGBPalette16 RubidiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, CRGB::Indigo, 0x3c0084, 0x2d0086, 0x1e0087, 0x0f0089, CRGB::DarkBlue};
  43. extern const TProgmemRGBPalette16 PotassiumFireColors_p FL_PROGMEM = {CRGB::Black, 0x0f001a, 0x1e0034, 0x2d004e, 0x3c0068, CRGB::Indigo, 0x591694, 0x682da6, 0x7643b7, 0x855ac9, CRGB::MediumPurple, 0xa95ecd, 0xbe4bbe, 0xd439b0, 0xe926a1, CRGB::DeepPink};

  44. static double fmap(const double x, const double in_min, const double in_max, const double out_min, const double out_max)
  45. {
  46.   return (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
  47. }

  48. const uint8_t exp_gamma[256] PROGMEM = {
  49. 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   1,   1,   1,
  50. 1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,   1,
  51. 1,   2,   2,   2,   2,   2,   2,   2,   2,   2,   3,   3,   3,   3,   3,
  52. 4,   4,   4,   4,   4,   5,   5,   5,   5,   5,   6,   6,   6,   7,   7,
  53. 7,   7,   8,   8,   8,   9,   9,   9,   10,  10,  10,  11,  11,  12,  12,
  54. 12,  13,  13,  14,  14,  14,  15,  15,  16,  16,  17,  17,  18,  18,  19,
  55. 19,  20,  20,  21,  21,  22,  23,  23,  24,  24,  25,  26,  26,  27,  28,
  56. 28,  29,  30,  30,  31,  32,  32,  33,  34,  35,  35,  36,  37,  38,  39,
  57. 39,  40,  41,  42,  43,  44,  44,  45,  46,  47,  48,  49,  50,  51,  52,
  58. 53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,  66,  67,
  59. 68,  70,  71,  72,  73,  74,  75,  77,  78,  79,  80,  82,  83,  84,  85,
  60. 87,  89,  91,  92,  93,  95,  96,  98,  99,  100, 101, 102, 105, 106, 108,
  61. 109, 111, 112, 114, 115, 117, 118, 120, 121, 123, 125, 126, 128, 130, 131,
  62. 133, 135, 136, 138, 140, 142, 143, 145, 147, 149, 151, 152, 154, 156, 158,
  63. 160, 162, 164, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187,
  64. 190, 192, 194, 196, 198, 200, 202, 204, 207, 209, 211, 213, 216, 218, 220,
  65. 222, 225, 227, 229, 232, 234, 236, 239, 241, 244, 246, 249, 251, 253, 254, 255};

  66. const uint8_t cos_wave[256] = {
  67.     0,   0,   0,   0,   1,   1,   1,   2,   2,   3,   4,   5,   6,   6,   8,
  68.     9,   10,  11,  12,  14,  15,  17,  18,  20,  22,  23,  25,  27,  29,  31,
  69.     33,  35,  38,  40,  42,  45,  47,  49,  52,  54,  57,  60,  62,  65,  68,
  70.     71,  73,  76,  79,  82,  85,  88,  91,  94,  97,  100, 103, 106, 109, 113,
  71.     116, 119, 122, 125, 128, 131, 135, 138, 141, 144, 147, 150, 153, 156, 159,
  72.     162, 165, 168, 171, 174, 177, 180, 183, 186, 189, 191, 194, 197, 199, 202,
  73.     204, 207, 209, 212, 214, 216, 218, 221, 223, 225, 227, 229, 231, 232, 234,
  74.     236, 238, 239, 241, 242, 243, 245, 246, 247, 248, 249, 250, 251, 252, 252,
  75.     253, 253, 254, 254, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 253,
  76.     253, 252, 252, 251, 250, 249, 248, 247, 246, 245, 243, 242, 241, 239, 238,
  77.     236, 234, 232, 231, 229, 227, 225, 223, 221, 218, 216, 214, 212, 209, 207,
  78.     204, 202, 199, 197, 194, 191, 189, 186, 183, 180, 177, 174, 171, 168, 165,
  79.     162, 159, 156, 153, 150, 147, 144, 141, 138, 135, 131, 128, 125, 122, 119,
  80.     116, 113, 109, 106, 103, 100, 97,  94,  91,  88,  85,  82,  79,  76,  73,
  81.     71,  68,  65,  62,  60,  57,  54,  52,  49,  47,  45,  42,  40,  38,  35,
  82.     33,  31,  29,  27,  25,  23,  22,  20,  18,  17,  15,  14,  12,  11,  10,
  83.     9,   8,   6,   6,   5,   4,   3,   2,   2,   1,   1,   1,   0,   0,   0, 0};

  84. void GammaCorrection(){   //gamma correction function
  85. byte r,g,b;
  86. for (uint16_t i=0; i<NUM_LEDS; i++){
  87. r=leds[i].r;
  88. g=leds[i].g;
  89. b=leds[i].b;
  90. leds[i].r = pgm_read_byte(exp_gamma + r);
  91. leds[i].g = pgm_read_byte(exp_gamma + g);
  92. leds[i].b = pgm_read_byte(exp_gamma + b);
  93. }
  94. }

  95. void draw() {
  96. byte speed = 5;

  97.   uint8_t w = 2;
  98.   uint8_t scale = 4;

  99.   uint16_t a=millis()/32;
  100.   uint16_t a2=a/2;
  101.   uint16_t a3=a/3;

  102.   uint16_t cx =  beatsin8 (10-speed,0,COLS)*scale;
  103.   uint16_t cy =  beatsin8 (12-speed,0,ROWS)*scale;
  104.   uint16_t cx1 = beatsin8 (13-speed,0,COLS)*scale;
  105.   uint16_t cy1 = beatsin8 (15-speed,0,ROWS)*scale;
  106.   uint16_t cx2 = beatsin8 (17-speed,0,COLS)*scale;
  107.   uint16_t cy2 = beatsin8 (14-speed,0,ROWS)*scale;
  108.   
  109.   uint16_t xoffs=0;

  110.   for (int x = 0; x < COLS; x++) {

  111.      xoffs += scale;
  112.      uint16_t yoffs = 0;

  113.     for (int y = 0; y < ROWS; y++) {

  114.        yoffs += scale;

  115.       // byte rdistort = cos_wave [((x+y)*8+a2)&255]>>1;
  116.       // byte gdistort = cos_wave [((x+y)*8+a3+32)&255]>>1;
  117.       // byte bdistort = cos_wave [((x+y)*8+a+64)&255]>>1;

  118.       byte rdistort = cos_wave [(cos_wave[((x<<3)+a )&255]+cos_wave[((y<<3)-a2)&255]+a3   )&255]>>1;
  119.       byte gdistort = cos_wave [(cos_wave[((x<<3)-a2)&255]+cos_wave[((y<<3)+a3)&255]+a+32 )&255]>>1;
  120.       byte bdistort = cos_wave [(cos_wave[((x<<3)+a3)&255]+cos_wave[((y<<3)-a) &255]+a2+64)&255]>>1;

  121.       byte valueR = rdistort+ w*  (a- ( ((xoffs - cx) *  (xoffs - cx) +  (yoffs - cy) *   (yoffs - cy))>>7  ));
  122.       byte valueG = gdistort+ w*  (a2-( ((xoffs - cx1) * (xoffs - cx1) + (yoffs - cy1) *  (yoffs - cy1))>>7 ));
  123.       byte valueB = bdistort+ w*  (a3-( ((xoffs - cx2) * (xoffs - cx2) + (yoffs - cy2) *  (yoffs - cy2))>>7 ));

  124.       valueR = cos_wave [(valueR)];
  125.       valueG = cos_wave [(valueG)];
  126.       valueB = cos_wave [(valueB)];

  127.       uint16_t index = XY(x, y);
  128.       leds[index].setRGB (valueR,valueG,valueB);
  129.     }
  130.   }
  131.   
  132.   GammaCorrection();
  133. }
  134. void loop(){

  135. draw();
  136. LEDS.show();
  137. }

  138. uint16_t XY( uint8_t x, uint8_t y)
  139. {
  140.   uint16_t i;
  141.   if( kMatrixSerpentineLayout == false) {
  142.     i = (y * kMatrixWidth) + x;
  143.   }
  144.   if( kMatrixSerpentineLayout == true) {
  145.     if( y & 0x01) {
  146.       // Odd rows run backwards
  147.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  148.       i = (y * kMatrixWidth) + reverseX;
  149.     } else {
  150.       // Even rows run forwards
  151.       i = (y * kMatrixWidth) + x;
  152.     }
  153.   }
  154.   return i;
  155. }
复制代码



作者: wfx777888    时间: 2024-7-27 05:49
谢谢分享资料学习




欢迎光临 数码之家 (https://www.mydigit.cn/) Powered by Discuz! X3.4