楼主: eagler8

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

[复制链接]

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 06:35 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十九:带有可编程调色板的 Fire2012 火灾模拟

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目十九:带有可编程调色板的 Fire2012 火灾模拟
  5. */

  6. #include <FastLED.h>

  7. #define LED_PIN     5
  8. #define COLOR_ORDER GRB
  9. #define CHIPSET     WS2811
  10. #define NUM_LEDS    8

  11. #define BRIGHTNESS  32
  12. #define FRAMES_PER_SECOND 60

  13. bool gReverseDirection = false;

  14. CRGB leds[NUM_LEDS];

  15. // Fire2012 with programmable Color Palette
  16. //
  17. // This code is the same fire simulation as the original "Fire2012",
  18. // but each heat cell's temperature is translated to color through a FastLED
  19. // programmable color palette, instead of through the "HeatColor(...)" function.
  20. //
  21. // Four different static color palettes are provided here, plus one dynamic one.
  22. //
  23. // The three static ones are:
  24. //   1. the FastLED built-in HeatColors_p -- this is the default, and it looks
  25. //      pretty much exactly like the original Fire2012.
  26. //
  27. //  To use any of the other palettes below, just "uncomment" the corresponding code.
  28. //
  29. //   2. a gradient from black to red to yellow to white, which is
  30. //      visually similar to the HeatColors_p, and helps to illustrate
  31. //      what the 'heat colors' palette is actually doing,
  32. //   3. a similar gradient, but in blue colors rather than red ones,
  33. //      i.e. from black to blue to aqua to white, which results in
  34. //      an "icy blue" fire effect,
  35. //   4. a simplified three-step gradient, from black to red to white, just to show
  36. //      that these gradients need not have four components; two or
  37. //      three are possible, too, even if they don't look quite as nice for fire.
  38. //
  39. // The dynamic palette shows how you can change the basic 'hue' of the
  40. // color palette every time through the loop, producing "rainbow fire".

  41. CRGBPalette16 gPal;

  42. void setup() {
  43.   delay(3000); // sanity delay
  44.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  45.   FastLED.setBrightness( BRIGHTNESS );

  46.   // This first palette is the basic 'black body radiation' colors,
  47.   // which run from black to red to bright yellow to white.
  48.   gPal = HeatColors_p;
  49.   
  50.   // These are other ways to set up the color palette for the 'fire'.
  51.   // First, a gradient from black to red to yellow to white -- similar to HeatColors_p
  52.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::Yellow, CRGB::White);
  53.   
  54.   // Second, this palette is like the heat colors, but blue/aqua instead of red/yellow
  55.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Blue, CRGB::Aqua,  CRGB::White);
  56.   
  57.   // Third, here's a simpler, three-step gradient, from black to red to white
  58.   //   gPal = CRGBPalette16( CRGB::Black, CRGB::Red, CRGB::White);

  59. }

  60. void loop()
  61. {
  62.   // Add entropy to random number generator; we use a lot of it.
  63.   random16_add_entropy( random());

  64.   // Fourth, the most sophisticated: this one sets up a new palette every
  65.   // time through the loop, based on a hue that changes every time.
  66.   // The palette is a gradient from black, to a dark color based on the hue,
  67.   // to a light color based on the hue, to white.
  68.   //
  69.   //   static uint8_t hue = 0;
  70.   //   hue++;
  71.   //   CRGB darkcolor  = CHSV(hue,255,192); // pure hue, three-quarters brightness
  72.   //   CRGB lightcolor = CHSV(hue,128,255); // half 'whitened', full brightness
  73.   //   gPal = CRGBPalette16( CRGB::Black, darkcolor, lightcolor, CRGB::White);


  74.   Fire2012WithPalette(); // run simulation frame, using palette colors
  75.   
  76.   FastLED.show(); // display this frame
  77.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  78. }


  79. // Fire2012 by Mark Kriegsman, July 2012
  80. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  81. ////
  82. // This basic one-dimensional 'fire' simulation works roughly as follows:
  83. // There's a underlying array of 'heat' cells, that model the temperature
  84. // at each point along the line.  Every cycle through the simulation,
  85. // four steps are performed:
  86. //  1) All cells cool down a little bit, losing heat to the air
  87. //  2) The heat from each cell drifts 'up' and diffuses a little
  88. //  3) Sometimes randomly new 'sparks' of heat are added at the bottom
  89. //  4) The heat from each cell is rendered as a color into the leds array
  90. //     The heat-to-color mapping uses a black-body radiation approximation.
  91. //
  92. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  93. //
  94. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  95. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  96. //
  97. // I recommend running this simulation at anywhere from 30-100 frames per second,
  98. // meaning an interframe delay of about 10-35 milliseconds.
  99. //
  100. // Looks best on a high-density LED setup (60+ pixels/meter).
  101. //
  102. //
  103. // There are two main parameters you can play with to control the look and
  104. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  105. // in step 3 above).
  106. //
  107. // COOLING: How much does the air cool as it rises?
  108. // Less cooling = taller flames.  More cooling = shorter flames.
  109. // Default 55, suggested range 20-100
  110. #define COOLING  55

  111. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  112. // Higher chance = more roaring fire.  Lower chance = more flickery fire.
  113. // Default 120, suggested range 50-200.
  114. #define SPARKING 120


  115. void Fire2012WithPalette()
  116. {
  117. // Array of temperature readings at each simulation cell
  118.   static uint8_t heat[NUM_LEDS];

  119.   // Step 1.  Cool down every cell a little
  120.     for( int i = 0; i < NUM_LEDS; i++) {
  121.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  122.     }
  123.   
  124.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  125.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  126.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  127.     }
  128.    
  129.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  130.     if( random8() < SPARKING ) {
  131.       int y = random8(7);
  132.       heat[y] = qadd8( heat[y], random8(160,255) );
  133.     }

  134.     // Step 4.  Map from heat cells to LED colors
  135.     for( int j = 0; j < NUM_LEDS; j++) {
  136.       // Scale the heat value from 0-255 down to 0-240
  137.       // for best results with color palettes.
  138.       uint8_t colorindex = scale8( heat[j], 240);
  139.       CRGB color = ColorFromPalette( gPal, colorindex);
  140.       int pixelnumber;
  141.       if( gReverseDirection ) {
  142.         pixelnumber = (NUM_LEDS-1) - j;
  143.       } else {
  144.         pixelnumber = j;
  145.       }
  146.       leds[pixelnumber] = color;
  147.     }
  148. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 06:43 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十:快速移动单个白色 LED

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十:快速移动单个白色 LED
  5. */

  6. // Use if you want to force the software SPI subsystem to be used for some reason (generally, you don't)
  7. // #define FASTLED_FORCE_SOFTWARE_SPI
  8. // Use if you want to force non-accelerated pin access (hint: you really don't, it breaks lots of things)
  9. // #define FASTLED_FORCE_SOFTWARE_SPI
  10. // #define FASTLED_FORCE_SOFTWARE_PINS
  11. #include <FastLED.h>

  12. ///////////////////////////////////////////////////////////////////////////////////////////
  13. //
  14. // Move a white dot along the strip of leds.  This program simply shows how to configure the leds,
  15. // and then how to turn a single pixel white and then off, moving down the line of pixels.
  16. //

  17. // How many leds are in the strip?
  18. #define NUM_LEDS 8

  19. // For led chips like WS2812, which have a data line, ground, and power, you just
  20. // need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
  21. // ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
  22. // Clock pin only needed for SPI based chipsets when not using hardware SPI
  23. #define DATA_PIN 5
  24. //#define CLOCK_PIN 13

  25. // This is an array of leds.  One item for each led in your strip.
  26. CRGB leds[NUM_LEDS];

  27. // This function sets up the ledsand tells the controller about them
  28. void setup() {
  29.   // sanity check delay - allows reprogramming if accidently blowing power w/leds
  30.   FastLED.setBrightness(26);
  31.   delay(2000);

  32.   // Uncomment/edit one of the following lines for your leds arrangement.
  33.   // ## Clockless types ##
  34.   // FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed
  35.   // FastLED.addLeds<SM16703, DATA_PIN, RGB>(leds, NUM_LEDS);
  36.   // FastLED.addLeds<TM1829, DATA_PIN, RGB>(leds, NUM_LEDS);
  37.   // FastLED.addLeds<TM1812, DATA_PIN, RGB>(leds, NUM_LEDS);
  38.   // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  39.   // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  40.   // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  41.   // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  42.   // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  43.   // FastLED.addLeds<UCS1904, DATA_PIN, RGB>(leds, NUM_LEDS);
  44.   // FastLED.addLeds<UCS2903, DATA_PIN, RGB>(leds, NUM_LEDS);
  45.   // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  46.   // FastLED.addLeds<WS2852, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  47.   // FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  48.   // FastLED.addLeds<GS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  49.   // FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  50.   // FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
  51.   // FastLED.addLeds<APA106, DATA_PIN, RGB>(leds, NUM_LEDS);
  52.   // FastLED.addLeds<PL9823, DATA_PIN, RGB>(leds, NUM_LEDS);
  53.   // FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
  54.   FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  55.   // FastLED.addLeds<WS2813, DATA_PIN, RGB>(leds, NUM_LEDS);
  56.   // FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
  57.   // FastLED.addLeds<WS2811_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  58.   // FastLED.addLeds<GE8822, DATA_PIN, RGB>(leds, NUM_LEDS);
  59.   // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  60.   // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  61.   // FastLED.addLeds<LPD1886, DATA_PIN, RGB>(leds, NUM_LEDS);
  62.   // FastLED.addLeds<LPD1886_8BIT, DATA_PIN, RGB>(leds, NUM_LEDS);
  63.   // ## Clocked (SPI) types ##
  64.   // FastLED.addLeds<LPD6803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  65.   // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  66.   // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  67.   // FastLED.addLeds<WS2803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  68.   // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  69.   // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  70.   // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  71.   // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  72.   // FastLED.addLeds<SK9822, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  73. }

  74. // This function runs over and over, and is where you do the magic to light
  75. // your leds.
  76. void loop() {
  77.   // Move a single white led
  78.   for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
  79.     // Turn our current led on to white, then show the leds
  80.     leds[whiteLed] = CRGB::White;

  81.     // Show the leds (only one of which is set to white, from above)
  82.     FastLED.show();

  83.     // Wait a little bit
  84.     delay(100);

  85.     // Turn our current led back to black for the next loop around
  86.     leds[whiteLed] = CRGB::Black;
  87.   }
  88. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 06:57 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十一: 使用多个控制器示例(多个 LED 阵列不同工作模式可调)

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十一: 使用多个控制器示例(多个 LED 阵列不同工作模式可调)
  5. */

  6. // MirroringSample - see https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples for more info on
  7. // using multiple controllers.  In this example, we're going to set up four NEOPIXEL strips on four
  8. // different pins, and show the same thing on all four of them, a simple bouncing dot/cyclon type pattern

  9. #include <FastLED.h>

  10. #define NUM_LEDS_PER_STRIP 8
  11. CRGB leds[NUM_LEDS_PER_STRIP];

  12. // For mirroring strips, all the "special" stuff happens just in setup.  We
  13. // just addLeds multiple times, once for each strip
  14. void setup() {
  15.   // tell FastLED there's 60 NEOPIXEL leds on pin 4
  16.   FastLED.addLeds<NEOPIXEL, 4>(leds, NUM_LEDS_PER_STRIP);

  17.   // tell FastLED there's 60 NEOPIXEL leds on pin 5
  18.   FastLED.addLeds<NEOPIXEL, 5>(leds, NUM_LEDS_PER_STRIP);

  19.   // tell FastLED there's 60 NEOPIXEL leds on pin 6
  20.   FastLED.addLeds<NEOPIXEL, 6>(leds, NUM_LEDS_PER_STRIP);

  21.   // tell FastLED there's 60 NEOPIXEL leds on pin 7
  22.   FastLED.addLeds<NEOPIXEL, 7>(leds, NUM_LEDS_PER_STRIP);
  23. }

  24. void loop() {
  25.   for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
  26.     // set our current dot to red
  27.     leds[i] = CRGB::Red;
  28.     FastLED.show();
  29.     // clear our current dot before we move on
  30.     leds[i] = CRGB::Black;
  31.     delay(100);
  32.   }

  33.   for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {
  34.     // set our current dot to red
  35.     leds[i] = CRGB::Red;
  36.     FastLED.show();
  37.     // clear our current dot before we move on
  38.     leds[i] = CRGB::Black;
  39.     delay(100);
  40.   }
  41. }
