2174浏览
查看: 2174|回复: 0

红红火火过大年(WS2812)

[复制链接]
红红火火过大年(WS2812)图2
过年了,家里的彩灯有些单调,想用WS2812自己做点炫酷的,自己编程有点力不从心。从网上找到一个Arduino WS2812灯带控制库FastLED库。
红红火火过大年(WS2812)图1

【参考教程】
可参考:http://www.taichi-maker.com/home ... dex/intelligent-21/
红红火火过大年(WS2812)图3
【测试例程】
  1. #include <FastLED.h>
  2. FASTLED_USING_NAMESPACE
  3. // FastLED "100-lines-of-code" demo reel, showing just a few
  4. // of the kinds of animation patterns you can quickly and easily
  5. // compose using FastLED.  
  6. //
  7. // This example also shows one easy way to define multiple
  8. // animations patterns and have them automatically rotate.
  9. //
  10. // -Mark Kriegsman, December 2014
  11. #define DATA_PIN    10
  12. //#define CLK_PIN   4
  13. #define LED_TYPE    WS2812
  14. #define COLOR_ORDER GRB
  15. #define NUM_LEDS    200
  16. CRGB leds[NUM_LEDS];
  17. #define BRIGHTNESS          96
  18. #define FRAMES_PER_SECOND  120
  19. void setup() {
  20.   delay(3000); // 3 second delay for recovery
  21.   
  22.   // tell FastLED about the LED strip configuration
  23.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  24.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  25.   // set master brightness control
  26.   FastLED.setBrightness(BRIGHTNESS);
  27. }
  28. // List of patterns to cycle through.  Each is defined as a separate function below.
  29. typedef void (*SimplePatternList[])();
  30. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  31. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  32. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  33.   
  34. void loop()
  35. {
  36.   // Call the current pattern function once, updating the 'leds' array
  37.   gPatterns[gCurrentPatternNumber]();
  38.   // send the 'leds' array out to the actual LED strip
  39.   FastLED.show();  
  40.   // insert a delay to keep the framerate modest
  41.   FastLED.delay(1000/FRAMES_PER_SECOND);
  42.   // do some periodic updates
  43.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  44.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  45. }
  46. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  47. void nextPattern()
  48. {
  49.   // add one to the current pattern number, and wrap around at the end
  50.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  51. }
  52. void rainbow()
  53. {
  54.   // FastLED's built-in rainbow generator
  55.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  56. }
  57. void rainbowWithGlitter()
  58. {
  59.   // built-in FastLED rainbow, plus some random sparkly glitter
  60.   rainbow();
  61.   addGlitter(80);
  62. }
  63. void addGlitter( fract8 chanceOfGlitter)
  64. {
  65.   if( random8() < chanceOfGlitter) {
  66.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  67.   }
  68. }
  69. void confetti()
  70. {
  71.   // random colored speckles that blink in and fade smoothly
  72.   fadeToBlackBy( leds, NUM_LEDS, 10);
  73.   int pos = random16(NUM_LEDS);
  74.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  75. }
  76. void sinelon()
  77. {
  78.   // a colored dot sweeping back and forth, with fading trails
  79.   fadeToBlackBy( leds, NUM_LEDS, 20);
  80.   int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  81.   leds[pos] += CHSV( gHue, 255, 192);
  82. }
  83. void bpm()
  84. {
  85.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  86.   uint8_t BeatsPerMinute = 62;
  87.   CRGBPalette16 palette = PartyColors_p;
  88.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  89.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  90.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  91.   }
  92. }
  93. void juggle() {
  94.   // eight colored dots, weaving in and out of sync with each other
  95.   fadeToBlackBy( leds, NUM_LEDS, 20);
  96.   uint8_t dothue = 0;
  97.   for( int i = 0; i < 8; i++) {
  98.     leds[beatsin16( i+7, 0, NUM_LEDS-1 )] |= CHSV(dothue, 200, 255);
  99.     dothue += 32;
  100.   }
  101. }
复制代码
【演示视频】

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

本版积分规则

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

硬件清单

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

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

mail