2015-04-15 63 views
0

目前我正在研究我的4x4x4 LED立方體,我想爲它編寫自己的代碼,但目前我被卡在使用3D陣列上。我在void setup()中聲明瞭幾個數組,並且我也嘗試將它們放入void loop()中。儘管如此,當試圖編譯時,它仍然會返回錯誤。Arduino的3D陣列

簡而言之,代碼應該生成一個帶有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 

有人可以幫助我,或至少點我在正確的方向。

+2

在測試任何代碼之前,不應該編寫這麼多的代碼。你試圖至少得到兩件新事物同時工作;獨立開發它們,然後將它們掛在一起。 – Beta

回答

1

void loop(),您聲明xSeedySeedzSeedif塊,這使得局部變量,如果塊,這意味着他們走出去的範圍時你,如果塊退出內部。在ledBuffer之前(在void loop()函數的開始處)聲明它們之前(在外部)if塊,因爲在您告訴編譯器它們存在之前您正試圖使用​​這些變量,因此編譯器的消息是它們「未聲明在此範圍內」

編輯

而且,看來你想所有這些功能相同的ledBuffer 3D陣列上運行。您應該考慮將其聲明爲全局變量(如果您使用的是C),或者如果您使用的是C++,則可以考慮將其作爲類,並將ledBuffer作爲屬性/字段。

0

我試圖拆分你的代碼並將其因式分解。我沒有Arduino構建系統,我無法測試我的代碼。 我不明白你的映射x,y,z到pin號碼我已經嘗試了一個。

/* 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);                 
};