感谢您的期待!
我正在每个臂上构建一个四轴飞行器,我已经在其上放置了一条RGB可寻址LED。我使用Arduino来驱动灯光,Arduino代码是C ++,这是一种我不太了解的语言。
Here is the first StackOverflow question我发布了有关此代码的上一个问题。它为您提供了更多关于我正在尝试做什么的背景(如果您有兴趣)。
由于first question的答案,我现在已正确编写了数组“gpsHoldArr”,但我无法访问它的值。
在下面的代码中,我调用toggleLights(gpsHoldArr[x][y])
并传入gpsHoldArr
的子数组。子阵列应该是指向给定LED条带([x]
)然后指向给定步骤([y]
)的结果。
toggleLights
然后应该迭代它传递的数组,并将每个LED的值(1-6中的某个数字)发送到控制台上,并将LED的红色,绿色和蓝色值发送到控制台。
不幸的是,当我运行下面的代码时,我收到此错误:
cannot convert int(*)[3] to int* for argument 1 to 'void toggleLights(int*)'
非常感谢任何帮助。
//4 arms, 6 steps, 6 leds
int gpsHoldArr[4][6][6][3] = {
{
{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
{
{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
{
{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
},
{
{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {0,0,0}},
{{255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}, {255,0,0}}
}
};
toggleLights(gpsHoldArr[0][0]); //Toggles lights on strip #1, step #1
toggleLights(gpsHoldArr[1][0]); //Toggles lights on strip #2, step #1
toggleLights(gpsHoldArr[2][0]); //Toggles lights on strip #3, step #1
toggleLights(gpsHoldArr[3][0]); //Toggles lights on strip #4, step #1
void toggleLights(int lights[]){
for(int i = 0; i <= 6; ++i)
{
set_color_led(i, lights[i], lights[i], lights[i]);
}
}
void set_color_led(int led, int r, int g, int b){
Serial.println(led); //Which LED (or "pixel") is it?
Serial.println(r); //What is the red value?
Serial.println(g); //What is the green value?
Serial.println(b); //What is the blue value?
}
答案 0 :(得分:2)
我可能会这样做,在此过程中删除一些重复 (宏诡计只是因为Arduino - 在桌面上我会使用类而不是数组。)
struct LED { int r, g, b; };
#define BLACK {0, 0, 0}
#define RED {255, 0, 0}
#define DEFAULT_LEDS \
{ {RED, BLACK, BLACK, BLACK, BLACK, BLACK},\
{RED, RED, BLACK, BLACK, BLACK, BLACK},\
{RED, RED, RED, BLACK, BLACK, BLACK},\
{RED, RED, RED, RED, BLACK, BLACK},\
{RED, RED, RED, RED, RED, BLACK},\
{RED, RED, RED, RED, RED, RED}}
LED gpsHoldArr[4][6][6] = {
DEFAULT_LEDS,
DEFAULT_LEDS,
DEFAULT_LEDS,
DEFAULT_LEDS
};
void set_color_led(int index, const LED& led){
Serial.println(index); //Which LED (or "pixel") is it?
Serial.println(led.r); //What is the red value?
Serial.println(led.g); //What is the green value?
Serial.println(led.b); //What is the blue value?
}
void toggleLights(LED (&leds)[6]){
for(int i = 0; i < 6; ++i) // You had a '<=' bug here.
{
set_color_led(i, leds[i]);
}
}
toggleLights(gpsHoldArr[0][0]); //Toggles lights on strip #1, step #1
答案 1 :(得分:1)
当你的函数存在一维数组(衰变为一个指针)时,你传入二维数组。
我可以通过制作结构/类来建议消除数组dimmensions,它会使事情更清晰。
例如
struct Led{
int r,g,b;
};
void toggleLights(Led lights[]){
Led gpsHoldArr[4][6][6] =
set_color_led(i, lights[i].r, lights[i].g, lights[i].b);
这应该是您需要进行的所有更改,其余的 应该按原样运行。
你可以更进一步,做一个手臂struct
和步骤struct
。
答案 2 :(得分:1)
我不认为toggleLights()
正在按照您的想法行事。它的输入是一维数组,但你传递的是一个大小为二维的数组[6] [3]。调用toggleLights(gpsHoldArr[0][0]);
时,函数看到的1-D内存数组为{255,0,0,0,0,0}
,即数组中的前六个值。然后,对于这些值中的每一个,您调用set_color_led();
并为多个参数传递相同的值。在toggleLights()
中展开循环,转换为
// set_color_led(i, lights[i], lights[i], lights[i]) for i = {0, ..., 6}
set_color_led(0, 255, 255, 255);
set_color_led(1, 0, 0, 0);
set_color_led(2, 0, 0, 0);
set_color_led(3, 0, 0, 0);
set_color_led(4, 0, 0, 0);
set_color_led(5, 0, 0, 0);
set_color_led(6, 0, 0, 0); // bug here as noted by molbdnilo
这可能不是你想要的。我会将toggleLights()
的定义更改为以下内容:
void toggleLights(int lights[][3]){
for(int i = 0; i < 6; ++i)
{
set_color_led(i, lights[i][0], lights[i][1], lights[i][2]);
}
}
在这种情况下,当调用toggleLights(gpsHoldArr[0][0]);
时,此函数看到的二维数组是
{{255,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}}
并在toggleLights()
中展开循环,这转换为以下一系列函数调用:
// set_color_led(i, lights[i][0], lights[i][1], lights[i][2]) for i = {0, ..., 5}
set_color_led(0, 255, 0, 0);
set_color_led(1, 0, 0, 0);
set_color_led(2, 0, 0, 0);
set_color_led(3, 0, 0, 0);
set_color_led(4, 0, 0, 0);
set_color_led(5, 0, 0, 0);