回答

收藏

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

消费电子 消费电子 833 人阅读 | 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. ?
  2. #include <WS2812B.h>  // 引入用于控制WS2812B LED灯带的库

  3. #define NUM_LEDS 9  // 定义灯带上的LED数量
  4. #define DELAYVAL 500 // 定义延迟时间(单位:毫秒),用于在不同效果之间的暂停时间
  5. /*
  6. * 注意:该库使用SPI1进行数据传输
  7. * 将WS2812B的数据信号输入引脚连接到开发板的MOSI引脚。
  8. */
  9. 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. ?
  2. void breathLight(uint8_t maxBrightness, uint8_t speed) {
  3. // 实现PWM呼吸灯效果
  4. for (uint8_t brightness = 0; brightness <= maxBrightness; brightness++) {
  5. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  6. // 设置每个LED的颜色,使用伽马校正后的亮度值
  7. strip.setPixelColor(i, strip.Color(LEDGamma[brightness], LEDGamma[brightness], LEDGamma[brightness]));
  8.         }
  9. strip.show();
  10. delay(speed); // 延迟以实现亮度变化
  11.     }
  12. for (uint8_t brightness = maxBrightness; brightness > 0; brightness--) {
  13. for (uint16_t i = 0; i < strip.numPixels(); i++) {
  14. strip.setPixelColor(i, strip.Color(LEDGamma[brightness], LEDGamma[brightness], LEDGamma[brightness]));
  15.         }
  16. strip.show();
  17. delay(speed);
  18. }
  19. }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  223. delay(500);

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


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

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

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

  253. static unsigned long lastTime = 0;


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

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

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

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

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

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

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

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


分享到:
回复

使用道具 举报

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

本版积分规则

511 积分
18 主题
+ 关注
热门推荐
关闭

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