1

感谢您的关注!

背景

我正在构建一个四轴飞行器,每个手臂上都放置了一条 RGB 可寻址 LED。我正在使用 Arduino 来驱动灯,而 Arduino 代码是 C++,一种我不太了解的语言。

这是我发布的第一个 StackOverflow 问题,涉及我之前使用此代码时遇到的问题。它为您提供了有关我到底想要做什么的更多背景信息(如果您有兴趣)。

问题

由于我的第一个问题的答案,我现在已经正确地编写了数组“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? 
}
4

3 回答 3

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
于 2013-01-19T17:59:35.797 回答
1

我不认为那toggleLights()正在做你认为它正在做的事情。它的输入是一个一维数组,但您传递给它的是一个大小为 [6][3] 的二维数组。调用时toggleLights(gpsHoldArr[0][0]);,函数看到的一维内存数组是{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);
于 2013-01-19T17:17:43.963 回答
1

当您的函数存在一维数组(衰减为指针)时,您将传入一个二维数组。

我可以建议通过制作结构/类来消除数组维度,它会使事情变得更加清晰。

例如

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

于 2013-01-19T16:29:48.337 回答