21753浏览
楼主: 驴友花雕

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

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 11:13:14

实验场景图  动态图

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

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 11:27:42

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十一:骄傲2015动画,不断变化的彩虹

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十一:骄傲2015动画,不断变化的彩虹
  5. */
  6. #include "FastLED.h"
  7. // Pride2015
  8. // Animated, ever-changing rainbows.
  9. // by Mark Kriegsman
  10. #if FASTLED_VERSION < 3001000
  11. #error "Requires FastLED 3.1 or later; check github for latest code."
  12. #endif
  13. #define DATA_PIN    6
  14. //#define CLK_PIN   4
  15. #define LED_TYPE    WS2811
  16. #define COLOR_ORDER GRB
  17. #define NUM_LEDS    256
  18. #define BRIGHTNESS  22
  19. CRGB leds[NUM_LEDS];
  20. void setup() {
  21.   delay(1000); // 3 second delay for recovery
  22.   
  23.   // tell FastLED about the LED strip configuration
  24.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
  25.     .setCorrection(TypicalLEDStrip)
  26.     .setDither(BRIGHTNESS < 255);
  27.   // set master brightness control
  28.   FastLED.setBrightness(BRIGHTNESS);
  29. }
  30. void loop()
  31. {
  32.   pride();
  33.   FastLED.show();  
  34. }
  35. // This function draws rainbows with an ever-changing,
  36. // widely-varying set of parameters.
  37. void pride()
  38. {
  39.   static uint16_t sPseudotime = 0;
  40.   static uint16_t sLastMillis = 0;
  41.   static uint16_t sHue16 = 0;
  42.   uint8_t sat8 = beatsin88( 87, 220, 250);
  43.   uint8_t brightdepth = beatsin88( 341, 96, 224);
  44.   uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  45.   uint8_t msmultiplier = beatsin88(147, 23, 60);
  46.   uint16_t hue16 = sHue16;//gHue * 256;
  47.   uint16_t hueinc16 = beatsin88(113, 1, 3000);
  48.   
  49.   uint16_t ms = millis();
  50.   uint16_t deltams = ms - sLastMillis ;
  51.   sLastMillis  = ms;
  52.   sPseudotime += deltams * msmultiplier;
  53.   sHue16 += deltams * beatsin88( 400, 5,9);
  54.   uint16_t brightnesstheta16 = sPseudotime;
  55.   
  56.   for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
  57.     hue16 += hueinc16;
  58.     uint8_t hue8 = hue16 / 256;
  59.     brightnesstheta16  += brightnessthetainc16;
  60.     uint16_t b16 = sin16( brightnesstheta16  ) + 32768;
  61.     uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
  62.     uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
  63.     bri8 += (255 - brightdepth);
  64.    
  65.     CRGB newcolor = CHSV( hue8, sat8, bri8);
  66.    
  67.     uint16_t pixelnumber = i;
  68.     pixelnumber = (NUM_LEDS-1) - pixelnumber;
  69.    
  70.     nblend( leds[pixelnumber], newcolor, 64);
  71.   }
  72. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 11:30:06

实验场景图

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

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

使用道具 举报

驴友花雕  中级技神
 楼主|

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

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十二:TwinkleFOX-淡入淡出的闪烁“假日”灯

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十二:TwinkleFOX-淡入淡出的闪烁“假日”灯
  5. */
  6. #include "FastLED.h"
  7. #define NUM_LEDS      256
  8. #define LED_TYPE   WS2811
  9. #define COLOR_ORDER   GRB
  10. #define DATA_PIN        6
  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( 1000 ); //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(23);
  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. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 11:45:27

