楼主: eagler8

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

[复制链接]

签到天数: 506 天

[LV.9]以坛为家II

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

  实验开源代码

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

  6. #include <Adafruit_NeoPixel.h>
  7. #ifdef __AVR__
  8. #include <avr/power.h>
  9. #endif

  10. #define PIN 6

  11. // Parameter 1 = number of pixels in strip
  12. // Parameter 2 = Arduino pin number (most are valid)
  13. // Parameter 3 = pixel type flags, add together as needed:
  14. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  15. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  16. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  17. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  18. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  19. Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);

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

  24. void setup() {
  25.   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  26. #if defined (__AVR_ATtiny85__)
  27.   if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  28. #endif
  29.   // End of trinket special code

  30.   strip.begin();
  31.   strip.setBrightness(50);
  32.   strip.show(); // Initialize all pixels to 'off'
  33. }

  34. void loop() {
  35.   // Some example procedures showing how to display to the pixels:
  36.   colorWipe(strip.Color(255, 0, 0), 50); // Red
  37.   colorWipe(strip.Color(0, 255, 0), 50); // Green
  38.   colorWipe(strip.Color(0, 0, 255), 50); // Blue
  39.   //colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
  40.   // Send a theater pixel chase in...
  41.   theaterChase(strip.Color(127, 127, 127), 50); // White
  42.   theaterChase(strip.Color(127, 0, 0), 50); // Red
  43.   theaterChase(strip.Color(0, 0, 127), 50); // Blue

  44.   rainbow(20);
  45.   rainbowCycle(20);
  46.   theaterChaseRainbow(50);
  47. }

  48. // Fill the dots one after the other with a color
  49. void colorWipe(uint32_t c, uint8_t wait) {
  50.   for (uint16_t i = 0; i < strip.numPixels(); i++) {
  51.     strip.setPixelColor(i, c);
  52.     strip.show();
  53.     delay(wait);
  54.   }
  55. }

  56. void rainbow(uint8_t wait) {
  57.   uint16_t i, j;

  58.   for (j = 0; j < 256; j++) {
  59.     for (i = 0; i < strip.numPixels(); i++) {
  60.       strip.setPixelColor(i, Wheel((i + j) & 255));
  61.     }
  62.     strip.show();
  63.     delay(wait);
  64.   }
  65. }

  66. // Slightly different, this makes the rainbow equally distributed throughout
  67. void rainbowCycle(uint8_t wait) {
  68.   uint16_t i, j;

  69.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  70.     for (i = 0; i < strip.numPixels(); i++) {
  71.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  72.     }
  73.     strip.show();
  74.     delay(wait);
  75.   }
  76. }

  77. //Theatre-style crawling lights.
  78. void theaterChase(uint32_t c, uint8_t wait) {
  79.   for (int j = 0; j < 10; j++) { //do 10 cycles of chasing
  80.     for (int q = 0; q < 3; q++) {
  81.       for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  82.         strip.setPixelColor(i + q, c);  //turn every third pixel on
  83.       }
  84.       strip.show();

  85.       delay(wait);

  86.       for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  87.         strip.setPixelColor(i + q, 0);      //turn every third pixel off
  88.       }
  89.     }
  90.   }
  91. }

  92. //Theatre-style crawling lights with rainbow effect
  93. void theaterChaseRainbow(uint8_t wait) {
  94.   for (int j = 0; j < 256; j++) {   // cycle all 256 colors in the wheel
  95.     for (int q = 0; q < 3; q++) {
  96.       for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  97.         strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
  98.       }
  99.       strip.show();

  100.       delay(wait);

  101.       for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  102.         strip.setPixelColor(i + q, 0);      //turn every third pixel off
  103.       }
  104.     }
  105.   }
  106. }

  107. // Input a value 0 to 255 to get a color value.
  108. // The colours are a transition r - g - b - back to r.
  109. uint32_t Wheel(byte WheelPos) {
  110.   WheelPos = 255 - WheelPos;
  111.   if (WheelPos < 85) {
  112.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  113.   }
  114.   if (WheelPos < 170) {
  115.     WheelPos -= 85;
  116.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  117.   }
  118.   WheelPos -= 170;
  119.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  120. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 10:27 | 显示全部楼层

  实验场景图

17.jpg

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 10:32 | 显示全部楼层
Adafruit_NeoPixel函数

一、初始化
1、包含头文件

  1. #include <Adafruit_NeoPixel.h>
复制代码


2、针对AVR单片机的特殊设置

  1. <div>#ifdef __AVR__</div><div> #include <avr/power.h> // Required for 16 MHz Adafruit Trinket</div><div>#endif</div><div>
  2. </div><div>// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.</div><div>// Any other board, you can remove this part (but no harm leaving it):</div><div>
  3. </div><div>#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)</div><div>  clock_prescale_set(clock_div_1);</div><div>#endif</div>
复制代码


3、引脚及灯珠的数量定义

  1. <div>// Which pin on the Arduino is connected to the NeoPixels?</div><div>#define PIN        6  // On Trinket or Gemma, suggest changing this to 1</div><div>
  2. </div><div>#define NUMPIXELS 3 // Popular NeoPixel ring size</div>
复制代码


4、定义strip函数

  1. Adafruit_NeoPixel strip(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
复制代码


5、初始化strip函数

  1. strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
复制代码


6、输出函数
每次设置完后需要调用此函数进行输出,否则无现象

  1. strip.show();
复制代码



签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 10:38 | 显示全部楼层
二、颜色设置
1、将颜色的R、G、B设置为0

  1. <p>strip.clear();</p><p></p>
复制代码

2、RGB设置函数

  1. <p>static uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b)</p><p>static uint32_t Adafruit_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w)</p><p>static uint32_t Adafruit_CPlay_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b)</p><p>static uint32_t Adafruit_CPlay_NeoPixel::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w)</p>
