21755浏览
楼主: 驴友花雕

[项目] 【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 15:48:12

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 16:06:59

实验开源图形编程(Mind+、编玩边学)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 16:12:27

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 19:13:14

实验开源图形编程(Mind+、编玩边学)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 19:15:10

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 19:35:42

实验开源图形编程(Mind+、编玩边学)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 19:38:10

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 19:52:51

实验开源图形编程(Mind+、编玩边学)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 19:54:57

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 20:24:39

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之七:按键控制进入九种变幻彩灯程序

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之七:按键控制进入九种变幻彩灯程序
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #ifdef __AVR__
  8. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  9. #endif
  10. // Digital IO pin connected to the button. This will be driven with a
  11. // pull-up resistor so the switch pulls the pin to ground momentarily.
  12. // On a high -> low transition the button press logic will execute.
  13. #define BUTTON_PIN   2
  14. #define PIXEL_PIN    6  // Digital IO pin connected to the NeoPixels.
  15. #define PIXEL_COUNT 256 // Number of NeoPixels
  16. // Declare our NeoPixel strip object:
  17. Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
  18. // Argument 1 = Number of pixels in NeoPixel strip
  19. // Argument 2 = Arduino pin number (most are valid)
  20. // Argument 3 = Pixel type flags, add together as needed:
  21. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  22. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  23. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  24. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  25. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  26. boolean oldState = HIGH;
  27. int     mode     = 0;    // Currently-active animation mode, 0-9
  28. void setup() {
  29.   pinMode(BUTTON_PIN, INPUT_PULLUP);
  30.   strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  31.   strip.show();  // Initialize all pixels to 'off'
  32. }
  33. void loop() {
  34.   // Get current button state.
  35.   boolean newState = digitalRead(BUTTON_PIN);
  36.   // Check if state changed from high to low (button press).
  37.   if ((newState == LOW) && (oldState == HIGH)) {
  38.     // Short delay to debounce button.
  39.     delay(1);
  40.     // Check if button is still low after debounce.
  41.     newState = digitalRead(BUTTON_PIN);
  42.     if (newState == LOW) {     // Yes, still low
  43.       if (++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
  44.       switch (mode) {          // Start the new animation...
  45.         case 0:
  46.           colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
  47.           break;
  48.         case 1:
  49.           colorWipe(strip.Color(255,   0,   0), 50);    // Red
  50.           break;
  51.         case 2:
  52.           colorWipe(strip.Color(  0, 255,   0), 50);    // Green
  53.           break;
  54.         case 3:
  55.           colorWipe(strip.Color(  0,   0, 255), 50);    // Blue
  56.           break;
  57.         case 4:
  58.           theaterChase(strip.Color(127, 127, 127), 50); // White
  59.           break;
  60.         case 5:
  61.           theaterChase(strip.Color(127,   0,   0), 50); // Red
  62.           break;
  63.         case 6:
  64.           theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  65.           break;
  66.         case 7:
  67.           rainbow(10);
  68.           break;
  69.         case 8:
  70.           theaterChaseRainbow(50);
  71.           break;
  72.       }
  73.     }
  74.   }
  75.   // Set the last-read button state to the old state.
  76.   oldState = newState;
  77. }
  78. // Fill strip pixels one after another with a color. Strip is NOT cleared
  79. // first; anything there will be covered pixel by pixel. Pass in color
  80. // (as a single 'packed' 32-bit value, which you can get by calling
  81. // strip.Color(red, green, blue) as shown in the loop() function above),
  82. // and a delay time (in milliseconds) between pixels.
  83. void colorWipe(uint32_t color, int wait) {
  84.   for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  85.     strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
  86.     strip.show();                          //  Update strip to match
  87.     delay(30);                           //  Pause for a moment
  88.   }
  89. }
  90. // Theater-marquee-style chasing lights. Pass in a color (32-bit value,
  91. // a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
  92. // between frames.
  93. void theaterChase(uint32_t color, int wait) {
  94.   for (int a = 0; a < 10; a++) { // Repeat 10 times...
  95.     for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
  96.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  97.       // 'c' counts up from 'b' to end of strip in steps of 3...
  98.       for (int c = b; c < strip.numPixels(); c += 3) {
  99.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  100.       }
  101.       strip.show(); // Update strip with new contents
  102.       delay(30);  // Pause for a moment
  103.     }
  104.   }
  105. }
  106. // Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
  107. void rainbow(int wait) {
  108.   // Hue of first pixel runs 3 complete loops through the color wheel.
  109.   // Color wheel has a range of 65536 but it's OK if we roll over, so
  110.   // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
  111.   // means we'll make 3*65536/256 = 768 passes through this outer loop:
  112.   for (long firstPixelHue = 0; firstPixelHue < 3 * 65536; firstPixelHue += 256) {
  113.     for (int i = 0; i < strip.numPixels(); i++) { // For each pixel in strip...
  114.       // Offset pixel hue by an amount to make one full revolution of the
  115.       // color wheel (range of 65536) along the length of the strip
  116.       // (strip.numPixels() steps):
  117.       int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
  118.       // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
  119.       // optionally add saturation and value (brightness) (each 0 to 255).
  120.       // Here we're using just the single-argument hue variant. The result
  121.       // is passed through strip.gamma32() to provide 'truer' colors
  122.       // before assigning to each pixel:
  123.       strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
  124.     }
  125.     strip.show(); // Update strip with new contents
  126.     delay(30);  // Pause for a moment
  127.   }
  128. }
  129. // Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
  130. void theaterChaseRainbow(int wait) {
  131.   int firstPixelHue = 0;     // First pixel starts at red (hue 0)
  132.   for (int a = 0; a < 30; a++) { // Repeat 30 times...
  133.     for (int b = 0; b < 3; b++) { //  'b' counts from 0 to 2...
  134.       strip.clear();         //   Set all pixels in RAM to 0 (off)
  135.       // 'c' counts up from 'b' to end of strip in increments of 3...
  136.       for (int c = b; c < strip.numPixels(); c += 3) {
  137.         // hue of pixel 'c' is offset by an amount to make one full
  138.         // revolution of the color wheel (range 65536) along the length
  139.         // of the strip (strip.numPixels() steps):
  140.         int      hue   = firstPixelHue + c * 65536L / strip.numPixels();
  141.         uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
  142.         strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
  143.       }
  144.       strip.show();                // Update strip with new contents
  145.       delay(30);                 // Pause for a moment
  146.       firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
  147.     }
  148.   }
  149. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 20:28:20

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 20:30:55

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 20:44:41

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之八:多彩颜色调色板

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之八:多彩颜色调色板
  5. */
  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define NUM_LEDS    256
  9. #define BRIGHTNESS  23
  10. #define LED_TYPE    WS2811
  11. #define COLOR_ORDER GRB
  12. CRGB leds[NUM_LEDS];
  13. #define UPDATES_PER_SECOND 100 //定义每秒更新数
  14. // This example shows several ways to set up and use 'palettes' of colors
  15. // with FastLED.
  16. //
  17. // These compact palettes provide an easy way to re-colorize your
  18. // animation on the fly, quickly, easily, and with low overhead.
  19. //
  20. // USING palettes is MUCH simpler in practice than in theory, so first just
  21. // run this sketch, and watch the pretty lights as you then read through
  22. // the code.  Although this sketch has eight (or more) different color schemes,
  23. // the entire sketch compiles down to about 6.5K on AVR.
  24. //
  25. // FastLED provides a few pre-configured color palettes, and makes it
  26. // extremely easy to make up your own color schemes with palettes.
  27. //
  28. // Some notes on the more abstract 'theory and practice' of
  29. // FastLED compact palettes are at the bottom of this file.
  30. CRGBPalette16 currentPalette;
  31. TBlendType    currentBlending;
  32. extern CRGBPalette16 myRedWhiteBluePalette;
  33. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  34. void setup() {
  35.   delay( 3000 ); // power-up safety delay
  36.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  37.   FastLED.setBrightness(  BRIGHTNESS );
  38.   currentPalette = RainbowColors_p;
  39.   currentBlending = LINEARBLEND;
  40. }
  41. void loop()
  42. {
  43.   ChangePalettePeriodically();
  44.   static uint8_t startIndex = 0;
  45.   startIndex = startIndex + 1; /* motion speed */
  46.   FillLEDsFromPaletteColors( startIndex);
  47.   FastLED.show();
  48.   FastLED.delay(1000 / UPDATES_PER_SECOND);
  49. }
  50. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  51. {
  52.   uint8_t brightness = 255;
  53.   for ( int i = 0; i < NUM_LEDS; ++i) {
  54.     leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  55.     colorIndex += 3;
  56.   }
  57. }
  58. // There are several different palettes of colors demonstrated here.
  59. //
  60. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  61. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  62. //
  63. // Additionally, you can manually define your own color palettes, or you can write
  64. // code that creates color palettes on the fly.  All are shown here.
  65. void ChangePalettePeriodically()
  66. {
  67.   uint8_t secondHand = (millis() / 1000) % 60;
  68.   static uint8_t lastSecond = 99;
  69.   if ( lastSecond != secondHand) {
  70.     lastSecond = secondHand;
  71.     if ( secondHand ==  0)  {
  72.       currentPalette = RainbowColors_p;
  73.       currentBlending = LINEARBLEND;
  74.     }
  75.     if ( secondHand == 10)  {
  76.       currentPalette = RainbowStripeColors_p;
  77.       currentBlending = NOBLEND;
  78.     }
  79.     if ( secondHand == 15)  {
  80.       currentPalette = RainbowStripeColors_p;
  81.       currentBlending = LINEARBLEND;
  82.     }
  83.     if ( secondHand == 20)  {
  84.       SetupPurpleAndGreenPalette();
  85.       currentBlending = LINEARBLEND;
  86.     }
  87.     if ( secondHand == 25)  {
  88.       SetupTotallyRandomPalette();
  89.       currentBlending = LINEARBLEND;
  90.     }
  91.     if ( secondHand == 30)  {
  92.       SetupBlackAndWhiteStripedPalette();
  93.       currentBlending = NOBLEND;
  94.     }
  95.     if ( secondHand == 35)  {
  96.       SetupBlackAndWhiteStripedPalette();
  97.       currentBlending = LINEARBLEND;
  98.     }
  99.     if ( secondHand == 40)  {
  100.       currentPalette = CloudColors_p;
  101.       currentBlending = LINEARBLEND;
  102.     }
  103.     if ( secondHand == 45)  {
  104.       currentPalette = PartyColors_p;
  105.       currentBlending = LINEARBLEND;
  106.     }
  107.     if ( secondHand == 50)  {
  108.       currentPalette = myRedWhiteBluePalette_p;
  109.       currentBlending = NOBLEND;
  110.     }
  111.     if ( secondHand == 55)  {
  112.       currentPalette = myRedWhiteBluePalette_p;
  113.       currentBlending = LINEARBLEND;
  114.     }
  115.   }
  116. }
  117. // This function fills the palette with totally random colors.
  118. void SetupTotallyRandomPalette()
  119. {
  120.   for ( int i = 0; i < 16; ++i) {
  121.     currentPalette[i] = CHSV( random8(), 255, random8());
  122.   }
  123. }
  124. // This function sets up a palette of black and white stripes,
  125. // using code.  Since the palette is effectively an array of
  126. // sixteen CRGB colors, the various fill_* functions can be used
  127. // to set them up.
  128. void SetupBlackAndWhiteStripedPalette()
  129. {
  130.   // 'black out' all 16 palette entries...
  131.   fill_solid( currentPalette, 16, CRGB::Black);
  132.   // and set every fourth one to white.
  133.   currentPalette[0] = CRGB::White;
  134.   currentPalette[4] = CRGB::White;
  135.   currentPalette[8] = CRGB::White;
  136.   currentPalette[12] = CRGB::White;
  137. }
  138. // This function sets up a palette of purple and green stripes.
  139. void SetupPurpleAndGreenPalette()
  140. {
  141.   CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  142.   CRGB green  = CHSV( HUE_GREEN, 255, 255);
  143.   CRGB black  = CRGB::Black;
  144.   currentPalette = CRGBPalette16(
  145.                      green,  green,  black,  black,
  146.                      purple, purple, black,  black,
  147.                      green,  green,  black,  black,
  148.                      purple, purple, black,  black );
  149. }
  150. // This example shows how to set up a static color palette
  151. // which is stored in PROGMEM (flash), which is almost always more
  152. // plentiful than RAM.  A static PROGMEM palette like this
  153. // takes up 64 bytes of flash.
  154. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  155. {
  156.   CRGB::Red,
  157.   CRGB::Gray, // 'white' is too bright compared to red and blue
  158.   CRGB::Blue,
  159.   CRGB::Black,
  160.   CRGB::Red,
  161.   CRGB::Gray,
  162.   CRGB::Blue,
  163.   CRGB::Black,
  164.   CRGB::Red,
  165.   CRGB::Red,
  166.   CRGB::Gray,
  167.   CRGB::Gray,
  168.   CRGB::Blue,
  169.   CRGB::Blue,
  170.   CRGB::Black,
  171.   CRGB::Black
  172. };
  173. // Additional notes on FastLED compact palettes:
  174. //
  175. // Normally, in computer graphics, the palette (or "color lookup table")
  176. // has 256 entries, each containing a specific 24-bit RGB color.  You can then
  177. // index into the color palette using a simple 8-bit (one byte) value.
  178. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
  179. // is quite possibly "too many" bytes.
  180. //
  181. // FastLED does offer traditional 256-element palettes, for setups that
  182. // can afford the 768-byte cost in RAM.
  183. //
  184. // However, FastLED also offers a compact alternative.  FastLED offers
  185. // palettes that store 16 distinct entries, but can be accessed AS IF
  186. // they actually have 256 entries; this is accomplished by interpolating
  187. // between the 16 explicit entries to create fifteen intermediate palette
  188. // entries between each pair.
  189. //
  190. // So for example, if you set the first two explicit entries of a compact
  191. // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
  192. // the first sixteen entries from the virtual palette (of 256), you'd get
  193. // Green, followed by a smooth gradient from green-to-blue, and then Blue.
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-22 20:46:19

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 09:59:59

实验开源仿真编程(Linkboy V4.63)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 10:03:20

实验开源仿真编程(Linkboy V4.63)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 10:05:15

实验开源仿真编程(Linkboy V4.63)

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 10:07:59

实验场景图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 10:21:35

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之九:快速淡入淡出循环变色

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序之九:快速淡入淡出循环变色
  5. */
  6. #include <FastLED.h>
  7. // How many leds in your strip?
  8. #define NUM_LEDS 256
  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 6
  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(24);
  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. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 10:23:07

实验场景图  动态图

【Arduino】168种传感器系列实验(214)---8x32位全彩WS2812B屏图1

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 11:11:43

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序之十:FastLED“100行代码”演示卷轴动画效果

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵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    6
  17. //#define CLK_PIN   4
  18. #define LED_TYPE    WS2811
  19. #define COLOR_ORDER GRB
  20. #define NUM_LEDS    256
  21. CRGB leds[NUM_LEDS];
  22. #define BRIGHTNESS         26
  23. #define FRAMES_PER_SECOND  120
  24. void setup() {
  25.   delay(500); // 3 second delay for recovery
  26.   
  27.   // tell FastLED about the LED strip configuration
  28.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  29.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  30.   // set master brightness control
  31.   FastLED.setBrightness(BRIGHTNESS);
  32. }
  33. // List of patterns to cycle through.  Each is defined as a separate function below.
  34. typedef void (*SimplePatternList[])();
  35. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  36. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  37. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  38.   
  39. void loop()
  40. {
  41.   // Call the current pattern function once, updating the 'leds' array
  42.   gPatterns[gCurrentPatternNumber]();
  43.   // send the 'leds' array out to the actual LED strip
  44.   FastLED.show();  
  45.   // insert a delay to keep the framerate modest
  46.   FastLED.delay(500/FRAMES_PER_SECOND);
  47.   // do some periodic updates
  48.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  49.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  50. }
  51. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  52. void nextPattern()
  53. {
  54.   // add one to the current pattern number, and wrap around at the end
  55.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  56. }
  57. void rainbow()
  58. {
  59.   // FastLED's built-in rainbow generator
  60.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  61. }
  62. void rainbowWithGlitter()
  63. {
  64.   // built-in FastLED rainbow, plus some random sparkly glitter
  65.   rainbow();
  66.   addGlitter(80);
  67. }
  68. void addGlitter( fract8 chanceOfGlitter)
  69. {
  70.   if( random8() < chanceOfGlitter) {
  71.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  72.   }
  73. }
  74. void confetti()
  75. {
  76.   // random colored speckles that blink in and fade smoothly
  77.   fadeToBlackBy( leds, NUM_LEDS, 10);
  78.   int pos = random16(NUM_LEDS);
  79.   leds[pos] += CHSV( gHue + random8(256), 200, 255);
  80. }
  81. void sinelon()
  82. {
  83.   // a colored dot sweeping back and forth, with fading trails
  84.   fadeToBlackBy( leds, NUM_LEDS, 20);
  85.   int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  86.   leds[pos] += CHSV( gHue, 255, 192);
  87. }
  88. void bpm()
  89. {
  90.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  91.   uint8_t BeatsPerMinute = 62;
  92.   CRGBPalette16 palette = PartyColors_p;
  93.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  94.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  95.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  96.   }
  97. }
  98. void juggle() {
  99.   // eight colored dots, weaving in and out of sync with each other
  100.   fadeToBlackBy( leds, NUM_LEDS, 20);
  101.   uint8_t dothue = 0;
  102.   for( int i = 0; i < 8; i++) {
  103.     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  104.     dothue += 32;
  105.   }
  106. }
复制代码


回复

使用道具 举报

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail