BASIC LED CUBE ANIMATIONS Part-1

In this article I'm going to show you how I built some animations for the 8x8x8 LED RGB cube project

Since I followed the Kevin Darrah's schematics for the cube project, the sketch for the animations he had made for the Arduino Uno and later I found online the same one but for the Arduino Mega which has bigger memory therefor it holds more animations. The sketch is written in C++, which is new for me and I have some skills on other programming languages which help me to understand how it flows. The main function to control all the leds in the cube is like this. LED(level(0-7),row(0-7),col(0-7),red(0-15),green(0-15),blue(0-15)). As you can see is not that hard to understand at this point. This function will light up any led that you want of course with some manipulations with variables and values. One thing that I noticed is that most of the animations will involve loops to get the values that we want. For example to light up all the cube we would do a nested loop like bellow.


  for(int i=0;i<8;i++)
    for(int j=0;j<8;j++)
      for(int k=0;k<8;k++){
        LED(i,j,k,15,0,0);
      }

Notice that the first loop with the (i)variable is for the level, the (j)variable for row and the (k)variable for the column. Then the LED function with the values to display, and this sample is set for color red but it could be any combination. Of course after we could add delay time to light up each led instead all of them at once. Another main function is the clean function which will turn off all the leds in the cube at once. Another way to look at would be instead of i,j,k  could be x,y,z. 

void clean(){
  int i, j, k;
    for(i=0; i<8; i++)
      for(j=0; j<8; j++)
        for(k=0; k<8; k++)
          LED(i,j,k,0,0,0);
}

The clean function above is use a lot when you make animations. To use you just have to mention clean(); any where and that will turn off all the cube. What it turns off the leds of course is the value of 0,0,0 for all the three colors. You can also turn off a specific led by mention the exact location of that led in a loop. Another very important point is that when I started playing with these animations I noticed a lot repetition of loops through out of the animations. Then I realized that I could build a function with whatever I need and then call that function every time I need within another function. This way cuts down the amount of code you have to write. I created a function named funnel which will look like a funnel when the cube is light up. Down bellow I will show you a two samples that it does the same thing, the first one is just using the loops to get the values and the second one is using a function call and add the values.

