如何在Arduino/C++中通过函数参数切换控制不同LED数组
问题描述
我正在基于Arduino开发LED灯带项目,使用FastLED.h库控制WS2812可寻址LED。项目中Arduino Mega控制器连接了两条LED灯带,分别对应引脚4和5,对应CRGB数组leds1和leds2。我希望复用函数void a()来控制这两个数组,但当前函数仅固定控制leds1。我尝试通过传递字符参数拼接数组名的方式切换到leds2,代码无语法错误但无法点亮LED,请问如何通过函数参数正确切换控制的LED数组?
原代码
#include <FastLED.h> #include <string.h> CRGB leds1[5]; CRGB leds2[5]; void setup() { FastLED.addLeds<WS2812, 4, GRB>(leds1, 5); FastLED.addLeds<WS2812, 5, GRB>(leds2, 10); FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); FastLED.clear(); FastLED.show(); } void loop() { num1(); } void num1(){ a(255); // single strip } void a(int value ){ int red = value; int green =0; int blue =0; leds1[0]=CRGB(red, green, blue); leds1[1]=CRGB(red, green, blue); leds1[2]=CRGB(red, green, blue); leds1[3]=CRGB(red, green, blue); leds1[4]=CRGB(red, green, blue); FastLED.setBrightness(20); FastLED.show(); }
错误尝试代码
#include <FastLED.h> #include <string.h> CRGB leds1[5]; CRGB leds2[5]; void setup() { FastLED.addLeds<WS2812, 4, GRB>(leds1, 5); FastLED.addLeds<WS2812, 5, GRB>(leds2, 10); FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); FastLED.clear(); FastLED.show(); } void loop() { num1('1'); num1('2'); } void num1(char suffix){ a(255,suffix); } void a(int value , char suffix){ int red = value; int green =0; int blue =0; char lednum[6] ; lednum[0]='l'; lednum[1]='e'; lednum[2]='d'; lednum[3]='s'; lednum[4]=suffix; lednum[5]=0; CRGB led= lednum; led[0]=CRGB(red, green, blue); led[1]=CRGB(red, green, blue); led[2]=CRGB(red, green, blue); led[3]=CRGB(red, green, blue); led[4]=CRGB(red, green, blue); FastLED.setBrightness(20); FastLED.show(); }
解决方案
C语言里不能通过字符串拼接的方式直接获取变量名对应的数组,错误尝试本质是把字符串地址赋值给了CRGB变量,完全没有操作到目标数组。正确的做法是直接传递数组指针或数组本身给函数:
方法1:传递数组指针
修改函数a(),让它接收CRGB数组的指针,可同时传递数组长度适配不同灯带:
#include <FastLED.h> CRGB leds1[5]; CRGB leds2[5]; void setup() { // 修正原代码中leds2长度参数与数组长度不符的问题 FastLED.addLeds<WS2812, 4, GRB>(leds1, 5); FastLED.addLeds<WS2812, 5, GRB>(leds2, 5); FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); FastLED.clear(); FastLED.show(); } void loop() { num1(leds1); delay(1000); num1(leds2); delay(1000); } void num1(CRGB* ledStrip) { a(255, ledStrip); } void a(int value, CRGB* ledStrip) { int red = value; int green = 0; int blue = 0; // 遍历数组给每个LED赋值 for(int i = 0; i < 5; i++){ ledStrip[i] = CRGB(red, green, blue); } FastLED.setBrightness(20); FastLED.show(); }
方法2:传递数组引用(类型更安全)
如果想在函数里明确数组长度,可使用数组引用:
#include <FastLED.h> const int STRIP_LENGTH = 5; CRGB leds1[STRIP_LENGTH]; CRGB leds2[STRIP_LENGTH]; void setup() { FastLED.addLeds<WS2812, 4, GRB>(leds1, STRIP_LENGTH); FastLED.addLeds<WS2812, 5, GRB>(leds2, STRIP_LENGTH); FastLED.setMaxPowerInVoltsAndMilliamps(5, 500); FastLED.clear(); FastLED.show(); } void loop() { num1(leds1); delay(1000); num1(leds2); delay(1000); } void num1(CRGB (&ledStrip)[STRIP_LENGTH]) { a(255, ledStrip); } void a(int value, CRGB (&ledStrip)[STRIP_LENGTH]) { int red = value; int green = 0; int blue = 0; for(int i = 0; i < STRIP_LENGTH; i++){ ledStrip[i] = CRGB(red, green, blue); } FastLED.setBrightness(20); FastLED.show(); }
关键注意事项
- 原代码中
FastLED.addLeds<WS2812,5,GRB>(leds2,10)的长度参数为10,但数组leds2仅5个元素,会导致内存越界,必须修正为匹配的长度。 - 传递数组时,C语言会自动将数组转换为指针(方法1),或通过引用明确数组类型(方法2),两种方式都能直接操作目标数组,实现函数复用。
内容的提问来源于stack exchange,提问作者yasooz
相关产品推荐
相关产品推荐

