用于 Arduino 的 3D 阵列
3D array for Arduino
目前我正在研究我的 4x4x4 LED 立方体,我想为它编写自己的代码,但目前我坚持使用 3D 数组。我在 void setup() 中声明了几个数组,我也尝试将它们放在 void loop() 中。尽管如此,当尝试编译时,它仍然返回错误。
简而言之,代码应该生成一个具有XYZ值的随机点。然后它必须将其写入缓冲区,该缓冲区必须将其投影并多路复用到led立方体上。
我将返回错误的行设置为粗体。
错误是:
LedCube1.0.ino: In function 'void loop()':
LedCube1.0.ino:41:3: error: 'ledBuffer' was not declared in this scope
LedCube1.0.ino:41:13: error: 'xSeed' was not declared in this scope
LedCube1.0.ino:41:20: error: 'ySeed' was not declared in this scope
LedCube1.0.ino:41:27: error: 'zSeed' was not declared in this scope
LedCube1.0.ino:42:7: error: 'rainstep' was not declared in this scope
LedCube1.0.ino: In function 'int allOff()':
LedCube1.0.ino:76:9: error: 'ledBuffer' was not declared in this scope
LedCube1.0.ino: In function 'int allOn()':
LedCube1.0.ino:86:9: error: 'ledBuffer' was not declared in this scope
Error compiling.
代码:
void setup() {
//sets all pins as output
for(int a=22;a<53;a++){
pinMode(a, OUTPUT);
}
//declares the sizes of the cube
int width = 4;
int depth = 4;
int height = 4;
int ledBuffer[4][4][4] = { //creates a buffer that can store values generated by a function
{
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}
},
{
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}
},
{
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
},
{
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0},
}
};
//defines to which connector each layer is connected
int ledXY[4][4] = {
{22,24,26,28} ,
{30,32,34,36},
{23,25,27,29},
{31,33,35,37}
};
int ledZ[4] = {38,40,42,44};
//create variables to start generating raindrops
int rainstep = 0;
int xSeed = 0;
int ySeed = 0;
int zSeed = 0;
}
void loop() {
//generatedrop
ledBuffer[xSeed][ySeed][zSeed] = 0;
if (rainstep == 0)
{
int xSeed=random(0,3);
int ySeed=random(0,3);
int zSeed=random(0,3);
}
else
{
zSeed = zSeed - rainstep;
}
ledBuffer[xSeed][ySeed][zSeed] = 1;
//updatecube
for(int i=0; i<80;i++){
int currentZ = i%4;
allOff;
for(int j=0;j<4;j++){
for(int k=0; i<4;i++){
if(ledBuffer[i][j][k]==0){
digitalWrite(ledBuffer[i][j][k],HIGH);
}else{
digitalWrite(ledBuffer[i][j][k],LOW);
}
}
}
}
}
//function declares entire array 0
int allOff(){
for(int c=0;c<4;c++){
for(int d=0;d<4;d++){
for(int e=0;e<4;e++){
ledBuffer[c][d][e]=0;
}
}
}
};
//function declares entire array 1
int allOn(){
for(int c=0;c<4;c++){
for(int d=0;d<4;d++){
for(int e=0;e<4;e++){
ledBuffer[c][d][e]=1;
}
}
}
};
I re
有人可以帮助我或至少为我指出正确的方向。
在 void loop()
下,您将在 if
块内声明 xSeed
、ySeed
和 zSeed
,这使得变量位于该 if 块的本地变量,这意味着当您退出 if 块时它们将超出范围。在 if 块之前(外部)声明它们,在 void loop()
函数的开头ledBuffer
之前声明它们,因为您在告诉编译器它们存在之前尝试使用这些变量,因此编译器的消息是它们"未在此范围内声明"
编辑
此外,您似乎希望所有这些功能在同一个ledBuffer
3D 阵列上运行。您应该考虑将其声明为全局变量(如果您使用的是 C),或者如果您使用的是 C++,也许可以考虑将其作为一个类,将 ledBuffer
作为属性/字段。
我试图拆分您的代码并将其分解。我没有Arduino构建系统,无法测试我的代码。我不明白你把x、y、z映射到我试过的引脚号。
/* Struct for global conf */
static struct {
int ledBuffer[4][4][4];
int ledXY[4][4];
int ledZ[4];
} _G;
void setup() {
_G.ledXY = {
{22, 24, 26, 28} ,
{30, 32, 34, 36},
{23, 25, 27, 29},
{31, 33, 35, 37}
};
_G.ledZ = {38,40,42,44};
//sets all used pins as output
for(int i = 0; i < 4; i++){
pinMode(_G.ledZ[i], OUTPUT);
for (int j = 0; j < 4; j++) {
pinMode(_G.ledXY[i][j], OUTPUT);
}
}
allOn(); /* switch on all led*/
allOff(); /* switch off all led */
}
void loop() {
static int xSeed;
static int ySeed;
static int zSeed;
_G.ledBuffer[xSeed][ySeed][zSeed] = 0;
if (zSeed == 0)
{
xSeed = random(0, 3);
ySeed = random(0, 3);
zSeed = random(0, 3);
} else {
zSeed--;
}
_G.ledBuffer[xSeed][ySeed][zSeed] = 1;
doLight();
}
static void doLight();
for (int step = 0; step < 80; step++) { /* Perhaps use sleep instead */
allOff();
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
for(int k = 0; i < 4; i++){
digitalSwitch(i, j, k, ledBuffer[i][j][k]);
}
}
}
}
}
static void digitalSwitch(int x, int y, int z, int on) {
/* convert x y z to the correct pin*/
digitalWrite(_G.ledXY[x][y], _G.[ledZ[z]], on ? HIGH : LOW);
}
static void _all(int on) {
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
for(int k = 0; i < 4; i++){
digitalSwitch(i, j, k, on);
}
}
}
}
int allOff(){
_all(0);
};
int allOn(){
_all(1);
};
相关文章:
- 如何使用FFTW库C++计算3D阵列的FFT?
- CUDA,使用2D和3D阵列
- 3D 阵列中的瓦尔格林德错误C++
- 3D阵列C 的动态初始化
- 在 cuda 中将 3D 阵列展平为 1D
- 如何在C++中轻松地将 3D 阵列绘制为表面
- 如何使用3D char数组调整双指针2D char阵列的大小
- 3d C 阵列到组装
- 在尺寸n的3D阵列中有效检查相邻的体素
- 将分配给一个3D阵列
- 用于 Arduino 的 3D 阵列
- 3D阵列删除C++的性能较慢
- 简单的 3D 阵列C++
- 高效访问存储为 1D 阵列的 3D 阵列
- 动态分配 3D 阵列
- 修改大型动态大小的3D阵列mex/C++
- 3D阵列到3D标准::矢量
- 指向3D阵列C++的指针
- 分割错误:使用动态3D阵列参数时为11
- 如何将用户输入存储到多维数组中?我正在使用 3D 阵列