风筝
发表于: 2020-2-3 19:15:02 | 显示全部楼层

LED幻彩灯带上面装有小型LED模块。 WS2812B是这些LED模块之一,它具有级联端口传输机制,只需要一根线。


这意味着多个WS2812B可以级联,并且可以通过单个连接单独控制以进行数据传输。级联多个WS2812B模块仅需要VCC、GND和数据三根线。因此,WS2812B模块非常适合LED幻彩灯带。如今,基于WS2812B的LED像素条具有各种变体。通常,每米可购买30至144个WS2812B模块的灯带。每米144个模块时,模块之间几乎没有空间。在本篇文章中,我们将主要介绍如何对Arduino进行编程,以使用FastLED库控制基于WS2812B的LED幻彩灯带。 FastLED是一个库,可以控制所有类型的LED灯条(WS2810、WS2811、LPD8806、Neopixel等)。


所需的材料清单:

–  Arduino Nano

–  跳线

–  面包板

–  基于WS2812B的LED幻彩灯带

ws2812b_variants.jpg


LED幻彩灯带的连接:

接线设置非常容易,因为LED幻彩灯带只有三个必须连接的输入引脚。通常,这些输入引脚代表GND、Vdd 5V和数据连接。我的LED幻彩灯带有三根线:白色(GND)、红色(Vdd)和绿色(Data)。输入引脚的颜色在厂商之间可能有所不同。因此,请查看购买的LED幻彩灯带的描述或数据表。

Arduino可以为带有三十个WS2812B模块的LED灯带提供足够的电源。因此,我们可以将GND引脚和5V引脚直接直接连接到白线和红线。根据一些论坛帖子的介绍,您可以从Arduino的5V引脚获得大约400mA的电流。最后但并非最不重要的一点是,我们将数据引脚(绿线)连接到Arduino的引脚3,您可以使用任何支持PWM信号的引脚。

fritzing_led_strip.png

将Arduino Nano连接至LED幻彩灯带的示意图。


示例源代码:

如前所述,该程序利用FastLED库来控制LED幻彩灯带。首先,包含FastLED头文件。在setup函数中,初始化LED幻彩灯带。由于FastLED支持很多的LED灯条(不同的LED模块,不同的长度等),因此初始化需要LED模块类型、用于数据传输的Arduino引脚号、颜色顺序、对应表示LED的数组的引用以及编号的LED。也可以设置颜色校正以便改善颜色的保真度。

接下来,有一个关闭所有像素的函数(“ showProgramCleanUp”)。此函数对于清空LED灯条颜色非常有用。

示例源代码包含三个不同的“灯光程序”。每个灯光程序均作为一个功能实现。第一个程序以随机​​选择的颜色(“ showProgramRandom”)打开所有LED。下一个程序显示单色像素从LED灯带的开始移到结尾(“ showProgramShiftSinglePixel”)。最后一个程序显示了多个像素如何从头移动到结尾(“ showProgramShiftMultiPixel”)。在此灯光程序中,每个像素都有一个随机选择的颜色。

在loop函数中,每个灯光程序使用不同的参数调用两次。在对调用灯光程序功能的函数之前,通过调用特殊程序“ showProgramCleanUp”来关闭LED灯带的所有像素。

  1. #include "FastLED.h"
  2. #define DATA_PIN 3
  3. #define LED_TYPE WS2812B
  4. #define COLOR_ORDER GRB
  5. #define NUM_LEDS 30
  6. #define BRIGHTNESS 96
  7. CRGB leds[NUM_LEDS];
  8. void setup() {
  9.   delay(3000); // initial delay of a few seconds is recommended
  10.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // initializes LED strip
  11.   FastLED.setBrightness(BRIGHTNESS);// global brightness
  12. }
  13. // switches off all LEDs
  14. void showProgramCleanUp(long delayTime) {
  15.   for (int i = 0; i < NUM_LEDS; ++i) {
  16.     leds[i] = CRGB::Black;
  17.   }
  18.   FastLED.show();
  19.   delay(delayTime);
  20. }
  21. // switches on all LEDs. Each LED is shown in random color.
  22. // numIterations: indicates how often LEDs are switched on in random colors
  23. // delayTime: indicates for how long LEDs are switched on.
  24. void showProgramRandom(int numIterations, long delayTime) {
  25.   for (int iteration = 0; iteration < numIterations; ++iteration) {
  26.     for (int i = 0; i < NUM_LEDS; ++i) {
  27.       leds[i] = CHSV(random8(),255,255); // hue, saturation, value
  28.     }
  29.     FastLED.show();
  30.     delay(delayTime);
  31.   }
  32. }
  33. // Shifts a single pixel from the start of strip to the end.
  34. // crgb: color of shifted pixel
  35. // delayTime: indicates how long the pixel is shown on each LED
  36. void showProgramShiftSinglePixel(CRGB crgb, long delayTime) {
  37.   for (int i = 0; i < NUM_LEDS; ++i) {
  38.     leds[i] = crgb;
  39.     FastLED.show();
  40.     delay(delayTime);
  41.     leds[i] = CRGB::Black;
  42.   }
  43. }
  44. // Shifts multiple pixel from the start of strip to the end. The color of each pixel is randomized.
  45. // delayTime: indicates how long the pixels are shown on each LED
  46. void showProgramShiftMultiPixel(long delayTime) {
  47.   for (int i = 0; i < NUM_LEDS; ++i) {
  48.     for (int j = i; j > 0; --j) {
  49.       leds[j] = leds[j-1];
  50.     }
  51.     CRGB newPixel = CHSV(random8(), 255, 255);
  52.     leds[0] = newPixel;
  53.     FastLED.show();
  54.     delay(delayTime);
  55.   }
  56. }
  57. // main program
  58. void loop() {
  59.   showProgramCleanUp(2500); // clean up
  60.   showProgramRandom(100, 100); // show "random" program
  61.   
  62.   showProgramCleanUp(2500); // clean up
  63.   showProgramRandom(10, 1000); // show "random" program
  64.   
  65.   showProgramCleanUp(2500); // clean up
  66.   showProgramShiftSinglePixel(CRGB::Blue, 250); // show "shift single pixel program" with blue pixel
  67.   
  68.   showProgramCleanUp(2500); // clean up
  69.   showProgramShiftSinglePixel(CRGB::Maroon, 100); // show "shift single pixel program" with maroon pixel
  70.   
  71.   showProgramCleanUp(2500); // clean up
  72.   showProgramShiftMultiPixel(500); // show "shift multi pixel" program
  73.   
  74.   showProgramCleanUp(2500); // clean up
  75.   showProgramShiftMultiPixel(50); // show "shift multi pixel" program
  76. }
复制代码

编译源代码并将其传输到Arduino后,LED灯带应会根据活动的灯光程序显示彩色像素。

led_strip_color.jpg

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1477



手机版|

GMT+8, 2024-3-19 10:13 , Processed in 0.101805 second(s), 8 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表