复制代码
  1. <p>void Adafruit_CPlay_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b)</p><p>void Adafruit_CPlay_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w)</p><p>void Adafruit_CPlay_NeoPixel::setPixelColor(uint16_t n, uint32_t c)</p><p>void Adafruit_CircuitPlayground::setPixelColor(uint8_t p, uint32_t c)</p><p>void Adafruit_CircuitPlayground::setPixelColor(uint8_t p, uint8_t r, uint8_t g, uint8_t b)</p><p>void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b)</p><p>void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w)</p><p>void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c)</p>
复制代码

备注:n是引脚标号,从0开始。w是亮度,0~255

三、亮度设置

  1. <p>void Adafruit_CPlay_NeoPixel::setBrightness(uint8_t b)</p><p>void Adafruit_CircuitPlayground::setBrightness(uint16_t b)</p><p>void Adafruit_NeoPixel::setBrightness(uint8_t b)</p>
复制代码

使用完这个函数之后需要调用show函数,才能输出

签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 13:09 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十三:使用FastLED库的简易流动变色

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目十三:使用FastLED库的简易流动变色
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define NUM_LEDS    8
  9. CRGB leds[NUM_LEDS];

  10. void setup() {
  11.   FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  12. }

  13. void loop() {
  14.   for (int i = 0; i <= 7; i++) {
  15.     leds[i] = CRGB ( 0, 0, 255);
  16.     FastLED.show();
  17.     delay(80);
  18.   }
  19.   for (int i = 7; i >= 0; i--) {
  20.     leds[i] = CRGB ( 220, 0, 0);
  21.     FastLED.show();
  22.     delay(80);
  23.   }
  24.     for (int i = 0; i <= 7; i++) {
  25.     leds[i] = CRGB ( 0, 230, 0);
  26.     FastLED.show();
  27.     delay(80);
  28.   }
  29.     for (int i = 7; i >= 0; i--) {
  30.     leds[i] = CRGB ( 150, 50, 0);
  31.     FastLED.show();
  32.     delay(80);
  33.   }
  34. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-16 22:22 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十四:使用FastLED库的八种快速调色板

  实验开源代码

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

  6. #include <FastLED.h>

  7. #define LED_PIN     5
  8. #define NUM_LEDS    8
  9. #define BRIGHTNESS  50
  10. #define LED_TYPE    WS2811
  11. #define COLOR_ORDER GRB
  12. CRGB leds[NUM_LEDS];

  13. #define UPDATES_PER_SECOND 100

  14. CRGBPalette16 currentPalette;
  15. TBlendType    currentBlending;

  16. extern CRGBPalette16 myRedWhiteBluePalette;
  17. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;


  18. void setup() {
  19.   delay( 3000 ); // power-up safety delay
  20.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  21.   FastLED.setBrightness(  BRIGHTNESS );
  22.   currentPalette = RainbowColors_p;
  23.   currentBlending = LINEARBLEND;
  24. }

  25. void loop() {
  26.   ChangePalettePeriodically();
  27.   static uint8_t startIndex = 0;
  28.   startIndex = startIndex + 1; /* motion speed */
  29.   FillLEDsFromPaletteColors( startIndex);
  30.   FastLED.show();
  31.   FastLED.delay(1000 / UPDATES_PER_SECOND);
  32. }

  33. void FillLEDsFromPaletteColors( uint8_t colorIndex) {
  34.   uint8_t brightness = 255;
  35.   for ( int i = 0; i < NUM_LEDS; ++i) {
  36.     leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  37.     colorIndex += 3;
  38.   }
  39. }

  40. void ChangePalettePeriodically() {
  41.   uint8_t secondHand = (millis() / 1000) % 60;
  42.   static uint8_t lastSecond = 99;
  43.   if ( lastSecond != secondHand) {
  44.     lastSecond = secondHand;
  45.     if ( secondHand ==  0)  {
  46.       currentPalette = RainbowColors_p;
  47.       currentBlending = LINEARBLEND;
  48.     }
  49.     if ( secondHand == 10)  {
  50.       currentPalette = RainbowStripeColors_p;
  51.       currentBlending = NOBLEND;
  52.     }
  53.     if ( secondHand == 15)  {
  54.       currentPalette = RainbowStripeColors_p;
  55.       currentBlending = LINEARBLEND;
  56.     }
  57.     if ( secondHand == 20)  {
  58.       SetupPurpleAndGreenPalette();
  59.       currentBlending = LINEARBLEND;
  60.     }
  61.     if ( secondHand == 25)  {
  62.       SetupTotallyRandomPalette();
  63.       currentBlending = LINEARBLEND;
  64.     }
  65.     if ( secondHand == 30)  {
  66.       SetupBlackAndWhiteStripedPalette();
  67.       currentBlending = NOBLEND;
  68.     }
  69.     if ( secondHand == 35)  {
  70.       SetupBlackAndWhiteStripedPalette();
  71.       currentBlending = LINEARBLEND;
  72.     }
  73.     if ( secondHand == 40)  {
  74.       currentPalette = CloudColors_p;
  75.       currentBlending = LINEARBLEND;
  76.     }
  77.     if ( secondHand == 45)  {
  78.       currentPalette = PartyColors_p;
  79.       currentBlending = LINEARBLEND;
  80.     }
  81.     if ( secondHand == 50)  {
  82.       currentPalette = myRedWhiteBluePalette_p;
  83.       currentBlending = NOBLEND;
  84.     }
  85.     if ( secondHand == 55)  {
  86.       currentPalette = myRedWhiteBluePalette_p;
  87.       currentBlending = LINEARBLEND;
  88.     }
  89.   }
  90. }

  91. // This function fills the palette with totally random colors.
  92. void SetupTotallyRandomPalette() {
  93.   for ( int i = 0; i < 16; ++i) {
  94.     currentPalette[i] = CHSV( random8(), 255, random8());
  95.   }
  96. }

  97. void SetupBlackAndWhiteStripedPalette() {
  98.   // 'black out' all 16 palette entries...
  99.   fill_solid( currentPalette, 16, CRGB::Black);
  100.   // and set every fourth one to white.
  101.   currentPalette[0] = CRGB::White;
  102.   currentPalette[4] = CRGB::White;
  103.   currentPalette[8] = CRGB::White;
  104.   currentPalette[12] = CRGB::White;
  105. }

  106. // This function sets up a palette of purple and green stripes.
  107. void SetupPurpleAndGreenPalette() {
  108.   CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  109.   CRGB green  = CHSV( HUE_GREEN, 255, 255);
  110.   CRGB black  = CRGB::Black;
  111.   currentPalette = CRGBPalette16(
  112.                      green,  green,  black,  black,
  113.                      purple, purple, black,  black,
  114.                      green,  green,  black,  black,
  115.                      purple, purple, black,  black );
  116. }

  117. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  118. {
  119.   CRGB::Red,
  120.   CRGB::Gray, // 'white' is too bright compared to red and blue
  121.   CRGB::Blue,
  122.   CRGB::Black,

  123.   CRGB::Red,
  124.   CRGB::Gray,
  125.   CRGB::Blue,
  126.   CRGB::Black,

  127.   CRGB::Red,
  128.   CRGB::Red,
  129.   CRGB::Gray,
  130.   CRGB::Gray,
  131.   CRGB::Blue,
  132.   CRGB::Blue,
  133.   CRGB::Black,
  134.   CRGB::Black
  135. };
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 06:00 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十五:每个 LED 灯条的颜色校正设置,以及总输出'色温'的总控制

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目十五:每个 LED 灯条的颜色校正设置,以及总输出'色温'的总控制
  5. */

  6. #include <FastLED.h>
  7. #define LED_PIN     5
  8. // Information about the LED strip itself
  9. #define NUM_LEDS    8
  10. #define CHIPSET     WS2811
  11. #define COLOR_ORDER GRB
  12. CRGB leds[NUM_LEDS];
  13. #define BRIGHTNESS  128

  14. // FastLED v2.1 provides two color-management controls:
  15. //   (1) color correction settings for each LED strip, and
  16. //   (2) master control of the overall output 'color temperature'
  17. //
  18. // THIS EXAMPLE demonstrates the second, "color temperature" control.
  19. // It shows a simple rainbow animation first with one temperature profile,
  20. // and a few seconds later, with a different temperature profile.
  21. //
  22. // The first pixel of the strip will show the color temperature.
  23. //
  24. // HELPFUL HINTS for "seeing" the effect in this demo:
  25. // * Don't look directly at the LED pixels.  Shine the LEDs aganst
  26. //   a white wall, table, or piece of paper, and look at the reflected light.
  27. //
  28. // * If you watch it for a bit, and then walk away, and then come back
  29. //   to it, you'll probably be able to "see" whether it's currently using
  30. //   the 'redder' or the 'bluer' temperature profile, even not counting
  31. //   the lowest 'indicator' pixel.
  32. //
  33. //
  34. // FastLED provides these pre-conigured incandescent color profiles:
  35. //     Candle, Tungsten40W, Tungsten100W, Halogen, CarbonArc,
  36. //     HighNoonSun, DirectSunlight, OvercastSky, ClearBlueSky,
  37. // FastLED provides these pre-configured gaseous-light color profiles:
  38. //     WarmFluorescent, StandardFluorescent, CoolWhiteFluorescent,
  39. //     FullSpectrumFluorescent, GrowLightFluorescent, BlackLightFluorescent,
  40. //     MercuryVapor, SodiumVapor, MetalHalide, HighPressureSodium,
  41. // FastLED also provides an "Uncorrected temperature" profile
  42. //    UncorrectedTemperature;

  43. #define TEMPERATURE_1 Tungsten100W
  44. #define TEMPERATURE_2 OvercastSky

  45. // How many seconds to show each temperature before switching
  46. #define DISPLAYTIME 20
  47. // How many seconds to show black between switches
  48. #define BLACKTIME   3

  49. void loop(){
  50.   // draw a generic, no-name rainbow
  51.   static uint8_t starthue = 0;
  52.   fill_rainbow( leds + 5, NUM_LEDS - 5, --starthue, 20);

  53.   // Choose which 'color temperature' profile to enable.
  54.   uint8_t secs = (millis() / 1000) % (DISPLAYTIME * 2);
  55.   if( secs < DISPLAYTIME) {
  56.     FastLED.setTemperature( TEMPERATURE_1 ); // first temperature
  57.     leds[0] = TEMPERATURE_1; // show indicator pixel
  58.   } else {
  59.     FastLED.setTemperature( TEMPERATURE_2 ); // second temperature
  60.     leds[0] = TEMPERATURE_2; // show indicator pixel
  61.   }

  62.   // Black out the LEDs for a few secnds between color changes
  63.   // to let the eyes and brains adjust
  64.   if( (secs % DISPLAYTIME) < BLACKTIME) {
  65.     memset8( leds, 0, NUM_LEDS * sizeof(CRGB));
  66.   }
  67.   
  68.   FastLED.show();
  69.   FastLED.delay(8);
  70. }

  71. void setup() {
  72.   delay( 3000 ); // power-up safety delay
  73.   // It's important to set the color correction for your LED strip here,
  74.   // so that colors can be more accurately rendered through the 'temperature' profiles
  75.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalSMD5050 );
  76.   FastLED.setBrightness( BRIGHTNESS );
  77. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

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

  实验开源代码

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

  6. #include <FastLED.h>

  7. // How many leds in your strip?
  8. #define NUM_LEDS 8

  9. // For led chips like Neopixels, which have a data line, ground, and power, you just
  10. // need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
  11. // ground, and power), like the LPD8806, define both DATA_PIN and CLOCK_PIN
  12. #define DATA_PIN 5
  13. //#define CLOCK_PIN 13

  14. // Define the array of leds
  15. CRGB leds[NUM_LEDS];

  16. void setup() {
  17.         Serial.begin(57600);
  18.         Serial.println("resetting");
  19.         FastLED.addLeds<WS2812,DATA_PIN,RGB>(leds,NUM_LEDS);
  20.         FastLED.setBrightness(30);
  21. }

  22. void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }

  23. void loop() {
  24.         static uint8_t hue = 0;
  25.         Serial.print("x");
  26.         // First slide the led in one direction
  27.         for(int i = 0; i < NUM_LEDS; i++) {
  28.                 // Set the i'th led to red
  29.                 leds[i] = CHSV(hue++, 255, 255);
  30.                 // Show the leds
  31.                 FastLED.show();
  32.                 // now that we've shown the leds, reset the i'th led to black
  33.                 // leds[i] = CRGB::Black;
  34.                 fadeall();
  35.                 // Wait a little bit before we loop around and do it again
  36.                 delay(10);
  37.         }
  38.         Serial.print("x");

  39.         // Now go in the other direction.  
  40.         for(int i = (NUM_LEDS)-1; i >= 0; i--) {
  41.                 // Set the i'th led to red
  42.                 leds[i] = CHSV(hue++, 255, 255);
  43.                 // Show the leds
  44.                 FastLED.show();
  45.                 // now that we've shown the leds, reset the i'th led to black
  46.                 // leds[i] = CRGB::Black;
  47.                 fadeall();
  48.                 // Wait a little bit before we loop around and do it again
  49.                 delay(10);
  50.         }
  51. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 06:19 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十七:FastLED“100行代码”演示卷轴动画效果

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目十七:FastLED“100行代码”演示卷轴动画效果
  5. */

  6. #include <FastLED.h>

  7. FASTLED_USING_NAMESPACE

  8. // FastLED "100-lines-of-code" demo reel, showing just a few
  9. // of the kinds of animation patterns you can quickly and easily
  10. // compose using FastLED.
  11. //
  12. // This example also shows one easy way to define multiple
  13. // animations patterns and have them automatically rotate.
  14. //
  15. // -Mark Kriegsman, December 2014


  16. #define DATA_PIN    5
  17. //#define CLK_PIN   4
  18. #define LED_TYPE    WS2811
  19. #define COLOR_ORDER GRB
  20. #define NUM_LEDS    8
  21. CRGB leds[NUM_LEDS];

  22. #define BRIGHTNESS          33
  23. #define FRAMES_PER_SECOND  120

  24. void setup() {
  25.   delay(3000); // 3 second delay for recovery

  26.   // tell FastLED about the LED strip configuration
  27.   FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  28.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

  29.   // set master brightness control
  30.   FastLED.setBrightness(BRIGHTNESS);
  31. }


  32. // List of patterns to cycle through.  Each is defined as a separate function below.
  33. typedef void (*SimplePatternList[])();
  34. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };

  35. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  36. uint8_t gHue = 0; // rotating "base color" used by many of the patterns

  37. void loop()
  38. {
  39.   // Call the current pattern function once, updating the 'leds' array
  40.   gPatterns[gCurrentPatternNumber]();

  41.   // send the 'leds' array out to the actual LED strip
  42.   FastLED.show();
  43.   // insert a delay to keep the framerate modest
  44.   FastLED.delay(1000 / FRAMES_PER_SECOND);

  45.   // do some periodic updates
  46.   EVERY_N_MILLISECONDS( 20 ) {
  47.     gHue++;  // slowly cycle the "base color" through the rainbow
  48.   }
  49.   EVERY_N_SECONDS( 10 ) {
  50.     nextPattern();  // change patterns periodically
  51.   }
  52. }

  53. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

  54. void nextPattern()
  55. {
  56.   // add one to the current pattern number, and wrap around at the end
  57.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  58. }

  59. void rainbow()
  60. {
  61.   // FastLED's built-in rainbow generator
  62.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  63. }

  64. void rainbowWithGlitter()
  65. {
  66.   // built-in FastLED rainbow, plus some random sparkly glitter
  67.   rainbow();
  68.   addGlitter(80);
  69. }

  70. void addGlitter( fract8 chanceOfGlitter)
  71. {
  72.   if ( random8() < chanceOfGlitter) {
  73.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  74.   }
  75. }

  76. void confetti()
  77. {
  78.   // random colored speckles that blink in and fade smoothly
  79.   fadeToBlackBy( leds, NUM_LEDS, 10);
  80.   int pos = random16(NUM_LEDS);
  81.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  82. }

  83. void sinelon()
  84. {
  85.   // a colored dot sweeping back and forth, with fading trails
  86.   fadeToBlackBy( leds, NUM_LEDS, 20);
  87.   int pos = beatsin16( 13, 0, NUM_LEDS - 1 );
  88.   leds[pos] += CHSV( gHue, 255, 192);
  89. }

  90. void bpm()
  91. {
  92.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  93.   uint8_t BeatsPerMinute = 62;
  94.   CRGBPalette16 palette = PartyColors_p;
  95.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  96.   for ( int i = 0; i < NUM_LEDS; i++) { //9948
  97.     leds[i] = ColorFromPalette(palette, gHue + (i * 2), beat - gHue + (i * 10));
  98.   }
  99. }

  100. void juggle() {
  101.   // eight colored dots, weaving in and out of sync with each other
  102.   fadeToBlackBy( leds, NUM_LEDS, 20);
  103.   uint8_t dothue = 0;
  104.   for ( int i = 0; i < 8; i++) {
  105.     leds[beatsin16( i + 7, 0, NUM_LEDS - 1 )] |= CHSV(dothue, 200, 255);
  106.     dothue += 32;
  107.   }
  108. }
