Arduino - PROGMEM Accessing 3 Dimensional Array Data Part 2

In this article we are going to build some animations for the 8x8x8 rgb led cube using the progmem flash memory

For this animation we are going to display a sphere bouncing around inside the cube or you can call a ball as well. The approach for this animation is similar to the previous PROGMEM article. The sphere will start in the center of the cube, but it can start from any where. Bellow is the array that will draw the sphere, and every group of three numbers represent {levels,rows,columns}.

const int sphereSet[4][26][3] PROGMEM = {
  {{1,3,3},{1,3,4},{2,3,2},{2,3,3},{2,3,4},{2,3,5},{3,3,1},{3,3,2},{3,3,3},{3,3,4},{3,3,5},{3,3,6},{4,3,1},{4,3,2},{4,3,3},{4,3,4},{4,3,5},{4,3,6},{5,3,2},{5,3,3},{5,3,4},{5,3,5},{6,3,3},{6,3,4},
  {3,2,2},{4,2,2}},
  {{1,4,3},{1,4,4},{2,4,2},{2,4,3},{2,4,4},{2,4,5},{3,4,1},{3,4,2},{3,4,3},{3,4,4},{3,4,5},{3,4,6},{4,4,1},{4,4,2},{4,4,3},{4,4,4},{4,4,5},{4,4,6},{5,4,2},{5,4,3},{5,4,4},{5,4,5},{6,4,3},{6,4,4},
  {3,2,5},{4,2,5}},
  {{1,3,4},{1,4,4},{2,2,4},{2,3,4},{2,4,4},{2,5,4},{3,1,4},{3,2,4},{3,3,4},{3,4,4},{3,5,4},{3,6,4},{4,1,4},{4,2,4},{4,3,4},{4,4,4},{4,5,4},{4,6,4},{5,2,4},{5,3,4},{5,4,4},{5,5,4},{6,3,4},{6,4,4},
  {3,5,5},{4,5,5}},
  {{1,3,3},{1,4,3},{2,2,3},{2,3,3},{2,4,3},{2,5,3},{3,1,3},{3,2,3},{3,3,3},{3,4,3},{3,5,3},{3,6,3},{4,1,3},{4,2,3},{4,3,3},{4,4,3},{4,5,3},{4,6,3},{5,2,3},{5,3,3},{5,4,3},{5,5,3},{6,3,3},{6,4,3},
  {3,5,2},{4,5,2}},
};

Is a little long but I went for the big sphere. Next the idea is to manipulate these values accordingly, so we can move the sphere around up and down side to side. In order to get that of course variables and for loop.

void spherePat(){
  start=millis();
  while(millis()-start<5000){
  int mydata,mydata1,mydata2,t=150;
  for(int ii=0; ii<24; ii++){
    for(int i=0; i<4; i++){
      for(int j=0,jj=26,jjj=0; j<26,jj>0,jjj<26; j++,jj--,jjj++){
        for(int k=0; k<3; k++){ 
         if(k==0){ mydata = pgm_read_byte(&sphereSet[i][j][k]);}
         if(k==1){ mydata1 = pgm_read_byte(&sphereSet[i][j][k]);}
         if(k==2){ mydata2 = pgm_read_byte(&sphereSet[i][j][k]);}            
        }
        if(ii==1){ mydata=mydata-1,mydata1=mydata1-1,mydata2=mydata2-1;}
        if(ii==2){ mydata=mydata,mydata1=mydata1-1,mydata2=mydata2-1;}
        if(ii==3){ mydata=mydata+1,mydata1=mydata1-1,mydata2=mydata2-1;}
        if(ii==4){ mydata=mydata+1,mydata1=mydata1,mydata2=mydata2-1;}
        if(ii==5){ mydata=mydata+1,mydata1=mydata1+1,mydata2=mydata2-1;}
        if(ii==6){ mydata=mydata+1,mydata1=mydata1+1,mydata2=mydata2;}
        if(ii==7){ mydata=mydata+1,mydata1=mydata1+1,mydata2=mydata2+1;}
        if(ii==8){ mydata=mydata,mydata1=mydata1+1,mydata2=mydata2+1;}
        if(ii==9){ mydata=mydata-1,mydata1=mydata1+1,mydata2=mydata2+1;}
        if(ii==10){ mydata=mydata-1,mydata1=mydata1,mydata2=mydata2+1;}
        if(ii==11){ mydata=mydata-1,mydata1=mydata1-1,mydata2=mydata2+1;}
        if(ii==12){ mydata=mydata,mydata1=mydata1-1,mydata2=mydata2;}
        if(ii==13){ mydata=mydata+1,mydata1=mydata1-1,mydata2=mydata2-1;}
        if(ii==14){ mydata=mydata,mydata1=mydata1,mydata2=mydata2-1;}
        if(ii==15){ mydata=mydata-1,mydata1=mydata1+1,mydata2=mydata2-1;}
        if(ii==16){ mydata=mydata,mydata1=mydata1,mydata2=mydata2;}
        if(ii==17){ mydata=mydata+1,mydata1=mydata1-1,mydata2=mydata2+1;}
        if(ii==18){ mydata=mydata,mydata1=mydata1,mydata2=mydata2+1;}
        if(ii==19){ mydata=mydata-1,mydata1=mydata1+1,mydata2=mydata2+1;}
        if(ii==20){ mydata=mydata,mydata1=mydata1,mydata2=mydata2;}
        if(ii==21){ mydata=mydata+1,mydata1=mydata1-1,mydata2=mydata2-1;}
        if(ii==22){ mydata=mydata,mydata1=mydata1-1,mydata2=mydata2;}
        if(ii==23){ mydata=mydata-1,mydata1=mydata1-1,mydata2=mydata2+1;}
        LED(mydata,mydata1,mydata2,jj,jjj,0);
      }
    }
    delay(t);
    clean();
    }
  }
}

So above we have the function that will play the sphere for us. It will play for 5 seconds, following by our variables. We can visualise like this, the mydata variable having the every first number of all the groups of three numbers from the array, the mydata1 variable having the every second number of all the groups of three numbers and the mydata2 having the every third number of all the groups of three numbers. The first time that big loop goes it draws the sphere at the center, and the next 23 times the loops goes we manipulate the variables by adding or subtracting from the variables moving up and down, left and right each time the loop goes. For the color I did the transition between red and green by adding a couple variables in the "j" loop. 

CONCLUSION:

I found that the PROGMEM can be very useful for animations like this, but for some animations the for loop is more suitable.    

    


by: Almir Puglia | 448