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

自发电无线开关(三)炫彩灯环开关

[复制链接]
本帖最后由 云天 于 2022-10-2 19:52 编辑

自发电无线开关(三)炫彩灯环开关图1


【项目设计】
使用自发电无线开关,控制变换灯环炫彩效果。
【硬件】
主板使用Arduino,灯环为93灯/6环 WS2812B RGB 圆环灯。
自发电无线开关(三)炫彩灯环开关图2

【fastled库】
FastLED 是一款功能强大,简单易用的控制WS2812, LPD8806, 等LED光带的Arduino第三方库。 目前FastLED是公认的Arduino开发者应用最为广泛的LED控制库之一。下载地址:https://github.com/FastLED/FastLED/releases
【库安装】
使用“加载库”,加载库文件“FastLED-3.5.0.zip”。
自发电无线开关(三)炫彩灯环开关图3

【测试】
1、点亮一个灯,灯环接12引脚。
  1. #include <FastLED.h>
  2. #define NUM_LEDS 1
  3. #define DATA_PIN 12
  4. #define CLOCK_PIN 13
  5. CRGB leds[NUM_LEDS];
  6. void setup() {
  7.     FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed
  8. }
  9. void loop() {
  10.   leds[0] = CRGB::Red;
  11.   FastLED.show();
  12.   delay(500);
  13.   leds[0] = CRGB::Black;
  14.   FastLED.show();
  15.   delay(500);
  16. }
复制代码
2、同时点亮两个灯环,两灯环分别接12,11引脚。
  1. #define DATA_PIN1    12
  2. #define DATA_PIN2    11
  3. void setup() {
  4.   FastLED.addLeds<LED_TYPE,DATA_PIN1,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  5.   FastLED.addLeds<LED_TYPE,DATA_PIN2,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  6. }
复制代码
【制作灯盒】

自发电无线开关(三)炫彩灯环开关图4


自发电无线开关(三)炫彩灯环开关图5



