回答

收藏

零知开源——玩转WS2812B灯条模块

消费电子 消费电子 1272 人阅读 | 0 人回复 | 2025-02-19

WS2812RGB灯带
  通过零知标准开发板平台上驱动WS2812RGB灯珠,包括WS2812B的供电电压、接线和代码实现。通过创建延时函数和设置级联数据,实现对RGB灯珠的控制,展示了从关闭到开启不同颜色以及跑马灯、呼吸灯和彩虹灯等效果。

一、工具原料
电脑、Windows系统
零知开发板
Micro-usb线
WS2812RGB灯

二、硬件连接
零知标准开发板
WS2812B
5V
VCC
GND
GND
11
Din
硬件连接示意图
实际连接


三、方法步骤
1、打开零知实验室软件开发工具,然后新建项目,输入代码
2、将库文件解压放到库文件存放的目录下
3、选择端口进行编译,然后上传到开发板中。
引用库文件WS2812B.h

  1. #include <WS2812B.h>  // 引入用于控制WS2812B LED灯带的库

  2. #define NUM_LEDS 9  // 定义灯带上的LED数量
  3. #define DELAYVAL 500 // 定义延迟时间(单位:毫秒),用于在不同效果之间的暂停时间
  4. /*
  5. * 注意:该库使用SPI1进行数据传输
  6. * 将WS2812B的数据信号输入引脚连接到开发板的MOSI引脚。
  7. */
  8. WS2812B strip = WS2812B(NUM_LEDS);  // 创建一个WS2812B对象,用于控制灯带
复制代码


设置SPI、点亮不同效果的RGB灯
  1. ​// 请注意。库中并不真正支持Gamma,它只是在本例中使用的一些函数需要Gamma时才包含
  2. uint8_t LEDGamma[] = {
  3.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
  4.     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,
  5.     1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,
  6.     2,  3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  5,  5,  5,
  7.     5,  6,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10,
  8.    10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
  9.    17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
  10.    25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
  11.    37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
  12.    51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
  13.    69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
  14.    90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
  15.   115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
  16.   144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
  17.   177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
  18.   215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255 };

  19. void setup()
  20. {
  21. strip.begin();// 设置SPI
  22. strip.show();// 清除strip,因为默认情况下条带数据设置为所有LED关闭。
  23. // strip.setBrightness(8);
  24. }

  25. void loop() {
  26. // 实现不同的灯光效果
  27. colorWipe(strip.Color(0, 255, 0), 20);  // 使用绿色逐个点亮灯带
  28. colorWipe(strip.Color(255, 0, 0), 20); // 使用红色逐个点亮灯带
  29. colorWipe(strip.Color(0, 0, 255), 20); // 使用蓝色逐个点亮灯带
  30. rainbow(10);                          // 显示彩虹渐变效果
  31. rainbowCycle(10);  // 显示循环的彩虹效果

  32. theaterChase(strip.Color(255, 0, 0), 20);  
  33. theaterChase(strip.Color(0, 255, 0), 20);
  34. theaterChase(strip.Color(0, 0, 255), 20);  
  35. theaterChaseRainbow(10);               

  36. whiteOverRainbow(20, 75, 5);  // 白光覆盖在彩虹效果上
  37. fullWhite();                  // 将灯带上的所有LED点亮为白光
  38. delay(250);                   // 暂停250毫秒
  39. rainbowFade2White(3, 3, 1);   // 彩虹效果逐渐过渡到白光
  40. theaterChaseWhiteAndOff(DELAYVAL); // 实现白光追逐与关闭的效果
  41. delay(250);

  42. flowingLight(100); // 实现流动光效果,速度为100ms
  43. breathLight(255, 50); // PWM呼吸灯效果
  44. }
复制代码