void funnel(){
  int t=20,r=random(16),g=random(16),b=random(16);
    for(int i=0; i<1; i++)
      for(int j=0; j<1; j++)
        for(int k=0; k<8; k++){
          LED(i,j,k,r,g,b);
          delay(t);
        }
    for(int i=0; i<1; i++)
      for(int j=0; j<8; j++)
        for(int k=7; k<8; k++){
          LED(i,j,k,r,g,b);
          delay(t);
        }
    for(int i=0; i<1; i++)
      for(int j=7; j<8; j++)
        for(int k=8; k>0; k--){
          LED(i,j,k,r,g,b);
          delay(t);
        }
    for(int i=0; i<1; i++)
      for(int j=8; j>0; j--)
        for(int k=0; k<1; k++){
          LED(i,j,k,r,g,b);
          delay(t);
        }

Now a function call within another function.

void funnel(){
  int r=random(16),g=random(16),b=random(16),ra=0,ga=0,ba=0;
  funnelOne(20,0,1,0,1,0,8,r,g,b);
  funnelTwo(20,0,1,0,8,7,8,r,g,b);
  funnelThree(20,0,1,7,8,8,0,r,g,b);
  funnelFour(20,0,1,8,0,0,1,r,g,b);
}

And these are the functions that I call within the function above.

void funnelOne(int t,int ia, int ib, int ja, int jb, int ka, int kb,int r,int g,int b){
    for(int i=ia; i<ib; i++)
      for(int j=ja; j<jb; j++)
        for(int k=ka; k<kb; k++){
          LED(i,j,k,r,g,b);
          delay(t);
        }
}
void funnelTwo(int t,int ia, int ib, int ja, int jb, int ka, int kb,int r,int g,int b){
    for(int i=ia; i<ib; i++)
      for(int j=ja; j<jb; j++)
        for(int k=ka; k<kb; k++){
          LED(i,j,k,r,g,b);
          delay(t);
        }  
}
void funnelThree(int t,int ia, int ib, int ja, int jb, int ka, int kb,int r,int g,int b){
    for(int i=ia; i<ib; i++)
      for(int j=ja; j<jb; j++)
        for(int k=ka; k>kb; k--){
          LED(i,j,k,r,g,b);
          delay(t);
        }  
}
void funnelFour(int t,int ia, int ib, int ja, int jb, int ka, int kb,int r,int g,int b){
    for(int i=ia; i<ib; i++)
      for(int j=ja; j>jb; j--)
        for(int k=ka; k<kb; k++){
          LED(i,j,k,r,g,b);
          delay(t);
        }  
}

At this point both main functions will display the first level of the funnel. Notice that when you build a function that you will be calling within another function the variables you declare between the parentheses as parameters then when you call this function you must enter the values corresponding to the variables. In this case there are ten variables as parameters, the first one is the delay time, second and third the levels, fourth and fifth the rows, six and seventh the columns and the other three the color red, green and blue. The name of the variables you can name anything you want but you have to be careful sometimes with variable names with the rest of the program. At the moment the two functions are almost the same size in terms of lines of code, but at the end of the funnel the last level, the first function with just loops would be a lot bigger. From now on with the second function I just can call the other function within and will be just one line of code. Notice that rgb = random(16), this means will give a level from 0-15 randomly for each color every iteration. I like to display every color possible not just red, green and blue. The delay time depending what kind of animation you want to speed up or slow down. 1000 means one second, and that is very slow. Most of the animations are just a fraction of that. To finish with this animation I will add two more functions for the center of the funnel. The reason is because the leds will light up in a sparrow sequence so back and forth -- ++, that why I can not use the other functions.

void funnelcenterOne(int t,int ja, int jb, int ka, int kb,int level,int r,int g,int b){
  for(int j=ja;j<jb;j++)
    for(int k=ka;k<kb;k++){
      LED(level,j,k,r,g,b);
      delay(t);
    }  
}
void funnelcenterTwo(int t,int ja, int jb, int ka, int kb,int level,int r,int g,int b){
  for(int j=ja;j<jb;j++)
    for(int k=ka;k>kb;k--){
      LED(level,j,k,r,g,b);
      delay(t);
    }
}

Now we are ready to finish this animation with the complete funnel function.

void funnel(){
  start=millis();
  while(millis()-start<5000){
  int t=20,r=random(16),g=random(16),b=random(16),ra=0,ga=0,ba=0;
  funnelOne(t,0,1,0,1,0,8,r,g,b);
  funnelTwo(t,0,1,0,8,7,8,r,g,b);
  funnelThree(t,0,1,7,8,8,0,r,g,b);
  funnelFour(t,0,1,8,0,0,1,r,g,b);
  //
  funnelOne(t,1,2,1,2,1,7,r,g,b);
  funnelTwo(t,1,2,1,7,6,7,r,g,b);    
  funnelThree(t,1,2,6,7,6,1,r,g,b);
  funnelFour(t,1,2,6,1,1,2,r,g,b);
  //
  funnelOne(t,2,3,2,3,2,6,r,g,b);
  funnelTwo(t,2,3,2,6,5,6,r,g,b); 
  funnelThree(t,2,3,5,6,5,1,r,g,b);
  funnelFour(t,2,3,5,2,2,3,r,g,b);
  //center
  funnelcenterOne(t,3,4,3,5,3,r,g,b);
  funnelcenterTwo(t,4,5,4,2,3,r,g,b);
  funnelcenterOne(t,3,4,3,5,4,r,g,b);
  funnelcenterTwo(t,4,5,4,2,4,r,g,b);
  //
  funnelOne(t,5,6,2,3,2,6,r,g,b);
  funnelTwo(t,5,6,2,6,5,6,r,g,b); 
  funnelThree(t,5,6,5,6,5,1,r,g,b);
  funnelFour(t,5,6,5,2,2,3,r,g,b);
  //
  funnelOne(t,6,7,1,2,1,7,r,g,b);
  funnelTwo(t,6,7,1,7,6,7,r,g,b);    
  funnelThree(t,6,7,6,7,6,1,r,g,b);
  funnelFour(t,6,7,6,1,1,2,r,g,b);
  //
  funnelOne(t,7,8,0,1,0,8,r,g,b);
  funnelTwo(t,7,8,0,8,7,8,r,g,b);
  funnelThree(t,7,8,7,8,8,0,r,g,b);
  funnelFour(t,7,8,8,0,0,1,r,g,b);
  //going clean down
  funnelOne(t,7,8,0,1,0,8,ra,ga,ba);
  funnelTwo(t,7,8,0,8,7,8,ra,ga,ba);
  funnelThree(t,7,8,7,8,8,0,ra,ga,ba);
  funnelFour(t,7,8,8,0,0,1,ra,ga,ba);
  //
  funnelOne(t,6,7,1,2,1,7,ra,ga,ba);
  funnelTwo(t,6,7,1,7,6,7,ra,ga,ba);    
  funnelThree(t,6,7,6,7,6,1,ra,ga,ba);
  funnelFour(t,6,7,6,1,1,2,ra,ga,ba);
  //  
  funnelOne(t,5,6,2,3,2,6,ra,ga,ba);
  funnelTwo(t,5,6,2,6,5,6,ra,ga,ba); 
  funnelThree(t,5,6,5,6,5,1,ra,ga,ba);
  funnelFour(t,5,6,5,2,2,3,ra,ga,ba);
  //  
  funnelcenterOne(t,3,4,3,5,4,ra,ga,ba);
  funnelcenterTwo(t,4,5,4,2,4,ra,ga,ba);
  funnelcenterOne(t,3,4,3,5,3,ra,ga,ba);
  funnelcenterTwo(t,4,5,4,2,3,ra,ga,ba);
  //
  funnelOne(t,2,3,2,3,2,6,ra,ga,ba);
  funnelTwo(t,2,3,2,6,5,6,ra,ga,ba); 
  funnelThree(t,2,3,5,6,5,1,ra,ga,ba);
  funnelFour(t,2,3,5,2,2,3,ra,ga,ba);
  //
  funnelOne(t,1,2,1,2,1,7,ra,ga,ba);
  funnelTwo(t,1,2,1,7,6,7,ra,ga,ba);    
  funnelThree(t,1,2,6,7,6,1,ra,ga,ba);
  funnelFour(t,1,2,6,1,1,2,ra,ga,ba);
  //
  funnelOne(t,0,1,0,1,0,8,ra,ga,ba);
  funnelTwo(t,0,1,0,8,7,8,ra,ga,ba);
  funnelThree(t,0,1,7,8,8,0,ra,ga,ba);
  funnelFour(t,0,1,8,0,0,1,ra,ga,ba);
  } 
}

At the top the millis() function with the while loop it determine how long it will play. After that I resign the time variable again so it makes easier to change the speed by doing once instead to change on every function. The ra,ga and ba = 0 are the variables that will turn off the funnel the other way. The // means comment out one line of the code, is always good practice to make comments through out the coding process.

CONCLUSION: C++ is a very powerful programming language and there are a lot to learn from. I'm sure there are different ways to do this and get the same results, but at the same time I'm learning and have a lot fun. If you have any question you can make through my youtube channel. See you on the next one. 


by: Almir Puglia | 340