复制代码




签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 07:04 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十二: 在三个 NEOPIXEL 条上设置三个不同的管脚,
  每个条都有自己的 CRGB 数组来播放

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十二: 在三个 NEOPIXEL 条上设置三个不同的管脚,
  5.   每个条都有自己的 CRGB 数组来播放
  6. */

  7. // MultiArrays - see https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples for more info on
  8. // using multiple controllers.  In this example, we're going to set up three NEOPIXEL strips on three
  9. // different pins, each strip getting its own CRGB array to be played with

  10. #include <FastLED.h>

  11. #define NUM_LEDS_PER_STRIP 8
  12. CRGB redLeds[NUM_LEDS_PER_STRIP];
  13. CRGB greenLeds[NUM_LEDS_PER_STRIP];
  14. CRGB blueLeds[NUM_LEDS_PER_STRIP];

  15. // For mirroring strips, all the "special" stuff happens just in setup.  We
  16. // just addLeds multiple times, once for each strip
  17. void setup() {
  18.   // tell FastLED there's 60 NEOPIXEL leds on pin 10
  19.   FastLED.addLeds<NEOPIXEL, 4>(redLeds, NUM_LEDS_PER_STRIP);

  20.   // tell FastLED there's 60 NEOPIXEL leds on pin 11
  21.   FastLED.addLeds<NEOPIXEL, 5>(greenLeds, NUM_LEDS_PER_STRIP);

  22.   // tell FastLED there's 60 NEOPIXEL leds on pin 12
  23.   FastLED.addLeds<NEOPIXEL, 6>(blueLeds, NUM_LEDS_PER_STRIP);

  24. }

  25. void loop() {
  26.   for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
  27.     // set our current dot to red, green, and blue
  28.     redLeds[i] = CRGB::Red;
  29.     greenLeds[i] = CRGB::Green;
  30.     blueLeds[i] = CRGB::Blue;
  31.     FastLED.show();
  32.     // clear our current dot before we move on
  33.     redLeds[i] = CRGB::Black;
  34.     greenLeds[i] = CRGB::Black;
  35.     blueLeds[i] = CRGB::Black;
  36.     delay(100);
  37.   }

  38.   for(int i = NUM_LEDS_PER_STRIP-1; i >= 0; i--) {
  39.     // set our current dot to red, green, and blue
  40.     redLeds[i] = CRGB::Red;
  41.     greenLeds[i] = CRGB::Green;
  42.     blueLeds[i] = CRGB::Blue;
  43.     FastLED.show();
  44.     // clear our current dot before we move on
  45.     redLeds[i] = CRGB::Black;
  46.     greenLeds[i] = CRGB::Black;
  47.     blueLeds[i] = CRGB::Black;
  48.     delay(100);
  49.   }
  50. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 07:11 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十三: 使用多个控制器。在本例中,我们将在三个 NEOPIXEL 条带上设置三个
  不同的引脚,每个条带将引用单个 LED 阵列的不同部分

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十三: 使用多个控制器。在本例中,我们将在三个 NEOPIXEL 条带上设置三个
  5.   不同的引脚,每个条带将引用单个 LED 阵列的不同部分
  6. */

  7. // MultipleStripsInOneArray - see https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples for more info on
  8. // using multiple controllers.  In this example, we're going to set up four NEOPIXEL strips on three
  9. // different pins, each strip will be referring to a different part of the single led array

  10. #include <FastLED.h>

  11. #define NUM_STRIPS 3 //灯条数量
  12. #define NUM_LEDS_PER_STRIP 8
  13. #define NUM_LEDS NUM_LEDS_PER_STRIP * NUM_STRIPS

  14. CRGB leds[NUM_STRIPS * NUM_LEDS_PER_STRIP];

  15. // For mirroring strips, all the "special" stuff happens just in setup.  We
  16. // just addLeds multiple times, once for each strip
  17. void setup() {
  18.   // tell FastLED there's 60 NEOPIXEL leds on pin 2, starting at index 0 in the led array
  19.   FastLED.addLeds<NEOPIXEL, 2>(leds, 0, NUM_LEDS_PER_STRIP);

  20.   // tell FastLED there's 60 NEOPIXEL leds on pin 3, starting at index 60 in the led array
  21.   FastLED.addLeds<NEOPIXEL, 3>(leds, NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);

  22.   // tell FastLED there's 60 NEOPIXEL leds on pin 4, starting at index 120 in the led array
  23.   FastLED.addLeds<NEOPIXEL, 5>(leds, 2 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);

  24. }

  25. void loop() {
  26.   for(int i = 0; i < NUM_LEDS; i++) {
  27.     leds[i] = CRGB::Red;
  28.     FastLED.show();
  29.     leds[i] = CRGB::Black;
  30.     delay(100);
  31.   }
  32. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 07:35 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十四:骄傲2015动画,不断变化的彩虹

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十四:骄傲2015动画,不断变化的彩虹
  5. */

  6. #include "FastLED.h"
  7. #if FASTLED_VERSION < 3001000
  8. #error "Requires FastLED 3.1 or later; check github for latest code."
  9. #endif

  10. #define DATA_PIN    5
  11. //#define CLK_PIN   4
  12. #define LED_TYPE    WS2811
  13. #define COLOR_ORDER GRB
  14. #define NUM_LEDS    8
  15. #define BRIGHTNESS  33

  16. CRGB leds[NUM_LEDS];


  17. void setup() {
  18.   delay(3000); // 3 second delay for recovery
  19.   
  20.   // tell FastLED about the LED strip configuration
  21.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
  22.     .setCorrection(TypicalLEDStrip)
  23.     .setDither(BRIGHTNESS < 255);

  24.   // set master brightness control
  25.   FastLED.setBrightness(BRIGHTNESS);
  26. }


  27. void loop()
  28. {
  29.   pride();
  30.   FastLED.show();  
  31. }


  32. // This function draws rainbows with an ever-changing,
  33. // widely-varying set of parameters.
  34. void pride()
  35. {
  36.   static uint16_t sPseudotime = 0;
  37.   static uint16_t sLastMillis = 0;
  38.   static uint16_t sHue16 = 0;

  39.   uint8_t sat8 = beatsin88( 87, 220, 250);
  40.   uint8_t brightdepth = beatsin88( 341, 96, 224);
  41.   uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  42.   uint8_t msmultiplier = beatsin88(147, 23, 60);

  43.   uint16_t hue16 = sHue16;//gHue * 256;
  44.   uint16_t hueinc16 = beatsin88(113, 1, 3000);
  45.   
  46.   uint16_t ms = millis();
  47.   uint16_t deltams = ms - sLastMillis ;
  48.   sLastMillis  = ms;
  49.   sPseudotime += deltams * msmultiplier;
  50.   sHue16 += deltams * beatsin88( 400, 5,9);
  51.   uint16_t brightnesstheta16 = sPseudotime;
  52.   
  53.   for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
  54.     hue16 += hueinc16;
  55.     uint8_t hue8 = hue16 / 256;

  56.     brightnesstheta16  += brightnessthetainc16;
  57.     uint16_t b16 = sin16( brightnesstheta16  ) + 32768;

  58.     uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
  59.     uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
  60.     bri8 += (255 - brightdepth);
  61.    
  62.     CRGB newcolor = CHSV( hue8, sat8, bri8);
  63.    
  64.     uint16_t pixelnumber = i;
  65.     pixelnumber = (NUM_LEDS-1) - pixelnumber;
  66.    
  67.     nblend( leds[pixelnumber], newcolor, 64);
  68.   }
  69. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 07:47 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十五:RGB校准代码

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十五:RGB校准代码
  5. */

  6. #include "FastLED.h"

  7. ////////////////////////////////////////////////////////////////////////////////////////////////////
  8. //
  9. // RGB Calibration code
  10. //
  11. // Use this sketch to determine what the RGB ordering for your chipset should be.  Steps for setting up to use:

  12. // * Uncomment the line in setup that corresponds to the LED chipset that you are using.  (Note that they
  13. //   all explicitly specify the RGB order as RGB)
  14. // * Define DATA_PIN to the pin that data is connected to.
  15. // * (Optional) if using software SPI for chipsets that are SPI based, define CLOCK_PIN to the clock pin
  16. // * Compile/upload/run the sketch

  17. // You should see six leds on.  If the RGB ordering is correct, you should see 1 red led, 2 green
  18. // leds, and 3 blue leds.  If you see different colors, the count of each color tells you what the
  19. // position for that color in the rgb orering should be.  So, for example, if you see 1 Blue, and 2
  20. // Red, and 3 Green leds then the rgb ordering should be BRG (Blue, Red, Green).

  21. // You can then test this ordering by setting the RGB ordering in the addLeds line below to the new ordering
  22. // and it should come out correctly, 1 red, 2 green, and 3 blue.
  23. //
  24. //////////////////////////////////////////////////

  25. #define NUM_LEDS 8

  26. // For led chips like WS2812, which have a data line, ground, and power, you just
  27. // need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
  28. // ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
  29. // Clock pin only needed for SPI based chipsets when not using hardware SPI
  30. #define DATA_PIN 5
  31. //#define CLOCK_PIN 13

  32. CRGB leds[NUM_LEDS];

  33. void setup() {
  34.     // sanity check delay - allows reprogramming if accidently blowing power w/leds
  35.     FastLED.setBrightness(33);
  36.     delay(2000);

  37.     // Uncomment/edit one of the following lines for your leds arrangement.
  38.     // ## Clockless types ##
  39.     // FastLED.addLeds<SM16703, DATA_PIN, RGB>(leds, NUM_LEDS);
  40.     // FastLED.addLeds<TM1829, DATA_PIN, RGB>(leds, NUM_LEDS);
  41.     // FastLED.addLeds<TM1812, DATA_PIN, RGB>(leds, NUM_LEDS);
  42.     // FastLED.addLeds<TM1809, DATA_PIN, RGB>(leds, NUM_LEDS);
  43.     // FastLED.addLeds<TM1804, DATA_PIN, RGB>(leds, NUM_LEDS);
  44.     // FastLED.addLeds<TM1803, DATA_PIN, RGB>(leds, NUM_LEDS);
  45.     // FastLED.addLeds<UCS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  46.     // FastLED.addLeds<UCS1903B, DATA_PIN, RGB>(leds, NUM_LEDS);
  47.     // FastLED.addLeds<UCS1904, DATA_PIN, RGB>(leds, NUM_LEDS);
  48.     // FastLED.addLeds<UCS2903, DATA_PIN, RGB>(leds, NUM_LEDS);
  49.     FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  50.     // FastLED.addLeds<WS2852, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  51.     // FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  52.     // FastLED.addLeds<GS1903, DATA_PIN, RGB>(leds, NUM_LEDS);
  53.     // FastLED.addLeds<SK6812, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  54.     // FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
  55.     // FastLED.addLeds<APA106, DATA_PIN, RGB>(leds, NUM_LEDS);
  56.     // FastLED.addLeds<PL9823, DATA_PIN, RGB>(leds, NUM_LEDS);
  57.     // FastLED.addLeds<SK6822, DATA_PIN, RGB>(leds, NUM_LEDS);
  58.     // FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  59.     // FastLED.addLeds<WS2813, DATA_PIN, RGB>(leds, NUM_LEDS);
  60.     // FastLED.addLeds<APA104, DATA_PIN, RGB>(leds, NUM_LEDS);
  61.     // FastLED.addLeds<WS2811_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  62.     // FastLED.addLeds<GE8822, DATA_PIN, RGB>(leds, NUM_LEDS);
  63.     // FastLED.addLeds<GW6205, DATA_PIN, RGB>(leds, NUM_LEDS);
  64.     // FastLED.addLeds<GW6205_400, DATA_PIN, RGB>(leds, NUM_LEDS);
  65.     // FastLED.addLeds<LPD1886, DATA_PIN, RGB>(leds, NUM_LEDS);
  66.     // FastLED.addLeds<LPD1886_8BIT, DATA_PIN, RGB>(leds, NUM_LEDS);
  67.     // ## Clocked (SPI) types ##
  68.     // FastLED.addLeds<LPD6803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  69.     // FastLED.addLeds<LPD8806, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  70.     // FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  71.     // FastLED.addLeds<WS2803, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  72.     // FastLED.addLeds<SM16716, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
  73.     // FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  74.     // FastLED.addLeds<DOTSTAR, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  75.     // FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
  76.     // FastLED.addLeds<SK9822, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical

  77.     // FastLED.setBrightness(CRGB(255,255,255));
  78. }

  79. void loop() {
  80.     leds[0] = CRGB(200,0,0);
  81.     leds[1] = CRGB(0,200,0);
  82.     leds[2] = CRGB(0,0,200);
  83.     leds[3] = CRGB(200,0,35);
  84.     leds[4] = CRGB(35,200,0);
  85.     leds[5] = CRGB(50,0,200);
  86.     leds[6] = CRGB(0,0,0);
  87.     leds[7] = CRGB(50,200,50);
  88.     FastLED.show();
  89.     delay(1000);
  90. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 07:56 | 显示全部楼层
  实验场景图

18.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 08:03 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十六:简单RGB变换过渡色校准代码

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十六:简单RGB变换过渡色校准代码
  5. */

  6. #include <FastLED.h>
  7. #define NUM_LEDS 8

  8. CRGBArray<NUM_LEDS> leds;

  9. void setup() {
  10.   FastLED.addLeds<NEOPIXEL, 5>(leds, NUM_LEDS);
  11.   FastLED.setBrightness(33);
  12. }

  13. void loop() {
  14.   static uint8_t hue;
  15.   for (int i = 0; i < NUM_LEDS / 2; i++) {
  16.     // fade everything out
  17.     leds.fadeToBlackBy(40);

  18.     // let's set an led value
  19.     leds[i] = CHSV(hue++, 255, 255);

  20.     // now, let's first 20 leds to the top 20 leds,
  21.     leds(NUM_LEDS / 2, NUM_LEDS - 1) = leds(NUM_LEDS / 2 - 1 , 0);
  22.     FastLED.delay(33);
  23.   }
  24. }
复制代码



签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 08:16 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目二十七:TwinkleFOX:淡入淡出的闪烁“假日”灯

  实验开源代码


  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一:直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目二十七:TwinkleFOX:淡入淡出的闪烁“假日”灯
  5. */

  6. #include "FastLED.h"

  7. #define NUM_LEDS      8
  8. #define LED_TYPE   WS2811
  9. #define COLOR_ORDER   GRB
  10. #define DATA_PIN        5
  11. //#define CLK_PIN       4
  12. #define VOLTS          12
  13. #define MAX_MA       4000

  14. //  TwinkleFOX: Twinkling 'holiday' lights that fade in and out.
  15. //  Colors are chosen from a palette; a few palettes are provided.
  16. //
  17. //  This December 2015 implementation improves on the December 2014 version
  18. //  in several ways:
  19. //  - smoother fading, compatible with any colors and any palettes
  20. //  - easier control of twinkle speed and twinkle density
  21. //  - supports an optional 'background color'
  22. //  - takes even less RAM: zero RAM overhead per pixel
  23. //  - illustrates a couple of interesting techniques (uh oh...)
  24. //
  25. //  The idea behind this (new) implementation is that there's one
  26. //  basic, repeating pattern that each pixel follows like a waveform:
  27. //  The brightness rises from 0..255 and then falls back down to 0.
  28. //  The brightness at any given point in time can be determined as
  29. //  as a function of time, for example:
  30. //    brightness = sine( time ); // a sine wave of brightness over time
  31. //
  32. //  So the way this implementation works is that every pixel follows
  33. //  the exact same wave function over time.  In this particular case,
  34. //  I chose a sawtooth triangle wave (triwave8) rather than a sine wave,
  35. //  but the idea is the same: brightness = triwave8( time ).
  36. //
  37. //  Of course, if all the pixels used the exact same wave form, and
  38. //  if they all used the exact same 'clock' for their 'time base', all
  39. //  the pixels would brighten and dim at once -- which does not look
  40. //  like twinkling at all.
  41. //
  42. //  So to achieve random-looking twinkling, each pixel is given a
  43. //  slightly different 'clock' signal.  Some of the clocks run faster,
  44. //  some run slower, and each 'clock' also has a random offset from zero.
  45. //  The net result is that the 'clocks' for all the pixels are always out
  46. //  of sync from each other, producing a nice random distribution
  47. //  of twinkles.
  48. //
  49. //  The 'clock speed adjustment' and 'time offset' for each pixel
  50. //  are generated randomly.  One (normal) approach to implementing that
  51. //  would be to randomly generate the clock parameters for each pixel
  52. //  at startup, and store them in some arrays.  However, that consumes
  53. //  a great deal of precious RAM, and it turns out to be totally
  54. //  unnessary!  If the random number generate is 'seeded' with the
  55. //  same starting value every time, it will generate the same sequence
  56. //  of values every time.  So the clock adjustment parameters for each
  57. //  pixel are 'stored' in a pseudo-random number generator!  The PRNG
  58. //  is reset, and then the first numbers out of it are the clock
  59. //  adjustment parameters for the first pixel, the second numbers out
  60. //  of it are the parameters for the second pixel, and so on.
  61. //  In this way, we can 'store' a stable sequence of thousands of
  62. //  random clock adjustment parameters in literally two bytes of RAM.
  63. //
  64. //  There's a little bit of fixed-point math involved in applying the
  65. //  clock speed adjustments, which are expressed in eighths.  Each pixel's
  66. //  clock speed ranges from 8/8ths of the system clock (i.e. 1x) to
  67. //  23/8ths of the system clock (i.e. nearly 3x).
  68. //
  69. //  On a basic Arduino Uno or Leonardo, this code can twinkle 300+ pixels
  70. //  smoothly at over 50 updates per seond.
  71. //
  72. //  -Mark Kriegsman, December 2015

  73. CRGBArray<NUM_LEDS> leds;

  74. // Overall twinkle speed.
  75. // 0 (VERY slow) to 8 (VERY fast).
  76. // 4, 5, and 6 are recommended, default is 4.
  77. #define TWINKLE_SPEED 4

  78. // Overall twinkle density.
  79. // 0 (NONE lit) to 8 (ALL lit at once).
  80. // Default is 5.
  81. #define TWINKLE_DENSITY 5

  82. // How often to change color palettes.
  83. #define SECONDS_PER_PALETTE  30
  84. // Also: toward the bottom of the file is an array
  85. // called "ActivePaletteList" which controls which color
  86. // palettes are used; you can add or remove color palettes
  87. // from there freely.

  88. // Background color for 'unlit' pixels
  89. // Can be set to CRGB::Black if desired.
  90. CRGB gBackgroundColor = CRGB::Black;
  91. // Example of dim incandescent fairy light background color
  92. // CRGB gBackgroundColor = CRGB(CRGB::FairyLight).nscale8_video(16);

  93. // If AUTO_SELECT_BACKGROUND_COLOR is set to 1,
  94. // then for any palette where the first two entries
  95. // are the same, a dimmed version of that color will
  96. // automatically be used as the background color.
  97. #define AUTO_SELECT_BACKGROUND_COLOR 0

  98. // If COOL_LIKE_INCANDESCENT is set to 1, colors will
  99. // fade out slighted 'reddened', similar to how
  100. // incandescent bulbs change color as they get dim down.
  101. #define COOL_LIKE_INCANDESCENT 1


  102. CRGBPalette16 gCurrentPalette;
  103. CRGBPalette16 gTargetPalette;

  104. void setup() {
  105.   delay( 3000 ); //safety startup delay
  106.   FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
  107.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
  108.   .setCorrection(TypicalLEDStrip);
  109.   FastLED.setBrightness(33);

  110.   chooseNextColorPalette(gTargetPalette);
  111. }


  112. void loop()
  113. {
  114.   EVERY_N_SECONDS( SECONDS_PER_PALETTE ) {
  115.     chooseNextColorPalette( gTargetPalette );
  116.   }

  117.   EVERY_N_MILLISECONDS( 10 ) {
  118.     nblendPaletteTowardPalette( gCurrentPalette, gTargetPalette, 12);
  119.   }

  120.   drawTwinkles( leds);

  121.   FastLED.show();
  122. }


  123. //  This function loops over each pixel, calculates the
  124. //  adjusted 'clock' that this pixel should use, and calls
  125. //  "CalculateOneTwinkle" on each pixel.  It then displays
  126. //  either the twinkle color of the background color,
  127. //  whichever is brighter.
  128. void drawTwinkles( CRGBSet& L)
  129. {
  130.   // "PRNG16" is the pseudorandom number generator
  131.   // It MUST be reset to the same starting value each time
  132.   // this function is called, so that the sequence of 'random'
  133.   // numbers that it generates is (paradoxically) stable.
  134.   uint16_t PRNG16 = 11337;

  135.   uint32_t clock32 = millis();

  136.   // Set up the background color, "bg".
  137.   // if AUTO_SELECT_BACKGROUND_COLOR == 1, and the first two colors of
  138.   // the current palette are identical, then a deeply faded version of
  139.   // that color is used for the background color
  140.   CRGB bg;
  141.   if ( (AUTO_SELECT_BACKGROUND_COLOR == 1) &&
  142.        (gCurrentPalette[0] == gCurrentPalette[1] )) {
  143.     bg = gCurrentPalette[0];
  144.     uint8_t bglight = bg.getAverageLight();
  145.     if ( bglight > 64) {
  146.       bg.nscale8_video( 16); // very bright, so scale to 1/16th
  147.     } else if ( bglight > 16) {
  148.       bg.nscale8_video( 64); // not that bright, so scale to 1/4th
  149.     } else {
  150.       bg.nscale8_video( 86); // dim, scale to 1/3rd.
  151.     }
  152.   } else {
  153.     bg = gBackgroundColor; // just use the explicitly defined background color
  154.   }

  155.   uint8_t backgroundBrightness = bg.getAverageLight();

  156.   for ( CRGB& pixel : L) {
  157.     PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
  158.     uint16_t myclockoffset16 = PRNG16; // use that number as clock offset
  159.     PRNG16 = (uint16_t)(PRNG16 * 2053) + 1384; // next 'random' number
  160.     // use that number as clock speed adjustment factor (in 8ths, from 8/8ths to 23/8ths)
  161.     uint8_t myspeedmultiplierQ5_3 =  ((((PRNG16 & 0xFF) >> 4) + (PRNG16 & 0x0F)) & 0x0F) + 0x08;
  162.     uint32_t myclock30 = (uint32_t)((clock32 * myspeedmultiplierQ5_3) >> 3) + myclockoffset16;
  163.     uint8_t  myunique8 = PRNG16 >> 8; // get 'salt' value for this pixel

  164.     // We now have the adjusted 'clock' for this pixel, now we call
  165.     // the function that computes what color the pixel should be based
  166.     // on the "brightness = f( time )" idea.
  167.     CRGB c = computeOneTwinkle( myclock30, myunique8);

  168.     uint8_t cbright = c.getAverageLight();
  169.     int16_t deltabright = cbright - backgroundBrightness;
  170.     if ( deltabright >= 32 || (!bg)) {
  171.       // If the new pixel is significantly brighter than the background color,
  172.       // use the new color.
  173.       pixel = c;
  174.     } else if ( deltabright > 0 ) {
  175.       // If the new pixel is just slightly brighter than the background color,
  176.       // mix a blend of the new color and the background color
  177.       pixel = blend( bg, c, deltabright * 8);
  178.     } else {
  179.       // if the new pixel is not at all brighter than the background color,
  180.       // just use the background color.
  181.       pixel = bg;
  182.     }
  183.   }
  184. }


  185. //  This function takes a time in pseudo-milliseconds,
  186. //  figures out brightness = f( time ), and also hue = f( time )
  187. //  The 'low digits' of the millisecond time are used as
  188. //  input to the brightness wave function.
  189. //  The 'high digits' are used to select a color, so that the color
  190. //  does not change over the course of the fade-in, fade-out
  191. //  of one cycle of the brightness wave function.
  192. //  The 'high digits' are also used to determine whether this pixel
  193. //  should light at all during this cycle, based on the TWINKLE_DENSITY.
  194. CRGB computeOneTwinkle( uint32_t ms, uint8_t salt)
  195. {
  196.   uint16_t ticks = ms >> (8 - TWINKLE_SPEED);
  197.   uint8_t fastcycle8 = ticks;
  198.   uint16_t slowcycle16 = (ticks >> 8) + salt;
  199.   slowcycle16 += sin8( slowcycle16);
  200.   slowcycle16 =  (slowcycle16 * 2053) + 1384;
  201.   uint8_t slowcycle8 = (slowcycle16 & 0xFF) + (slowcycle16 >> 8);

  202.   uint8_t bright = 0;
  203.   if ( ((slowcycle8 & 0x0E) / 2) < TWINKLE_DENSITY) {
  204.     bright = attackDecayWave8( fastcycle8);
  205.   }

  206.   uint8_t hue = slowcycle8 - salt;
  207.   CRGB c;
  208.   if ( bright > 0) {
  209.     c = ColorFromPalette( gCurrentPalette, hue, bright, NOBLEND);
  210.     if ( COOL_LIKE_INCANDESCENT == 1 ) {
  211.       coolLikeIncandescent( c, fastcycle8);
  212.     }
  213.   } else {
  214.     c = CRGB::Black;
  215.   }
  216.   return c;
  217. }


  218. // This function is like 'triwave8', which produces a
  219. // symmetrical up-and-down triangle sawtooth waveform, except that this
  220. // function produces a triangle wave with a faster attack and a slower decay:
  221. //
  222. //     / \
  223. //    /     \
  224. //   /         \
  225. //  /             \
  226. //

  227. uint8_t attackDecayWave8( uint8_t i)
  228. {
  229.   if ( i < 86) {
  230.     return i * 3;
  231.   } else {
  232.     i -= 86;
  233.     return 255 - (i + (i / 2));
  234.   }
  235. }

  236. // This function takes a pixel, and if its in the 'fading down'
  237. // part of the cycle, it adjusts the color a little bit like the
  238. // way that incandescent bulbs fade toward 'red' as they dim.
  239. void coolLikeIncandescent( CRGB& c, uint8_t phase)
  240. {
  241.   if ( phase < 128) return;

  242.   uint8_t cooling = (phase - 128) >> 4;
  243.   c.g = qsub8( c.g, cooling);
  244.   c.b = qsub8( c.b, cooling * 2);
  245. }

  246. // A mostly red palette with green accents and white trim.
  247. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  248. const TProgmemRGBPalette16 RedGreenWhite_p FL_PROGMEM =
  249. { CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
  250.   CRGB::Red, CRGB::Red, CRGB::Red, CRGB::Red,
  251.   CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray,
  252.   CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green
  253. };

  254. // A mostly (dark) green palette with red berries.
  255. #define Holly_Green 0x00580c
  256. #define Holly_Red   0xB00402
  257. const TProgmemRGBPalette16 Holly_p FL_PROGMEM =
  258. { Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  259.   Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  260.   Holly_Green, Holly_Green, Holly_Green, Holly_Green,
  261.   Holly_Green, Holly_Green, Holly_Green, Holly_Red
  262. };

  263. // A red and white striped palette
  264. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  265. const TProgmemRGBPalette16 RedWhite_p FL_PROGMEM =
  266. { CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red,
  267.   CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray,
  268.   CRGB::Red,  CRGB::Red,  CRGB::Red,  CRGB::Red,
  269.   CRGB::Gray, CRGB::Gray, CRGB::Gray, CRGB::Gray
  270. };

  271. // A mostly blue palette with white accents.
  272. // "CRGB::Gray" is used as white to keep the brightness more uniform.
  273. const TProgmemRGBPalette16 BlueWhite_p FL_PROGMEM =
  274. { CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  275.   CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  276.   CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue,
  277.   CRGB::Blue, CRGB::Gray, CRGB::Gray, CRGB::Gray
  278. };

  279. // A pure "fairy light" palette with some brightness variations
  280. #define HALFFAIRY ((CRGB::FairyLight & 0xFEFEFE) / 2)
  281. #define QUARTERFAIRY ((CRGB::FairyLight & 0xFCFCFC) / 4)
  282. const TProgmemRGBPalette16 FairyLight_p FL_PROGMEM =
  283. { CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight,
  284.   HALFFAIRY,        HALFFAIRY,        CRGB::FairyLight, CRGB::FairyLight,
  285.   QUARTERFAIRY,     QUARTERFAIRY,     CRGB::FairyLight, CRGB::FairyLight,
  286.   CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight, CRGB::FairyLight
  287. };

  288. // A palette of soft snowflakes with the occasional bright one
  289. const TProgmemRGBPalette16 Snow_p FL_PROGMEM =
  290. { 0x304048, 0x304048, 0x304048, 0x304048,
  291.   0x304048, 0x304048, 0x304048, 0x304048,
  292.   0x304048, 0x304048, 0x304048, 0x304048,
  293.   0x304048, 0x304048, 0x304048, 0xE0F0FF
  294. };

  295. // A palette reminiscent of large 'old-school' C9-size tree lights
  296. // in the five classic colors: red, orange, green, blue, and white.
  297. #define C9_Red    0xB80400
  298. #define C9_Orange 0x902C02
  299. #define C9_Green  0x046002
  300. #define C9_Blue   0x070758
  301. #define C9_White  0x606820
  302. const TProgmemRGBPalette16 RetroC9_p FL_PROGMEM =
  303. { C9_Red,    C9_Orange, C9_Red,    C9_Orange,
  304.   C9_Orange, C9_Red,    C9_Orange, C9_Red,
  305.   C9_Green,  C9_Green,  C9_Green,  C9_Green,
  306.   C9_Blue,   C9_Blue,   C9_Blue,
  307.   C9_White
  308. };

  309. // A cold, icy pale blue palette
  310. #define Ice_Blue1 0x0C1040
  311. #define Ice_Blue2 0x182080
  312. #define Ice_Blue3 0x5080C0
  313. const TProgmemRGBPalette16 Ice_p FL_PROGMEM =
  314. {
  315.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  316.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  317.   Ice_Blue1, Ice_Blue1, Ice_Blue1, Ice_Blue1,
  318.   Ice_Blue2, Ice_Blue2, Ice_Blue2, Ice_Blue3
  319. };


  320. // Add or remove palette names from this list to control which color
  321. // palettes are used, and in what order.
  322. const TProgmemRGBPalette16* ActivePaletteList[] = {
  323.   &RetroC9_p,
  324.   &BlueWhite_p,
  325.   &RainbowColors_p,
  326.   &FairyLight_p,
  327.   &RedGreenWhite_p,
  328.   &PartyColors_p,
  329.   &RedWhite_p,
  330.   &Snow_p,
  331.   &Holly_p,
  332.   &Ice_p
  333. };


  334. // Advance to the next color palette in the list (above).
  335. void chooseNextColorPalette( CRGBPalette16& pal)
  336. {
  337.   const uint8_t numberOfPalettes = sizeof(ActivePaletteList) / sizeof(ActivePaletteList[0]);
  338.   static uint8_t whichPalette = -1;
  339.   whichPalette = addmod8( whichPalette, 1, numberOfPalettes);

  340.   pal = *(ActivePaletteList[whichPalette]);
  341. }
复制代码


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

本版积分规则

热门推荐

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