user2473971
user2473971

Reputation:

3D array for Arduino

Currently I am working on my 4x4x4 led cube, I wanted to write my own code for it but currently I am stuck on using 3D arrays. I declare several arrays in void setup(), and I have tried putting them in the void loop() as well. Still, when trying to compile it keeps returning errors.

In short, the code is supposed to generate a random point with a XYZ value. Then it has to write it to an buffer, this buffer has to project and multiplex it onto the led cube.

I made the lines that return the errors bold.

The errors are:

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.

The code:

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

Can someone help me or at least point me in the right direction.

Upvotes: 0

Views: 828

Answers (2)

&#212;rel
&#212;rel

Reputation: 7622

I have tried to split your code and factorized it. I don't have an Arduino build system I can't test my code. I don't understand your mapping x, y, z to pin number I have tried one.

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

Upvotes: 0

Konrad
Konrad

Reputation: 2287

Under void loop(), you are declaring xSeed, ySeed, and zSeed inside the if block, which makes the variables local to that if block, which means they go out of scope when you exit the if block. Declare them before (outside) the if block instead, before ledBuffer, at the beginning of the void loop() function, since you are trying to use those variables before you have told the compiler that they exist, hence the compiler's message that they were "not declared in this scope"

EDIT

Also, it seems you want all these functions to operate on the same ledBuffer 3D array. You should consider declaring it as a global variable (if you are using C), or if you are using C++, perhaps consider making it a class, with ledBuffer as an attribute/field.

Upvotes: 1

Related Questions