复制代码


签到天数: 506 天

[LV.9]以坛为家II

 楼主| 发表于 2021-9-17 06:29 | 显示全部楼层
  【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  项目十八:随机60帧火焰花

  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+图形编程+仿真编程)
  3.   实验六十一: 直条8位 WS2812B 5050 RGB LED内置全彩驱动彩灯模块
  4.   项目十八:随机60帧火焰花
  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  36
  12. #define FRAMES_PER_SECOND 60

  13. bool gReverseDirection = false;

  14. CRGB leds[NUM_LEDS];

  15. void setup() {
  16.   delay(3000); // sanity delay
  17.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  18.   FastLED.setBrightness( BRIGHTNESS );
  19. }

  20. void loop()
  21. {
  22.   // Add entropy to random number generator; we use a lot of it.
  23.   // random16_add_entropy( random());

  24.   Fire2012(); // run simulation frame
  25.   
  26.   FastLED.show(); // display this frame
  27.   FastLED.delay(1000 / FRAMES_PER_SECOND);
  28. }


  29. // Fire2012 by Mark Kriegsman, July 2012
  30. // as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
  31. ////
  32. // This basic one-dimensional 'fire' simulation works roughly as follows:
  33. // There's a underlying array of 'heat' cells, that model the temperature
  34. // at each point along the line.  Every cycle through the simulation,
  35. // four steps are performed:
  36. //  1) All cells cool down a little bit, losing heat to the air
  37. //  2) The heat from each cell drifts 'up' and diffuses a little
  38. //  3) Sometimes randomly new 'sparks' of heat are added at the bottom
  39. //  4) The heat from each cell is rendered as a color into the leds array
  40. //     The heat-to-color mapping uses a black-body radiation approximation.
  41. //
  42. // Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
  43. //
  44. // This simulation scales it self a bit depending on NUM_LEDS; it should look
  45. // "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
  46. //
  47. // I recommend running this simulation at anywhere from 30-100 frames per second,
  48. // meaning an interframe delay of about 10-35 milliseconds.
  49. //
  50. // Looks best on a high-density LED setup (60+ pixels/meter).
  51. //
  52. //
  53. // There are two main parameters you can play with to control the look and
  54. // feel of your fire: COOLING (used in step 1 above), and SPARKING (used
  55. // in step 3 above).
  56. //
  57. // COOLING: How much does the air cool as it rises?
  58. // Less cooling = taller flames.  More cooling = shorter flames.
  59. // Default 50, suggested range 20-100
  60. #define COOLING  55

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


  65. void Fire2012()
  66. {
  67. // Array of temperature readings at each simulation cell
  68.   static uint8_t heat[NUM_LEDS];

  69.   // Step 1.  Cool down every cell a little
  70.     for( int i = 0; i < NUM_LEDS; i++) {
  71.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  72.     }
  73.   
  74.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  75.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  76.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  77.     }
  78.    
  79.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  80.     if( random8() < SPARKING ) {
  81.       int y = random8(7);
  82.       heat[y] = qadd8( heat[y], random8(160,255) );
  83.     }

  84.     // Step 4.  Map from heat cells to LED colors
  85.     for( int j = 0; j < NUM_LEDS; j++) {
  86.       CRGB color = HeatColor( heat[j]);
  87.       int pixelnumber;
  88.       if( gReverseDirection ) {
  89.         pixelnumber = (NUM_LEDS-1) - j;
  90.       } else {
  91.         pixelnumber = j;
  92.       }
  93.       leds[pixelnumber] = color;
  94.     }
  95. }
复制代码


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

本版积分规则

热门推荐

原价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!   
快速回复 返回顶部 返回列表