实验场景图

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:13:51

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十三:随机不同像素布局的xy矩阵

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十三:随机不同像素布局的xy矩阵
  5. */
  6. #include <FastLED.h>
  7. // Params for width and height
  8. const uint8_t kMatrixWidth = 8;
  9. const uint8_t kMatrixHeight = 32;
  10. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  11. // Param for different pixel layouts
  12. #define kMatrixSerpentineLayout  true
  13. // led array
  14. CRGB leds[kMatrixWidth * kMatrixHeight];
  15. // x,y, & time values
  16. uint32_t x,y,v_time,hue_time,hxy;
  17. // Play with the values of the variables below and see what kinds of effects they
  18. // have!  More octaves will make things slower.
  19. // how many octaves to use for the brightness and hue functions
  20. uint8_t octaves=1;
  21. uint8_t hue_octaves=3;
  22. // the 'distance' between points on the x and y axis
  23. int xscale=57771;
  24. int yscale=57771;
  25. // the 'distance' between x/y points for the hue noise
  26. int hue_scale=1;
  27. // how fast we move through time & hue noise
  28. int time_speed=1111;
  29. int hue_speed=31;
  30. // adjust these values to move along the x or y axis between frames
  31. int x_speed=331;
  32. int y_speed=1111;
  33. void loop() {
  34.   // fill the led array 2/16-bit noise values
  35.   fill_2dnoise16(leds, kMatrixWidth, kMatrixHeight, kMatrixSerpentineLayout,
  36.                 octaves,x,xscale,y,yscale,v_time,
  37.                 hue_octaves,hxy,hue_scale,hxy,hue_scale,hue_time, false);
  38.   FastLED.show();
  39.   // adjust the intra-frame time values
  40.   x += x_speed;
  41.   y += y_speed;
  42.   v_time += time_speed;
  43.   hue_time += hue_speed;
  44.   // delay(50);
  45. }
  46. void setup() {
  47.   // initialize the x/y and time values
  48.   random16_set_seed(8934);
  49.   random16_add_entropy(analogRead(3));
  50.   Serial.begin(57600);
  51.   Serial.println("resetting!");
  52.   delay(3000);
  53.   FastLED.addLeds<WS2811,6,GRB>(leds,NUM_LEDS);
  54.   FastLED.setBrightness(36);
  55.   hxy = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  56.   x = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  57.   y = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  58.   v_time = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  59.   hue_time = (uint32_t)((uint32_t)random16() << 16) + (uint32_t)random16();
  60. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:18:59

实验场景图

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:20:59

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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:31:41

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十四:将噪声数据转换为 LED 阵列中的动态颜色

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十四:将噪声数据转换为 LED 阵列中的动态颜色
  5. */
  6. #include <FastLED.h>
  7. #define LED_PIN     6
  8. #define BRIGHTNESS  26
  9. #define LED_TYPE    WS2811
  10. #define COLOR_ORDER GRB
  11. // Params for width and height
  12. const uint8_t kMatrixWidth  = 8;
  13. const uint8_t kMatrixHeight = 32;
  14. // Param for different pixel layouts
  15. const bool    kMatrixSerpentineLayout = true;
  16. // This example combines two features of FastLED to produce a remarkable range of
  17. // effects from a relatively small amount of code.  This example combines FastLED's
  18. // color palette lookup functions with FastLED's Perlin noise generator, and
  19. // the combination is extremely powerful.
  20. //
  21. // You might want to look at the "ColorPalette" and "Noise" examples separately
  22. // if this example code seems daunting.
  23. //
  24. //
  25. // The basic setup here is that for each frame, we generate a new array of
  26. // 'noise' data, and then map it onto the LED matrix through a color palette.
  27. //
  28. // Periodically, the color palette is changed, and new noise-generation parameters
  29. // are chosen at the same time.  In this example, specific noise-generation
  30. // values have been selected to match the given color palettes; some are faster,
  31. // or slower, or larger, or smaller than others, but there's no reason these
  32. // parameters can't be freely mixed-and-matched.
  33. //
  34. // In addition, this example includes some fast automatic 'data smoothing' at
  35. // lower noise speeds to help produce smoother animations in those cases.
  36. //
  37. // The FastLED built-in color palettes (Forest, Clouds, Lava, Ocean, Party) are
  38. // used, as well as some 'hand-defined' ones, and some proceedurally generated
  39. // palettes.
  40. #define NUM_LEDS (kMatrixWidth * kMatrixHeight)
  41. #define MAX_DIMENSION ((kMatrixWidth>kMatrixHeight) ? kMatrixWidth : kMatrixHeight)
  42. // The leds
  43. CRGB leds[kMatrixWidth * kMatrixHeight];
  44. // The 16 bit version of our coordinates
  45. static uint16_t x;
  46. static uint16_t y;
  47. static uint16_t z;
  48. // We're using the x/y dimensions to map to the x/y pixels on the matrix.  We'll
  49. // use the z-axis for "time".  speed determines how fast time moves forward.  Try
  50. // 1 for a very slow moving effect, or 60 for something that ends up looking like
  51. // water.
  52. uint16_t speed = 20; // speed is set dynamically once we've started up
  53. // Scale determines how far apart the pixels in our noise matrix are.  Try
  54. // changing these values around to see how it affects the motion of the display.  The
  55. // higher the value of scale, the more "zoomed out" the noise iwll be.  A value
  56. // of 1 will be so zoomed in, you'll mostly see solid colors.
  57. uint16_t scale = 30; // scale is set dynamically once we've started up
  58. // This is the array that we keep our computed noise values in
  59. uint8_t noise[MAX_DIMENSION][MAX_DIMENSION];
  60. CRGBPalette16 currentPalette( PartyColors_p );
  61. uint8_t       colorLoop = 1;
  62. void setup() {
  63.   delay(3000);
  64.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  65.   FastLED.setBrightness(BRIGHTNESS);
  66.   // Initialize our coordinates to some random values
  67.   x = random16();
  68.   y = random16();
  69.   z = random16();
  70. }
  71. // Fill the x/y array of 8-bit noise values using the inoise8 function.
  72. void fillnoise8() {
  73.   // If we're runing at a low "speed", some 8-bit artifacts become visible
  74.   // from frame-to-frame.  In order to reduce this, we can do some fast data-smoothing.
  75.   // The amount of data smoothing we're doing depends on "speed".
  76.   uint8_t dataSmoothing = 0;
  77.   if ( speed < 50) {
  78.     dataSmoothing = 200 - (speed * 4);
  79.   }
  80.   for (int i = 0; i < MAX_DIMENSION; i++) {
  81.     int ioffset = scale * i;
  82.     for (int j = 0; j < MAX_DIMENSION; j++) {
  83.       int joffset = scale * j;
  84.       uint8_t data = inoise8(x + ioffset, y + joffset, z);
  85.       // The range of the inoise8 function is roughly 16-238.
  86.       // These two operations expand those values out to roughly 0..255
  87.       // You can comment them out if you want the raw noise data.
  88.       data = qsub8(data, 16);
  89.       data = qadd8(data, scale8(data, 39));
  90.       if ( dataSmoothing ) {
  91.         uint8_t olddata = noise[i][j];
  92.         uint8_t newdata = scale8( olddata, dataSmoothing) + scale8( data, 256 - dataSmoothing);
  93.         data = newdata;
  94.       }
  95.       noise[i][j] = data;
  96.     }
  97.   }
  98.   z += speed;
  99.   // apply slow drift to X and Y, just for visual variation.
  100.   x += speed / 8;
  101.   y -= speed / 16;
  102. }
  103. void mapNoiseToLEDsUsingPalette()
  104. {
  105.   static uint8_t ihue = 0;
  106.   for (int i = 0; i < kMatrixWidth; i++) {
  107.     for (int j = 0; j < kMatrixHeight; j++) {
  108.       // We use the value at the (i,j) coordinate in the noise
  109.       // array for our brightness, and the flipped value from (j,i)
  110.       // for our pixel's index into the color palette.
  111.       uint8_t index = noise[j][i];
  112.       uint8_t bri =   noise[i][j];
  113.       // if this palette is a 'loop', add a slowly-changing base value
  114.       if ( colorLoop) {
  115.         index += ihue;
  116.       }
  117.       // brighten up, as the color palette itself often contains the
  118.       // light/dark dynamic range desired
  119.       if ( bri > 127 ) {
  120.         bri = 255;
  121.       } else {
  122.         bri = dim8_raw( bri * 2);
  123.       }
  124.       CRGB color = ColorFromPalette( currentPalette, index, bri);
  125.       leds[XY(i, j)] = color;
  126.     }
  127.   }
  128.   ihue += 1;
  129. }
  130. void loop() {
  131.   // Periodically choose a new palette, speed, and scale
  132.   ChangePaletteAndSettingsPeriodically();
  133.   // generate noise data
  134.   fillnoise8();
  135.   // convert the noise data to colors in the LED array
  136.   // using the current palette
  137.   mapNoiseToLEDsUsingPalette();
  138.   FastLED.show();
  139.   // delay(10);
  140. }
  141. // There are several different palettes of colors demonstrated here.
  142. //
  143. // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
  144. // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
  145. //
  146. // Additionally, you can manually define your own color palettes, or you can write
  147. // code that creates color palettes on the fly.
  148. // 1 = 5 sec per palette
  149. // 2 = 10 sec per palette
  150. // etc
  151. #define HOLD_PALETTES_X_TIMES_AS_LONG 1
  152. void ChangePaletteAndSettingsPeriodically()
  153. {
  154.   uint8_t secondHand = ((millis() / 1000) / HOLD_PALETTES_X_TIMES_AS_LONG) % 60;
  155.   static uint8_t lastSecond = 99;
  156.   if ( lastSecond != secondHand) {
  157.     lastSecond = secondHand;
  158.     if ( secondHand ==  0)  {
  159.       currentPalette = RainbowColors_p;
  160.       speed = 20;
  161.       scale = 30;
  162.       colorLoop = 1;
  163.     }
  164.     if ( secondHand ==  5)  {
  165.       SetupPurpleAndGreenPalette();
  166.       speed = 10;
  167.       scale = 50;
  168.       colorLoop = 1;
  169.     }
  170.     if ( secondHand == 10)  {
  171.       SetupBlackAndWhiteStripedPalette();
  172.       speed = 20;
  173.       scale = 30;
  174.       colorLoop = 1;
  175.     }
  176.     if ( secondHand == 15)  {
  177.       currentPalette = ForestColors_p;
  178.       speed =  8;
  179.       scale = 120;
  180.       colorLoop = 0;
  181.     }
  182.     if ( secondHand == 20)  {
  183.       currentPalette = CloudColors_p;
  184.       speed =  4;
  185.       scale = 30;
  186.       colorLoop = 0;
  187.     }
  188.     if ( secondHand == 25)  {
  189.       currentPalette = LavaColors_p;
  190.       speed =  8;
  191.       scale = 50;
  192.       colorLoop = 0;
  193.     }
  194.     if ( secondHand == 30)  {
  195.       currentPalette = OceanColors_p;
  196.       speed = 20;
  197.       scale = 90;
  198.       colorLoop = 0;
  199.     }
  200.     if ( secondHand == 35)  {
  201.       currentPalette = PartyColors_p;
  202.       speed = 20;
  203.       scale = 30;
  204.       colorLoop = 1;
  205.     }
  206.     if ( secondHand == 40)  {
  207.       SetupRandomPalette();
  208.       speed = 20;
  209.       scale = 20;
  210.       colorLoop = 1;
  211.     }
  212.     if ( secondHand == 45)  {
  213.       SetupRandomPalette();
  214.       speed = 50;
  215.       scale = 50;
  216.       colorLoop = 1;
  217.     }
  218.     if ( secondHand == 50)  {
  219.       SetupRandomPalette();
  220.       speed = 90;
  221.       scale = 90;
  222.       colorLoop = 1;
  223.     }
  224.     if ( secondHand == 55)  {
  225.       currentPalette = RainbowStripeColors_p;
  226.       speed = 30;
  227.       scale = 20;
  228.       colorLoop = 1;
  229.     }
  230.   }
  231. }
  232. // This function generates a random palette that's a gradient
  233. // between four different colors.  The first is a dim hue, the second is
  234. // a bright hue, the third is a bright pastel, and the last is
  235. // another bright hue.  This gives some visual bright/dark variation
  236. // which is more interesting than just a gradient of different hues.
  237. void SetupRandomPalette()
  238. {
  239.   currentPalette = CRGBPalette16(
  240.                      CHSV( random8(), 255, 32),
  241.                      CHSV( random8(), 255, 255),
  242.                      CHSV( random8(), 128, 255),
  243.                      CHSV( random8(), 255, 255));
  244. }
  245. // This function sets up a palette of black and white stripes,
  246. // using code.  Since the palette is effectively an array of
  247. // sixteen CRGB colors, the various fill_* functions can be used
  248. // to set them up.
  249. void SetupBlackAndWhiteStripedPalette()
  250. {
  251.   // 'black out' all 16 palette entries...
  252.   fill_solid( currentPalette, 16, CRGB::Black);
  253.   // and set every fourth one to white.
  254.   currentPalette[0] = CRGB::White;
  255.   currentPalette[4] = CRGB::White;
  256.   currentPalette[8] = CRGB::White;
  257.   currentPalette[12] = CRGB::White;
  258. }
  259. // This function sets up a palette of purple and green stripes.
  260. void SetupPurpleAndGreenPalette()
  261. {
  262.   CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  263.   CRGB green  = CHSV( HUE_GREEN, 255, 255);
  264.   CRGB black  = CRGB::Black;
  265.   currentPalette = CRGBPalette16(
  266.                      green,  green,  black,  black,
  267.                      purple, purple, black,  black,
  268.                      green,  green,  black,  black,
  269.                      purple, purple, black,  black );
  270. }
  271. //
  272. // Mark's xy coordinate mapping code.  See the XYMatrix for more information on it.
  273. //
  274. uint16_t XY( uint8_t x, uint8_t y)
  275. {
  276.   uint16_t i;
  277.   if ( kMatrixSerpentineLayout == false) {
  278.     i = (y * kMatrixWidth) + x;
  279.   }
  280.   if ( kMatrixSerpentineLayout == true) {
  281.     if ( y & 0x01) {
  282.       // Odd rows run backwards
  283.       uint8_t reverseX = (kMatrixWidth - 1) - x;
  284.       i = (y * kMatrixWidth) + reverseX;
  285.     } else {
  286.       // Even rows run forwards
  287.       i = (y * kMatrixWidth) + x;
  288.     }
  289.   }
  290.   return i;
  291. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:37:04

实验场景图  动态图

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

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:56:35

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十五:随机追逐的彗星效果

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十五:随机追逐的彗星效果
  5. */
  6. #include <WS2812FX.h>
  7. #define LED_COUNT 256
  8. #define LED_PIN   6
  9. WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  10. void setup() {
  11.   Serial.begin(115200);
  12.   ws2812fx.init();
  13.   ws2812fx.setBrightness(25);
  14.   // segment 0 is the builtin comet effect
  15.   ws2812fx.setSegment(0, 0, LED_COUNT / 2 - 1, FX_MODE_COMET,  RED, 1000, false);
  16.   // segment 1 is our custom effect
  17.   ws2812fx.setCustomMode(myCustomEffect);
  18.   ws2812fx.setSegment(1, LED_COUNT / 2, LED_COUNT - 1,   FX_MODE_CUSTOM, RED, 50, false);
  19.   ws2812fx.start();
  20. }
  21. void loop() {
  22.   ws2812fx.service();
  23. }
  24. uint16_t myCustomEffect(void) { // random chase
  25.   WS2812FX::Segment* seg = ws2812fx.getSegment(); // get the current segment
  26.   for (uint16_t i = seg->stop; i > seg->start; i--) {
  27.     ws2812fx.setPixelColor(i, ws2812fx.getPixelColor(i - 1));
  28.   }
  29.   uint32_t color = ws2812fx.getPixelColor(seg->start + 1);
  30.   int r = random(6) != 0 ? (color >> 16 & 0xFF) : random(256);
  31.   int g = random(6) != 0 ? (color >> 8  & 0xFF) : random(256);
  32.   int b = random(6) != 0 ? (color       & 0xFF) : random(256);
  33.   ws2812fx.setPixelColor(seg->start, r, g, b);
  34.   return seg->speed; // return the delay until the next animation step (in msec)
  35. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 14:58:23

实验场景图

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

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:28:33

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十六:WS2812FX库最简单的程序形式

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十六:WS2812FX库最简单的程序形式
  5. */
  6. #include <WS2812FX.h> //导入库
  7. #define LED_COUNT 256 //WS2812B LED数量
  8. #define LED_PIN    6 //WS2812B LED接脚
  9. WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  10. void setup() {
  11.   ws2812fx.init(); //初始化
  12.   ws2812fx.setBrightness(255); //设置亮度(0-255),可以控制总电流(重要!)
  13.   ws2812fx.setSpeed(200); // 设置速度
  14.   ws2812fx.setMode(FX_MODE_FIREWORKS_RANDOM);// 设置模式(内置63种模式)
  15.   ws2812fx.start(); //启动
  16. }
  17. void loop() {
  18.   ws2812fx.service(); //循环运行
  19. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:31:02

实验场景图

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

回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:35:31

本帖最后由 驴友花雕 于 2022-10-23 15:39 编辑

通过将 LED 串分成段(最多 10 个)并独立编程每个段,可以创建更复杂的效果。使用setSegment()函数对每个段的模式、颜色、速度和方向(正常或反向)进行编程:

      1. setSegment(segment index, start LED, stop LED, mode, color, speed, reverse);
      复制代码



请注意,某些效果使用不止一种颜色(最多三种),并通过指定颜色数组进行编程:


      1. setSegment(segment index, start LED, stop LED, mode, colors[], speed, reverse);
      复制代码



    1. //将 LED 串分成两个独立的部分
    2. uint32_t colours[] = {RED, GREEN};
    3. ws2812fx.setSegment( 0 , 0 , (LED_COUNT/ 2 )- 1 , FX_MODE_BLINK, colors, 1000 , false );
    4. ws2812fx.setSegment( 1 , LED_COUNT/ 2 , LED_COUNT- 1 , FX_MODE_BLINK, COLORS(ORANGE, PURPLE), 1000 , false );
    复制代码




回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:41:52

内置效果清单
静态- 不闪烁。只是普通的旧静态灯。
闪烁- 正常闪烁。50% 开/关时间。
呼吸- 众所周知的 i-Devices 的“待机呼吸”。固定速度。
颜色擦除- 依次点亮所有 LED。然后按顺序关闭它们。重复。
颜色擦除反转 - 与 Color Wipe 相同,但交换开/关颜色。
颜色擦除反向 - 依次点亮所有 LED。然后以相反的顺序关闭它们。重复。
颜色擦除反向反向 - 与 上条相同,除了交换开/关颜色。
随机颜色擦除- 将所有 LED 依次变为随机颜色。然后用另一种颜色重新开始。
随机颜色- 以一种随机颜色点亮所有 LED。然后将它们切换到下一个随机颜色。
单动态- 以随机颜色点亮每个 LED。将一个随机的 LED 一个接一个地更改为随机颜色。
多动态- 以随机颜色点亮每个 LED。同时将所有 LED 更改为新的随机颜色。
彩虹- 通过彩虹一次循环所有 LED。
彩虹循环- 在整个 LED 串上循环彩虹。
扫描- 来回运行单个像素。
双扫描- 以相反的方向来回运行两个像素。
淡入淡出- 使 LED 灯再次亮起和(几乎)熄灭。
剧院追逐 - 剧院式爬行灯。受 Adafruit 示例的启发。
剧院追逐彩虹- 具有彩虹效果的剧院式爬行灯。受 Adafruit 示例的启发。
行车灯- 带平滑正弦过渡的行车灯效果。
闪烁- 使多个 LED 闪烁、重置、重复。
闪烁随机 - 以随机颜色闪烁几个 LED,重置,重复。
闪烁淡入淡出 - 闪烁几个 LED,然后逐渐消失。
19 / 5000
翻译结果
闪烁淡入淡出随机  - 以随机颜色闪烁几个 LED,然后逐渐消失。
闪烁- 一次闪烁一个 LED。
Flash Sparkle - 以所选颜色点亮所有 LED。随机闪烁单个白色像素。
Hyper Sparkle - 像闪光一样。更多的闪光。
频闪- 经典频闪效果。
频闪彩虹- 经典频闪效果。骑自行车穿过彩虹。
Multi Strobe - 具有不同频闪次数和暂停的频闪效果,由速度设置控制。
闪烁彩虹- 经典闪烁效果。骑自行车穿过彩虹。
Chase White - 在白色上运行的颜色。
Chase Color - 白色在颜色上运行。
Chase Random - 白色运行,然后是随机颜色。
Chase Rainbow - 白色在彩虹上奔跑。
Chase Flash - 白色闪烁彩色。
Chase Flash Random - 白色闪烁,然后是随机颜色。
Chase Rainbow White - 运行在白色的彩虹。
Chase Blackout - 黑色在彩色上运行。
Chase Blackout Rainbow - 黑色在彩虹上奔跑。
随机颜色扫描- 从条带的开始和结束交替引入随机颜色。
运行颜色- 交替运行的颜色/白色像素。
Running Red Blue - 交替运行红色/蓝色像素。
随机运行- 随机彩色像素运行。
Larson 扫描仪- KITT
Comet - 从一端发射彗星。
烟花- 烟花火花。
Fireworks Random - 随机彩色烟花火花。
圣诞快乐- 交替运行绿色/红色像素。
火焰闪烁- 火焰闪烁效果。就像在狂风中。
Fire Flicker (soft) - 火焰闪烁效果。跑得更慢/更软。
Fire Flicker (intense) - 火焰闪烁效果。颜色范围更广。
Circus Combustus - 交替运行的白色/红色/黑色像素。
万圣节- 交替运行橙色/紫色像素。
双色追逐- 两个 LED 在背景色上运行。
三色追逐- 交替运行三个彩色像素。
TwinkleFOX - 灯光随机淡入淡出。
通过 63.自定义- 最多八个用户创建的自定义效果。
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:52:54

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  项目程序十七:使用Adafruit_NeoPixel库的串口控制20种效果

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百一十四:WS2812B全彩RGB像素屏 8x32点阵LED显示屏 硬屏模块
  4.   项目程序十七:使用Adafruit_NeoPixel库的串口控制20种效果
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #define PIN 6 //LED'in Din pinini yazın
  8. #define NUM_LEDS 256 //Kaç tane LED'iniz varsa buraya yazın
  9. #define BRIGHTNESS 20
  10. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
  11. int mod;
  12. int lastmod;
  13. String veri;
  14. int randommod;
  15. int parlaklik = 0;
  16. void setup() {
  17.   Serial.begin(9600);
  18.   veri.reserve(5);
  19.   strip.begin();
  20.   strip.show();
  21.   strip.setBrightness(BRIGHTNESS); //亮度范围0-255
  22.   Serial.println("WS2812准备就绪");
  23.   Serial.println("串口输入1-21");
  24. }
  25. void loop() {
  26.   switch (mod) {
  27.     case 1:
  28.       Serial.println("RGBLoop");
  29.       RGBLoop();
  30.       break;
  31.     case 2:
  32.       Serial.println("Strobe");
  33.       Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
  34.       break;
  35.     case 3:
  36.       Serial.println("HalloweenEyes");
  37.       HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
  38.       break;
  39.     case 4:
  40.       Serial.println("NewKITT RightToLeft");
  41.       NewKITT(0xff, 0, 0, 8, 10, 50);
  42.       break;
  43.     case 5:
  44.       Serial.println("Twinkle");
  45.       Twinkle(0xff, 0, 0, 10, 100, false);
  46.       break;
  47.     case 6:
  48.       Serial.println("TwinkleRandom");
  49.       Twinkle(0xff, 0, 0, 10, 100, false);
  50.       break;
  51.     case 7:
  52.       Serial.println("Sparkle");
  53.       Sparkle(0xff, 0xff, 0xff, 0);
  54.       break;
  55.     case 8:
  56.       Serial.println("SnowSparkle");
  57.       SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
  58.       break;
  59.     case 9:
  60.       Serial.println("RunningLights");
  61.       RunningLights(0xff, 0xff, 0x00, 50);
  62.       break;
  63.     case 10:
  64.       Serial.println("colorWipe");
  65.       colorWipe(0x00, 0xff, 0x00, 50);
  66.       colorWipe(0xff, 0x00, 0x00, 50);
  67.       break;
  68.     case 11:
  69.       Serial.println("rainbowCycle");
  70.       rainbowCycle(20);
  71.       break;
  72.     case 12:
  73.       Serial.println("theaterChase");
  74.       theaterChase(0xff, 0, 0, 50);
  75.       break;
  76.     case 13:
  77.       Serial.println("theaterChaseRainbow");
  78.       theaterChaseRainbow(50);
  79.       break;
  80.     case 14:
  81.       Serial.println("Fire");
  82.       Fire(55, 120, 15);
  83.       break;
  84.     case 15:
  85.       Serial.println("BouncingBalls");
  86.       meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  87.       break;
  88.     case 16:
  89.       Serial.println("meteorRain");
  90.       meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  91.       break;
  92.     case 17:
  93.       Serial.println("Red");
  94.       setAll(255, 0, 0);
  95.       break;
  96.     case 18:
  97.       Serial.println("Green");
  98.       setAll(0, 255, 0);
  99.       break;
  100.     case 19:
  101.       Serial.println("Blue");
  102.       setAll(0, 0, 255);
  103.       break;
  104.     case 20:
  105.       Serial.println("ON");
  106.       randommod = random(1, 19);
  107.       switch (randommod) {
  108.         case 1:
  109.           Serial.println("RGBLoop");
  110.           RGBLoop();
  111.           break;
  112.         case 2:
  113.           Serial.println("Strobe");
  114.           Strobe(0xff, 0xff, 0xff, 10, 50, 1000);
  115.           break;
  116.         case 3:
  117.           Serial.println("HalloweenEyes");
  118.           HalloweenEyes(0xff, 0x00, 0x00, 1, 4, true, random(5, 50), random(10, 50), random(50, 300));
  119.           break;
  120.         case 4:
  121.           Serial.println("NewKITT RightToLeft");
  122.           NewKITT(0xff, 0, 0, 8, 10, 50);
  123.           break;
  124.         case 5:
  125.           Serial.println("Twinkle");
  126.           Twinkle(0xff, 0, 0, 10, 100, false);
  127.           break;
  128.         case 6:
  129.           Serial.println("TwinkleRandom");
  130.           Twinkle(0xff, 0, 0, 10, 100, false);
  131.           break;
  132.         case 7:
  133.           Serial.println("Sparkle");
  134.           Sparkle(0xff, 0xff, 0xff, 0);
  135.           break;
  136.         case 8:
  137.           Serial.println("SnowSparkle");
  138.           SnowSparkle(0x10, 0x10, 0x10, 20, random(100, 1000));
  139.           break;
  140.         case 9:
  141.           Serial.println("RunningLights");
  142.           RunningLights(0xff, 0xff, 0x00, 50);
  143.           break;
  144.         case 10:
  145.           Serial.println("colorWipe");
  146.           colorWipe(0x00, 0xff, 0x00, 50);
  147.           colorWipe(0xff, 0x00, 0x00, 50);
  148.           break;
  149.         case 11:
  150.           Serial.println("rainbowCycle");
  151.           rainbowCycle(20);
  152.           break;
  153.         case 12:
  154.           Serial.println("theaterChase");
  155.           theaterChase(0xff, 0, 0, 50);
  156.           break;
  157.         case 13:
  158.           Serial.println("theaterChaseRainbow");
  159.           theaterChaseRainbow(50);
  160.           break;
  161.         case 14:
  162.           Serial.println("Fire");
  163.           Fire(55, 120, 15);
  164.           break;
  165.         case 15:
  166.           Serial.println("BouncingBalls");
  167.           meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  168.           break;
  169.         case 16:
  170.           Serial.println("meteorRain");
  171.           meteorRain(0xff, 0xff, 0xff, 10, 64, true, 30);
  172.           break;
  173.         case 17:
  174.           Serial.println("Red");
  175.           setAll(255, 0, 0);
  176.           break;
  177.         case 18:
  178.           Serial.println("Green");
  179.           setAll(0, 255, 0);
  180.           break;
  181.         case 19:
  182.           Serial.println("Blue");
  183.           setAll(0, 0, 255);
  184.           break;
  185.       }
  186.       break;
  187.     case 21:
  188.       Serial.println("OFF");
  189.       setAll(0, 0, 0);
  190.       break;
  191.     default:
  192.       //Serial.println("Red");
  193.       setAll(255, 0, 0);
  194.       break;
  195.   }
  196. }
  197. void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  198.   setAll(0, 0, 0);
  199.   for (int i = 0; i < NUM_LEDS + NUM_LEDS; i++) {
  200.     // fade brightness all LEDs one step
  201.     for (int j = 0; j < NUM_LEDS; j++) {
  202.       if ( (!meteorRandomDecay) || (random(10) > 5) ) {
  203.         fadeToBlack(j, meteorTrailDecay );
  204.       }
  205.       if (serialEvent() != false) break;
  206.     }
  207.     // draw meteor
  208.     for (int j = 0; j < meteorSize; j++) {
  209.       if ( ( i - j < NUM_LEDS) && (i - j >= 0) ) {
  210.         setPixel(i - j, red, green, blue);
  211.       }
  212.       if (serialEvent() != false) break;
  213.     }
  214.     showStrip();
  215.     delay(SpeedDelay);
  216.     if (serialEvent() != false) break;
  217.   }
  218. }
  219. void fadeToBlack(int ledNo, byte fadeValue) {
  220. #ifdef ADAFRUIT_NEOPIXEL_H
  221.   // NeoPixel
  222.   uint32_t oldColor;
  223.   uint8_t r, g, b;
  224.   int value;
  225.   oldColor = strip.getPixelColor(ledNo);
  226.   r = (oldColor & 0x00ff0000UL) >> 16;
  227.   g = (oldColor & 0x0000ff00UL) >> 8;
  228.   b = (oldColor & 0x000000ffUL);
  229.   r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
  230.   g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
  231.   b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);
  232.   strip.setPixelColor(ledNo, r, g, b);
  233. #endif
  234. #ifndef ADAFRUIT_NEOPIXEL_H
  235.   // FastLED
  236.   leds[ledNo].fadeToBlackBy( fadeValue );
  237. #endif
  238. }
  239. void BouncingBalls(byte red, byte green, byte blue, int BallCount) {
  240.   float Gravity = -9.81;
  241.   int StartHeight = 1;
  242.   float Height[BallCount];
  243.   float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
  244.   float ImpactVelocity[BallCount];
  245.   float TimeSinceLastBounce[BallCount];
  246.   int   Position[BallCount];
  247.   long  ClockTimeSinceLastBounce[BallCount];
  248.   float Dampening[BallCount];
  249.   for (int i = 0 ; i < BallCount ; i++) {
  250.     ClockTimeSinceLastBounce[i] = millis();
  251.     Height[i] = StartHeight;
  252.     Position[i] = 0;
  253.     ImpactVelocity[i] = ImpactVelocityStart;
  254.     TimeSinceLastBounce[i] = 0;
  255.     Dampening[i] = 0.90 - float(i) / pow(BallCount, 2);
  256.     if (serialEvent() != false) break;
  257.   }
  258.   while (true) {
  259.     for (int i = 0 ; i < BallCount ; i++) {
  260.       TimeSinceLastBounce[i] =  millis() - ClockTimeSinceLastBounce[i];
  261.       Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i] / 1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i] / 1000;
  262.       if ( Height[i] < 0 ) {
  263.         Height[i] = 0;
  264.         ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
  265.         ClockTimeSinceLastBounce[i] = millis();
  266.         if ( ImpactVelocity[i] < 0.01 ) {
  267.           ImpactVelocity[i] = ImpactVelocityStart;
  268.         }
  269.       }
  270.       Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
  271.       if (serialEvent() != false) break;
  272.     }
  273.     for (int i = 0 ; i < BallCount ; i++) {
  274.       setPixel(Position[i], red, green, blue);
  275.       if (serialEvent() != false) break;
  276.     }
  277.     showStrip();
  278.     setAll(0, 0, 0);
  279.     if (serialEvent() != false) break;
  280.   }
  281. }
  282. void Fire(int Cooling, int Sparking, int SpeedDelay) {
  283.   static byte heat[NUM_LEDS];
  284.   int cooldown;
  285.   for ( int i = 0; i < NUM_LEDS; i++) {
  286.     cooldown = random(0, ((Cooling * 10) / NUM_LEDS) + 2);
  287.     if (cooldown > heat[i]) {
  288.       heat[i] = 0;
  289.     } else {
  290.       heat[i] = heat[i] - cooldown;
  291.     }
  292.     if (serialEvent() != false) break;
  293.   }
  294.   for ( int k = NUM_LEDS - 1; k >= 2; k--) {
  295.     heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
  296.     if (serialEvent() != false) break;
  297.   }
  298.   if ( random(255) < Sparking ) {
  299.     int y = random(7);
  300.     heat[y] = heat[y] + random(160, 255);
  301.   }
  302.   for ( int j = 0; j < NUM_LEDS; j++) {
  303.     setPixelHeatColor(j, heat[j] );
  304.     if (serialEvent() != false) break;
  305.   }
  306.   showStrip();
  307.   delay(SpeedDelay);
  308. }
  309. void setPixelHeatColor (int Pixel, byte temperature) {
  310.   byte t192 = round((temperature / 255.0) * 191);
  311.   byte heatramp = t192 & 0x3F; // 0..63
  312.   heatramp <<= 2; // scale up to 0..252
  313.   if ( t192 > 0x80) {                    // hottest
  314.     setPixel(Pixel, 255, 255, heatramp);
  315.   } else if ( t192 > 0x40 ) {            // middle
  316.     setPixel(Pixel, 255, heatramp, 0);
  317.   } else {                               // coolest
  318.     setPixel(Pixel, heatramp, 0, 0);
  319.   }
  320. }
  321. void theaterChaseRainbow(int SpeedDelay) {
  322.   byte *c;
  323.   for (int j = 0; j < 256; j++) {
  324.     for (int q = 0; q < 3; q++) {
  325.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  326.         c = Wheel( (i + j) % 255);
  327.         setPixel(i + q, *c, *(c + 1), *(c + 2));
  328.         if (serialEvent() != false) break;
  329.       }
  330.       showStrip();
  331.       delay(SpeedDelay);
  332.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  333.         setPixel(i + q, 0, 0, 0);
  334.         if (serialEvent() != false) break;
  335.       }
  336.       if (serialEvent() != false) break;
  337.     }
  338.     if (serialEvent() != false) break;
  339.   }
  340. }
  341. void theaterChase(byte red, byte green, byte blue, int SpeedDelay) {
  342.   for (int j = 0; j < 10; j++) {
  343.     for (int q = 0; q < 3; q++) {
  344.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  345.         setPixel(i + q, red, green, blue);
  346.         if (serialEvent() != false) break;
  347.       }
  348.       showStrip();
  349.       delay(SpeedDelay);
  350.       for (int i = 0; i < NUM_LEDS; i = i + 3) {
  351.         setPixel(i + q, 0, 0, 0);
  352.         if (serialEvent() != false) break;
  353.       }
  354.       if (serialEvent() != false) break;
  355.     }
  356.     if (serialEvent() != false) break;
  357.   }
  358. }
  359. void rainbowCycle(int SpeedDelay) {
  360.   byte *c;
  361.   uint16_t i, j;
  362.   for (j = 0; j < 256 * 5; j++) {
  363.     for (i = 0; i < NUM_LEDS; i++) {
  364.       c = Wheel(((i * 256 / NUM_LEDS) + j) & 255);
  365.       setPixel(i, *c, *(c + 1), *(c + 2));
  366.       if (serialEvent() != false) break;
  367.     }
  368.     showStrip();
  369.     delay(SpeedDelay);
  370.     if (serialEvent() != false) break;
  371.   }
  372. }
  373. byte * Wheel(byte WheelPos) {
  374.   static byte c[3];
  375.   if (WheelPos < 85) {
  376.     c[0] = WheelPos * 3;
  377.     c[1] = 255 - WheelPos * 3;
  378.     c[2] = 0;
  379.   } else if (WheelPos < 170) {
  380.     WheelPos -= 85;
  381.     c[0] = 255 - WheelPos * 3;
  382.     c[1] = 0;
  383.     c[2] = WheelPos * 3;
  384.   } else {
  385.     WheelPos -= 170;
  386.     c[0] = 0;
  387.     c[1] = WheelPos * 3;
  388.     c[2] = 255 - WheelPos * 3;
  389.   }
  390.   return c;
  391. }
  392. void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  393.   for (uint16_t i = 0; i < NUM_LEDS; i++) {
  394.     setPixel(i, red, green, blue);
  395.     showStrip();
  396.     delay(SpeedDelay);
  397.     if (serialEvent() != false) break;
  398.   }
  399. }
  400. void RunningLights(byte red, byte green, byte blue, int WaveDelay) {
  401.   int Position = 0;
  402.   for (int j = 0; j < NUM_LEDS * 2; j++)
  403.   {
  404.     Position++; // = 0; //Position + Rate;
  405.     for (int i = 0; i < NUM_LEDS; i++) {
  406.       // sine wave, 3 offset waves make a rainbow!
  407.       //float level = sin(i+Position) * 127 + 128;
  408.       //setPixel(i,level,0,0);
  409.       //float level = sin(i+Position) * 127 + 128;
  410.       setPixel(i, ((sin(i + Position) * 127 + 128) / 255)*red,
  411.                ((sin(i + Position) * 127 + 128) / 255)*green,
  412.                ((sin(i + Position) * 127 + 128) / 255)*blue);
  413.       if (serialEvent() != false) break;
  414.     }
  415.     showStrip();
  416.     delay(WaveDelay);
  417.     if (serialEvent() != false) break;
  418.   }
  419. }
  420. void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
  421.   setAll(red, green, blue);
  422.   int Pixel = random(NUM_LEDS);
  423.   setPixel(Pixel, 0xff, 0xff, 0xff);
  424.   showStrip();
  425.   delay(SparkleDelay);
  426.   setPixel(Pixel, red, green, blue);
  427.   showStrip();
  428.   delay(SpeedDelay);
  429. }
  430. void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
  431.   int Pixel = random(NUM_LEDS);
  432.   setPixel(Pixel, red, green, blue);
  433.   showStrip();
  434.   delay(SpeedDelay);
  435.   setPixel(Pixel, 0, 0, 0);
  436. }
  437. void TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne) {
  438.   setAll(0, 0, 0);
  439.   for (int i = 0; i < Count; i++) {
  440.     setPixel(random(NUM_LEDS), random(0, 255), random(0, 255), random(0, 255));
  441.     showStrip();
  442.     delay(SpeedDelay);
  443.     if (OnlyOne) {
  444.       setAll(0, 0, 0);
  445.     }
  446.     if (serialEvent() != false) break;
  447.   }
  448.   delay(SpeedDelay);
  449. }
  450. void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  451.   setAll(0, 0, 0);
  452.   for (int i = 0; i < Count; i++) {
  453.     setPixel(random(NUM_LEDS), red, green, blue);
  454.     showStrip();
  455.     delay(SpeedDelay);
  456.     if (OnlyOne) {
  457.       setAll(0, 0, 0);
  458.     }
  459.     if (serialEvent() != false) break;
  460.   }
  461.   delay(SpeedDelay);
  462. }
  463. void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  464.   RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  465.   LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  466.   /*OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  467.     CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  468.     LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  469.     RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  470.     OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  471.     CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);*/
  472. }
  473. void CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  474.   for (int i = ((NUM_LEDS - EyeSize) / 2); i >= 0; i--) {
  475.     setAll(0, 0, 0);
  476.     setPixel(i, red / 10, green / 10, blue / 10);
  477.     for (int j = 1; j <= EyeSize; j++) {
  478.       setPixel(i + j, red, green, blue);
  479.       if (serialEvent() != false) break;
  480.     }
  481.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  482.     setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
  483.     for (int j = 1; j <= EyeSize; j++) {
  484.       setPixel(NUM_LEDS - i - j, red, green, blue);
  485.       if (serialEvent() != false) break;
  486.     }
  487.     setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
  488.     showStrip();
  489.     delay(SpeedDelay);
  490.     if (serialEvent() != false) break;
  491.   }
  492.   delay(ReturnDelay);
  493. }
  494. void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  495.   for (int i = 0; i <= ((NUM_LEDS - EyeSize) / 2); i++) {
  496.     setAll(0, 0, 0);
  497.     setPixel(i, red / 10, green / 10, blue / 10);
  498.     for (int j = 1; j <= EyeSize; j++) {
  499.       setPixel(i + j, red, green, blue);
  500.       if (serialEvent() != false) break;
  501.     }
  502.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  503.     setPixel(NUM_LEDS - i, red / 10, green / 10, blue / 10);
  504.     for (int j = 1; j <= EyeSize; j++) {
  505.       setPixel(NUM_LEDS - i - j, red, green, blue);
  506.       if (serialEvent() != false) break;
  507.     }
  508.     setPixel(NUM_LEDS - i - EyeSize - 1, red / 10, green / 10, blue / 10);
  509.     showStrip();
  510.     delay(SpeedDelay);
  511.     if (serialEvent() != false) break;
  512.   }
  513.   delay(ReturnDelay);
  514. }
  515. void LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  516.   for (int i = 0; i < NUM_LEDS - EyeSize - 2; i++) {
  517.     setAll(0, 0, 0);
  518.     setPixel(i, red / 10, green / 10, blue / 10);
  519.     for (int j = 1; j <= EyeSize; j++) {
  520.       setPixel(i + j, red, green, blue);
  521.       if (serialEvent() != false) break;;
  522.     }
  523.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  524.     showStrip();
  525.     delay(SpeedDelay);
  526.     if (serialEvent() != false) break;
  527.   }
  528.   delay(ReturnDelay);
  529. }
  530. void RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  531.   for (int i = NUM_LEDS - EyeSize - 2; i > 0; i--) {
  532.     setAll(0, 0, 0);
  533.     setPixel(i, red / 10, green / 10, blue / 10);
  534.     for (int j = 1; j <= EyeSize; j++) {
  535.       setPixel(i + j, red, green, blue);
  536.       if (serialEvent() != false) break;
  537.     }
  538.     setPixel(i + EyeSize + 1, red / 10, green / 10, blue / 10);
  539.     showStrip();
  540.     delay(SpeedDelay);
  541.     if (serialEvent() != false) break;
  542.   }
  543.   delay(ReturnDelay);
  544. }
  545. void HalloweenEyes(byte red, byte green, byte blue, int EyeWidth, int EyeSpace, boolean Fade, int Steps, int FadeDelay, int EndPause) {
  546.   randomSeed(analogRead(0));
  547.   int i;
  548.   int StartPoint  = random( 0, NUM_LEDS - (2 * EyeWidth) - EyeSpace );
  549.   int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
  550.   for (i = 0; i < EyeWidth; i++) {
  551.     setPixel(StartPoint + i, red, green, blue);
  552.     setPixel(Start2ndEye + i, red, green, blue);
  553.     if (serialEvent() != false) break;
  554.   }
  555.   showStrip();
  556.   if (Fade == true) {
  557.     float r, g, b;
  558.     for (int j = Steps; j >= 0; j--) {
  559.       r = j * (red / Steps);
  560.       g = j * (green / Steps);
  561.       b = j * (blue / Steps);
  562.       for (i = 0; i < EyeWidth; i++) {
  563.         setPixel(StartPoint + i, r, g, b);
  564.         setPixel(Start2ndEye + i, r, g, b);
  565.         if (serialEvent() != false) break;
  566.       }
  567.       showStrip();
  568.       delay(FadeDelay);
  569.       if (serialEvent() != false) break;
  570.     }
  571.   }
  572.   setAll(0, 0, 0); // Set all black
  573.   delay(EndPause);
  574. }
  575. void Strobe(byte red, byte green, byte blue, int StrobeCount, int FlashDelay, int EndPause) {
  576.   for (int j = 0; j < StrobeCount; j++) {
  577.     setAll(red, green, blue);
  578.     showStrip();
  579.     delay(FlashDelay);
  580.     setAll(0, 0, 0);
  581.     showStrip();
  582.     delay(FlashDelay);
  583.     if (serialEvent() != false) break;
  584.   }
  585.   delay(EndPause);
  586. }
  587. void RGBLoop() {
  588.   for (int j = 0; j < 3; j++ ) {
  589.     // Fade IN
  590.     for (int k = 0; k < 256; k++) {
  591.       switch (j) {
  592.         case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
  593.         case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
  594.         case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
  595.       }
  596.       showStrip();
  597.       if (serialEvent() != false)break;
  598.     }
  599.     // Fade OUT
  600.     for (int k = 255; k >= 0; k--) {
  601.       switch (j) {
  602.         case 0: setAll(k, 0, 0); if (serialEvent() != false) break; break;
  603.         case 1: setAll(0, k, 0); if (serialEvent() != false) break; break;
  604.         case 2: setAll(0, 0, k); if (serialEvent() != false) break; break;
  605.       }
  606.       showStrip();
  607.       if (serialEvent() != false)break;
  608.     }
  609.     if (serialEvent() != false)break;
  610.   }
  611. }
  612. void showStrip() {
  613. #ifdef ADAFRUIT_NEOPIXEL_H
  614.   // NeoPixel
  615.   strip.show();
  616. #endif
  617. #ifndef ADAFRUIT_NEOPIXEL_H
  618.   // FastLED
  619.   FastLED.show();
  620. #endif
  621. }
  622. void setPixel(int Pixel, byte red, byte green, byte blue) {
  623. #ifdef ADAFRUIT_NEOPIXEL_H
  624.   // NeoPixel
  625.   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
  626. #endif
  627. #ifndef ADAFRUIT_NEOPIXEL_H
  628.   // FastLED
  629.   leds[Pixel].r = red;
  630.   leds[Pixel].g = green;
  631.   leds[Pixel].b = blue;
  632. #endif
  633. }
  634. void setAll(byte red, byte green, byte blue) {
  635.   for (int i = 0; i < NUM_LEDS; i++ ) {
  636.     setPixel(i, red, green, blue);
  637.   }
  638.   showStrip();
  639. }
  640. bool serialEvent() {
  641.   if (Serial.available()) {
  642.     veri = Serial.readString();
  643.     if (veri.charAt(0) == 'b') {
  644.       veri = veri.substring(1, 4);
  645.       parlaklik = veri.toInt();
  646.       parlaklik = map(parlaklik, 0, 100, 0, 255);
  647.       strip.setBrightness(parlaklik);
  648.     }
  649.     else {
  650.       mod = veri.toInt();
  651.       return true;
  652.     }
  653.   }
  654.   return false;
  655. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 15:57:17

实验串口返回情况


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

使用道具 举报

驴友花雕  中级技神
 楼主|

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

串口输入20,随机显示不同效果


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

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2022-10-23 16:01:28

使用PC端控制软件,有二种控制WS2812B的方式
1、连接串口后,在底部窗口输入1-21,转换显示效果

2、点击16个效果按钮

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

使用道具 举报

驴友花雕  中级技神
 楼主|

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

实验场景图

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

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

回复

使用道具 举报

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

本版积分规则

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

硬件清单

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

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

mail