将呼吸灯、彩虹灯和流水灯等功能的库函数封装

  1. void breathLight(uint8_t maxBrightness, uint8_t speed) {
  2. // 实现PWM呼吸灯效果
  3. for (uint8_t brightness = 0; brightness <= maxBrightness; brightness++) {
  4. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  5. // 设置每个LED的颜色,使用伽马校正后的亮度值
  6. strip.setPixelColor(i, strip.Color(LEDGamma[brightness], LEDGamma[brightness], LEDGamma[brightness]));
  7.         }
  8. strip.show();
  9. delay(speed); // 延迟以实现亮度变化
  10.     }
  11. for (uint8_t brightness = maxBrightness; brightness > 0; brightness--) {
  12. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  13. strip.setPixelColor(i, strip.Color(LEDGamma[brightness], LEDGamma[brightness], LEDGamma[brightness]));
  14.         }
  15. strip.show();
  16. delay(speed);
  17. }
  18. }

  19. void flowingLight(uint8_t speed) {
  20. // 实现流动光效果
  21. uint16_t index = 0;        // 记录当前流动的索引
  22. uint8_t currentColor = 0;  // 记录当前的颜色(红、绿、蓝)

  23. while (true) {
  24. for (uint16_t i = 0; i < NUM_LEDS; i++) {
  25. // 根据当前索引计算目标索引
  26. uint16_t targetIndex = (i + index) % NUM_LEDS;
  27.      
  28. switch (currentColor) {
  29. case 0:
  30. strip.setPixelColor(targetIndex, strip.Color(255, 0, 0)); //  设置为红色
  31. break;
  32. case 1:
  33. strip.setPixelColor(targetIndex, strip.Color(0, 255, 0)); //  设置为绿色
  34. break;
  35. case 2:
  36. strip.setPixelColor(targetIndex, strip.Color(0, 0, 255)); //  设置为蓝色
  37. break;
  38.       }

  39. strip.setPixelColor(i, strip.Color(0, 0, 0)); // 将之前的LED关闭
  40.      
  41. strip.show();
  42. delay(speed);// 设置流动光的速度
  43.     }
  44.    
  45. // 切换到下一个颜色
  46. currentColor = (currentColor + 1) % 3;
  47.    
  48. // 增加索引,实现流动效果
  49. index++;
  50. if (index >= NUM_LEDS) {
  51. index = 0; //重置索引 }
  52. }
  53. }

  54. void theaterChaseWhiteAndOff(uint8_t wait) {
  55. for (int j = 0; j < 10; j++) {  // 10 cycles of chasing
  56. for (int q = 0; q < 3; q++) {
  57. for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  58. strip.setPixelColor(i + q, (j % 2 == 0 ? strip.Color(128, 0, 128) : 0));
  59.       }
  60. strip.show();
  61. delay(wait);
  62. for (uint16_t i = 0; i < strip.numPixels(); i = i + 3) {
  63. strip.setPixelColor(i + q, 0);  // Turn every third pixel off
  64.    }
  65.   }
  66. }
  67. }

  68. // Fill the dots one after the other with a color
  69. void colorWipe(uint32_t c, uint8_t wait)
  70. {
  71. for(uint16_t i=0; i<strip.numPixels(); i++)
  72. {
  73. strip.setPixelColor(i, c);
  74. strip.show();
  75. delay(wait);
  76. }
  77. }

  78. // rainbow 函数: 实现彩虹效果的灯带动画
  79. // 参数:
  80. //   wait: 每次更新彩虹效果的等待时间(毫秒)
  81. void rainbow(uint8_t wait) {
  82. uint16_t i, j;

  83. // 外层循环控制彩虹的移动效果
  84. for(j = 0; j < 256; j++) {
  85. // 内层循环逐个像素更新颜色
  86. for(i = 0; i < strip.numPixels(); i++) {
  87. // 通过 Wheel 函数生成彩虹颜色
  88. strip.setPixelColor(i, Wheel((i + j) & 255));
  89.         }
  90. strip.show(); // 更新灯带显示
  91. delay(wait);  // 等待一段时间
  92. }
  93. }

  94. // rainbowCycle 函数: 实现彩虹效果循环播放,每个像素显示的颜色均匀分布
  95. // 参数:
  96. //   wait: 每次更新动画的等待时间(毫秒)
  97. void rainbowCycle(uint8_t wait) {
  98. uint16_t i, j;

  99. // 外层循环控制彩虹的循环次数,5次完整循环
  100. for(j = 0; j < 256 * 5; j++) {
  101. // 内层循环更新每个像素的颜色
  102. for(i = 0; i < strip.numPixels(); i++) {
  103. // 使用 Wheel 函数计算彩虹颜色,保证均匀分布
  104. strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  105.         }
  106. strip.show(); // 更新灯带显示
  107. delay(wait);  // 等待一段时间
  108. }
  109. }

  110. // Wheel 函数: 根据输入值生成渐变的 RGB 颜色
  111. // 参数:
  112. //   WheelPos: 输入值(范围 0-255),表示颜色的渐变位置
  113. // 返回值:
  114. //   对应的 RGB 颜色值(以 32 位整数形式表示)
  115. uint32_t Wheel(byte WheelPos) {
  116. if(WheelPos < 85) {
  117. // 红色逐渐变为绿色
  118. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  119. } else if(WheelPos < 170) {
  120. // 绿色逐渐变为蓝色
  121. WheelPos -= 85;
  122. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  123. } else {
  124. // 蓝色逐渐变为红色
  125. WheelPos -= 170;
  126. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  127. }
  128. }

  129. // theaterChase 函数: 实现戏剧风格的爬行灯效果,每三个像素作为一个循环
  130. // 参数:
  131. //   c: 颜色值(RGB 格式)
  132. //   wait: 每次更新动画的等待时间(毫秒)
  133. void theaterChase(uint32_t c, uint8_t wait) {
  134. // 外层循环控制效果的整体循环次数
  135. for(int j = 0; j < 10; j++) {
  136. // 中间循环负责调整动画的偏移量
  137. for(int q = 0; q < 3; q++) {
  138. // 内层循环设置每第三个像素的颜色
  139. for(uint16_t i = 0; i < strip.numPixels(); i += 3) {
  140. strip.setPixelColor(i + q, c); // 设置当前像素为指定颜色
  141.             }
  142. strip.show(); // 更新灯带显示
  143. delay(wait);  // 等待一段时间

  144. // 关闭每第三个像素
  145. for(uint16_t i = 0; i < strip.numPixels(); i += 3) {
  146. strip.setPixelColor(i + q, 0);
  147.    }
  148.   }
  149. }
  150. }

  151. // theaterChaseRainbow 函数: 实现戏剧风格的彩虹爬行灯效果
  152. // 参数:
  153. //   wait: 每次更新动画的等待时间(毫秒)
  154. void theaterChaseRainbow(uint8_t wait) {
  155. // 外层循环控制颜色的变化
  156. for(int j = 0; j < 256; j++) {
  157. // 中间循环控制动画的偏移
  158. for(int q = 0; q < 3; q++) {
  159. // 内层循环设置每第三个像素的彩虹颜色
  160. for(uint16_t i = 0; i < strip.numPixels(); i += 3) {
  161. strip.setPixelColor(i + q, Wheel((i + j) % 255));
  162. }
  163. strip.show(); // 更新灯带显示
  164. delay(wait);  // 等待一段时间

  165. // 关闭每第三个像素
  166. for(uint16_t i = 0; i < strip.numPixels(); i += 3) {
  167. strip.setPixelColor(i + q, 0);
  168.    }
  169.   }
  170. }
  171. }

  172. void pulseWhite(uint8_t wait) {
  173. // 功能:实现白色脉冲效果
  174. // 参数:wait - 每次更新动画的等待时间(毫秒)

  175. for(int j = 0; j < 256 ; j++){ // 亮度从0渐变至255
  176. for(uint16_t i=0; i<strip.numPixels(); i++) {
  177. // 设置每个LED的颜色,亮度根据j的值变化
  178. strip.setPixelColor(i, strip.Color(LEDGamma[j],LEDGamma[j],LEDGamma[j] ) );
  179. }  
  180. delay(wait);
  181. strip.show();
  182. }

  183. for(int j = 255; j >= 0 ; j--){ // 亮度从255渐变回0
  184. for(uint16_t i=0; i<strip.numPixels(); i++) {
  185. strip.setPixelColor(i, strip.Color(LEDGamma[j],LEDGamma[j],LEDGamma[j] ) );
  186. }
  187. delay(wait);
  188. strip.show();
  189. }
  190. }

  191. void rainbowFade2White(uint8_t wait, int rainbowLoops, int whiteLoops) {
  192.   // 功能:先显示彩虹效果,然后逐渐过渡到白色
  193.   // 参数:
  194.   //   wait - 每次更新动画的等待时间(毫秒)
  195.   //   rainbowLoops - 彩虹循环次数
  196.   //   whiteLoops - 白色循环次数

  197. float fadeMax = 100.0;
  198. int fadeVal = 0;
  199. uint32_t wheelVal;
  200. int redVal, greenVal, blueVal;

  201. for(int k = 0 ; k < rainbowLoops ; k ++){ // 循环执行彩虹效果
  202. for(int j=0; j<256; j++) {
  203. for(int i=0; i< strip.numPixels(); i++) {
  204. wheelVal = Wheel(((i * 256 / strip.numPixels()) + j) & 255);
  205. // 逐步调整RGB值以实现渐变效果
  206. redVal = red(wheelVal) * float(fadeVal/fadeMax);
  207. greenVal = green(wheelVal) * float(fadeVal/fadeMax);
  208. blueVal = blue(wheelVal) * float(fadeVal/fadeMax);
  209. strip.setPixelColor( i, strip.Color( redVal, greenVal, blueVal ) );
  210. }
  211. if(k == 0 && fadeVal < fadeMax-1) {
  212. // 第一次循环,亮度逐渐增加
  213. fadeVal++;
  214. } else if(k == rainbowLoops - 1 && j > 255 - fadeMax){
  215. // 最后一次循环,亮度逐渐减少
  216. fadeVal--;
  217. }
  218. strip.show();
  219. delay(wait);
  220. }
  221.   }

  222. delay(500);

  223. for(int k = 0 ; k < whiteLoops ; k ++){ // 循环执行白色渐变效果
  224. for(int j = 0; j < 256 ; j++){
  225. for(uint16_t i=0; i < strip.numPixels(); i++) {
  226. strip.setPixelColor(i, strip.Color(LEDGamma[j],LEDGamma[j],LEDGamma[j] ) );
  227. }
  228. strip.show();
  229. delay(wait);
  230. }
  231. delay(2000);
  232. for(int j = 255; j >= 0 ; j--){
  233. for(uint16_t i=0; i < strip.numPixels(); i++) {
  234. strip.setPixelColor(i, strip.Color(LEDGamma[j],LEDGamma[j],LEDGamma[j] ) );
  235. }
  236. strip.show();
  237. delay(wait);
  238.   }
  239. }
  240. }


  241. void whiteOverRainbow(uint8_t wait, uint8_t whiteSpeed, uint8_t whiteLength ) {
  242.   // 功能:在彩虹背景上叠加白色的流动效果
  243.   // 参数:
  244.   //   wait - 动画更新间隔
  245.   //   whiteSpeed - 白色流动速度
  246.   //   whiteLength - 白色光束长度
  247. if(whiteLength >= strip.numPixels()) whiteLength = strip.numPixels() - 1;

  248.   int head = whiteLength - 1;
  249.   int tail = 0;

  250.   int loops = 3;
  251.   int loopNum = 0;

  252. static unsigned long lastTime = 0;


  253. while(true){
  254.   for(int j=0; j<256; j++) {
  255.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  256.   if((i >= tail && i <= head) || (tail > head && i >= tail) || (tail > head && i <= head) ){
  257.    // 如果在白色光束范围内,设置为白色
  258.    strip.setPixelColor(i, strip.Color(255,255,255 ) );
  259.    }
  260.   else{
  261.    // 否则,根据j值设置彩虹颜色
  262.    strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  263.    }
  264. }

  265. if(millis() - lastTime > whiteSpeed) {
  266. head++;
  267. tail++;
  268. if(head == strip.numPixels()){
  269.   loopNum++;
  270. }
  271.   lastTime = millis();
  272. }

  273. if(loopNum == loops) return;
  274.   head%=strip.numPixels();
  275.   tail%=strip.numPixels();
  276.   strip.show();
  277.   delay(wait);
  278.   }
  279. }
  280. }

  281. void fullWhite() {
  282.   // 功能:点亮所有LED为白色
  283.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  284. strip.setPixelColor(i, strip.Color(255, 255, 255 ) );
  285. }
  286. strip.show();
  287. }

  288. // red 函数: 提取颜色值中的红色分量
  289. // 参数:
  290. //   c: RGB 颜色值(32 位整数)
  291. // 返回值:
  292. //   红色分量(0-255)
  293. uint8_t red(uint32_t c) {
  294. return (c >> 16);
  295. }

  296. // green 函数: 提取颜色值中的绿色分量
  297. // 参数:
  298. //   c: RGB 颜色值(32 位整数)
  299. // 返回值:
  300. //   绿色分量(0-255)
  301. uint8_t green(uint32_t c) {
  302. return (c >> 8);
  303. }

  304. // blue 函数: 提取颜色值中的蓝色分量
  305. // 参数:
  306. //   c: RGB 颜色值(32 位整数)
  307. // 返回值:
  308. //   蓝色分量(0-255)
  309. uint8_t blue(uint32_t c) {
  310. return (c);
  311. }
复制代码

四、成果展示
将上述代码验证后上传到零知板,就可以看到测试结果,实现流水灯、彩虹灯和呼吸灯等效果


分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条