【程序代码】
自发电无线开关(三)炫彩灯环开关图6

  1. #include <FastLED.h>
  2. FASTLED_USING_NAMESPACE
  3. #define DATA_PIN1    12
  4. #define DATA_PIN2    11
  5. //#define CLK_PIN   4
  6. #define LED_TYPE    WS2811
  7. #define COLOR_ORDER GRB
  8. #define NUM_LEDS    93
  9. CRGB leds[NUM_LEDS];
  10. #define BRIGHTNESS          96
  11. #define FRAMES_PER_SECOND  120
  12. CRGBPalette16 gPal;
  13. bool gReverseDirection = false;
  14. int bs;
  15. void setup() {
  16.   bs=0;
  17.   delay(3000); // 3 second delay for recovery
  18.   gPal = HeatColors_p;
  19.   FastLED.addLeds<LED_TYPE,DATA_PIN1,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  20.   FastLED.addLeds<LED_TYPE,DATA_PIN2,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  21.   FastLED.setBrightness(BRIGHTNESS);
  22. }
  23. typedef void (*SimplePatternList[])();
  24. SimplePatternList gPatterns = { rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm };
  25. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  26. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  27.   
  28. void loop()
  29. {
  30.    if (digitalRead(4)) {
  31.     bs += 1;
  32.     if(bs==3){
  33.       bs=0;
  34.     }
  35.     delay(3000);
  36.   }
  37.   if(bs==0){
  38.     random16_add_entropy( random());
  39.     Fire2012WithPalette(); // run simulation frame, using palette colors
  40.   }
  41.   if(bs==1){
  42.     pride();
  43.   }
  44.   if(bs==2){
  45.      gPatterns[gCurrentPatternNumber]();
  46.   }
  47.   FastLED.show();  
  48.   FastLED.delay(1000/FRAMES_PER_SECOND);
  49.    
  50.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  51.   EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  52. }
  53. #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
  54. void nextPattern()
  55. {
  56.   gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
  57. }
  58. void rainbow()
  59. {
  60.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  61. }
  62. void rainbowWithGlitter()
  63. {
  64.   rainbow();
  65.   addGlitter(80);
  66. }
  67. void addGlitter( fract8 chanceOfGlitter)
  68. {
  69.   if( random8() < chanceOfGlitter) {
  70.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  71.   }
  72. }
  73. void confetti()
  74. {
  75.   fadeToBlackBy( leds, NUM_LEDS, 10);
  76.   int pos = random16(NUM_LEDS);
  77.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  78. }
  79. void sinelon()
  80. {
  81.   fadeToBlackBy( leds, NUM_LEDS, 20);
  82.   int pos = beatsin16( 13, 0, NUM_LEDS-1 );
  83.   leds[pos] += CHSV( gHue, 255, 192);
  84. }
  85. void bpm()
  86. {
  87.   uint8_t BeatsPerMinute = 62;
  88.   CRGBPalette16 palette = PartyColors_p;
  89.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  90.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  91.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  92.   }
  93. }
  94. void juggle() {
  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. }
  102. void pride()
  103. {
  104.   static uint16_t sPseudotime = 0;
  105.   static uint16_t sLastMillis = 0;
  106.   static uint16_t sHue16 = 0;
  107.   uint8_t sat8 = beatsin88( 87, 220, 250);
  108.   uint8_t brightdepth = beatsin88( 341, 96, 224);
  109.   uint16_t brightnessthetainc16 = beatsin88( 203, (25 * 256), (40 * 256));
  110.   uint8_t msmultiplier = beatsin88(147, 23, 60);
  111.   uint16_t hue16 = sHue16;//gHue * 256;
  112.   uint16_t hueinc16 = beatsin88(113, 1, 3000);
  113.   
  114.   uint16_t ms = millis();
  115.   uint16_t deltams = ms - sLastMillis ;
  116.   sLastMillis  = ms;
  117.   sPseudotime += deltams * msmultiplier;
  118.   sHue16 += deltams * beatsin88( 400, 5,9);
  119.   uint16_t brightnesstheta16 = sPseudotime;
  120.   
  121.   for( uint16_t i = 0 ; i < NUM_LEDS; i++) {
  122.     hue16 += hueinc16;
  123.     uint8_t hue8 = hue16 / 256;
  124.     brightnesstheta16  += brightnessthetainc16;
  125.     uint16_t b16 = sin16( brightnesstheta16  ) + 32768;
  126.     uint16_t bri16 = (uint32_t)((uint32_t)b16 * (uint32_t)b16) / 65536;
  127.     uint8_t bri8 = (uint32_t)(((uint32_t)bri16) * brightdepth) / 65536;
  128.     bri8 += (255 - brightdepth);
  129.    
  130.     CRGB newcolor = CHSV( hue8, sat8, bri8);
  131.    
  132.     uint16_t pixelnumber = i;
  133.     pixelnumber = (NUM_LEDS-1) - pixelnumber;
  134.    
  135.     nblend( leds[pixelnumber], newcolor, 64);
  136.   }
  137. }
  138. #define COOLING  55
  139. #define SPARKING 120
  140. void Fire2012WithPalette()
  141. {
  142.   static uint8_t heat[NUM_LEDS];
  143.     for( int i = 0; i < NUM_LEDS; i++) {
  144.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  145.     }
  146.   
  147.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  148.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  149.     }
  150.    
  151.     if( random8() < SPARKING ) {
  152.       int y = random8(7);
  153.       heat[y] = qadd8( heat[y], random8(160,255) );
  154.     }
  155.     for( int j = 0; j < NUM_LEDS; j++) {
  156.       uint8_t colorindex = scale8( heat[j], 240);
  157.       CRGB color = ColorFromPalette( gPal, colorindex);
  158.       int pixelnumber;
  159.       if( gReverseDirection ) {
  160.         pixelnumber = (NUM_LEDS-1) - j;
  161.       } else {
  162.         pixelnumber = j;
  163.       }
  164.       leds[pixelnumber] = color;
  165.     }
  166. }
复制代码


自发电无线开关(三)炫彩灯环开关图7


【演示视频】

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

本版积分规则

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

硬件清单

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

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

mail