gpt4 book ai didi

c++ - 如何创建变量数组 Arduino

转载 作者:太空宇宙 更新时间:2023-11-04 07:20:07 24 4
gpt4 key购买 nike

我将如何创建一个变量数组,例如.....

int incColor, c0, c1, c2, c3, c4, c5 = 0;
int circleArray[11][9] = { {c5, c5, c5, c5, c5, c5, c5, c5, c5},
{c4, c4, c4, c4, c4, c4, c4, c4, c4},
{c4, c3, c3, c3, c3, c3, c3, c3, c4},
{c4, c3, c2, c2, c2, c2, c2, c3, c4},
{c4, c3, c2, c1, c1, c1, c2, c3, c4},
{c4, c3, c2, c1, c0, c1, c2, c3, c4},
{c4, c3, c2, c1, c1, c1, c2, c3, c4},
{c4, c3, c2, c2, c2, c2, c2, c3, c4},
{c4, c3, c3, c3, c3, c3, c3, c3, c4},
{c4, c4, c4, c4, c4, c4, c4, c4, c4},
{c5, c5, c5, c5, c5, c5, c5, c5, c5} };

所以稍后在代码中我将能够像这样解决它们...

void circle(uint16_t mode) 
{
switch (mode)
{
case 0:
c0 = random(255);
break;
case 1:
c0 = incColor;
incColor++;
break;
}
for (int x = 0; x < 11; x++)
{
for (int y = 0; y < 9; y++)
{
strip.setPixelColor(x, y, Wheel(circleArray[x][y]));
}
}
c5 = c4;
c4 = c3;
c3 = c2;
c2 = c1;
c1 = c0;

}

上面的代码不起作用,我测试了 c5 和 circleArray[0][0]

c5 = 34
circleArray[0][0] = 0

circleArray[0][0] 应该与我认为的 c5 相同,但由于某种原因,该值未设置...

有人知道我在这里做错了什么吗?

~~~~~~~~~~~~~~编辑~~~~~~~~~~~~~~~~~

已修复!感谢@sj0h 帮助我找到了一个更简单的解决方案,所以现在我可以把它...

int c[6] = {0, 0, 0, 0, 0, 0};    
int circleArray[11][9] = { {5, 5, 5, 5, 5, 5, 5, 5, 5},
{4, 4, 4, 4, 4, 4, 4, 4, 4},
{4, 3, 3, 3, 3, 3, 3, 3, 4},
{4, 3, 2, 2, 2, 2, 2, 3, 4},
{4, 3, 2, 1, 1, 1, 2, 3, 4},
{4, 3, 2, 1, 0, 1, 2, 3, 4},
{4, 3, 2, 1, 1, 1, 2, 3, 4},
{4, 3, 2, 2, 2, 2, 2, 3, 4},
{4, 3, 3, 3, 3, 3, 3, 3, 4},
{4, 4, 4, 4, 4, 4, 4, 4, 4},
{5, 5, 5, 5, 5, 5, 5, 5, 5} };

void circle(uint16_t mode)
{
switch (mode)
{
case 0:
c[0] = random(255);
break;
case 1:
c[0] = incColor;
incColor++;
break;
}
for (int x = 0; x < 11; x++)
{
for (int y = 0; y < 9; y++)
{
strip.setPixelColor(x, y, Wheel(c[circleArray[x][y]]));
}
}
c[5] = c[4];
c[4] = c[3];
c[3] = c[2];
c[2] = c[1];
c[1] = c[0];

}

进入现实生活中的应用....

www.famousmods.com for more info

最佳答案

引用可以做你想做的事:

int &circleArray[11][9] = ......

这将使 circleArray 成为引用数组而不是复制值。


编辑:标准不支持上述内容,支持取决于编译器。

改用指针,您将拥有

int *circleArray[11][9] = {{&c5, &c5, .....

然后将所有 circleArray 访问更改为 *circleArray[x][y] 而不是 circleArray[x][y]。


另一种可能更通用的方法是将颜色数组索引保留在 circleArray 中:

int incColor;
int cn[6] = {0,0,0,0,0,0};
int circleArray[11][9] = { {5,5,5,5,5,5,5,5,5},
{4,4 .....

然后 c0 将变为 c[0],您将像这样访问数组值

cn[circleArray[x][y]]

您还可以制作 circleArray uint8_t 以节省一些空间,因为所有条目都将放在一个字节内。

关于c++ - 如何创建变量数组 Arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22083676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com