Demo entry 6310535

Arduino LED Cube

   

Submitted by anonymous on Oct 23, 2016 at 04:28
Language: Arduino. Code size: 21.2 kB.

      
      
      //Column 과 Layer 반복을 효율적으로 하기 위한 배열 정의
      int column[17] = {0,3,2,1,0,4,5,6,7,8,9,10,11,12,13,A5,A4};
      int layer[5] = {0,A3,A2,A1,A0};
      int layer4 = layer[4]; int layer3 = layer[3]; int layer2 = layer[2]; int layer1 = layer[1];
       
      int D0 = column[1]; int D1 = column[2]; int D2 = column[3]; int D3 = column[4];
      int D4 = column[5];  int D5 = column[6]; int D6 = column[7]; int D7 = column[8]; 
      int D8 = column[9]; int D9 = column[10]; int D10 = column[11]; int D11 = column[12]; 
      int D12 = column[13]; int D13 = column[14]; int D14 = column[15]; int D15 = column[16];
      int i,j; //반복자
      int k,l; //반복자
      
      
      //지면기준
      //Layer 1 : 1층
      //Layer 2 : 2층
      //Layer 3 : 3층
      //Layer 4 : 4층
      /*
          조감도 
          
          D12 D8  D4 D0
         
          D13 D9  D5 D1
          
          D14 D10 D6 D2
          
          D15 D11 D7 D3
          
          ---------캐소드연결방향
      가로 방향: X축 
      세로 방향: Y축
      높이 : Z축
      */
      
      
      //Layer LOW : Layer ON
      //Layer HIGH : Layer OFF
      //Column HIGH : Column ON
      //Column LOW : Column OFF
      
      // 핀 셋업함수
      void setup() {                
        // initialize the digital pin as an output.
        pinMode(D1, OUTPUT);pinMode(D0, OUTPUT);
        pinMode(D2, OUTPUT); pinMode(D3, OUTPUT); pinMode(D4, OUTPUT);
        pinMode(D5, OUTPUT); pinMode(D6, OUTPUT); pinMode(D7, OUTPUT);
        pinMode(D8, OUTPUT); pinMode(D9, OUTPUT); pinMode(D10, OUTPUT);  
        pinMode(D11, OUTPUT);pinMode(12, OUTPUT); pinMode(D13, OUTPUT);
        pinMode(D14, OUTPUT);pinMode(D15, OUTPUT);
        pinMode(layer1, OUTPUT); pinMode(layer2, OUTPUT); pinMode(layer3, OUTPUT);  pinMode(layer4, OUTPUT);   
      }
      
      // the loop routine runs over and over again forever:
      void loop() {
          
            //LED 1개씩 ON OFF
           OnOffEachLEDLayer();        //1층부터 시작해서 1층마다 LED 한개씩 ON OFF
           OnOffEachLEDColumn();       //Column 방향으로 1개씩 ON OFF
         
           //LED line 별로 ON OFF
           ColumnLineOnOFF();          //Z라인 1줄씩 밝히기 모든 Layer를 HIGH 한 상태에서 원하는 D1~16를 차례로 HIGH -> LOW 
           LayerLineOnOFF();           //XY 평면과 평행한 Line 으로 한줄씩 ON OFF
         
           //LED 평면으로 ON OFF
           YZOnOFF();                  //YZ평면으로 LED ON OFF
           XYOnOFF();                  //XY평면으로 LED ON OFF
           XZOnOFF();                  //XZ평면으로 LED ON OFF
     
      //   randomRain();
      
      }
      
       /* -------------------------모듈 함수 --------------------------*/
      //지연 시간 전체 설정
      void TimeDelay()
      {
           delay(100); 
      }
      
      //모든 column의 LED ON 모듈화
      void ColumnON()
      {
                //All HIGH
                digitalWrite(column[1], HIGH);digitalWrite(column[2], HIGH);digitalWrite(column[3], HIGH);digitalWrite(column[4], HIGH);
                digitalWrite(column[5], HIGH);digitalWrite(column[6], HIGH);digitalWrite(column[7], HIGH);digitalWrite(column[8], HIGH);
                digitalWrite(column[9], HIGH);digitalWrite(column[10], HIGH);digitalWrite(column[11], HIGH);digitalWrite(column[12], HIGH);
                digitalWrite(column[13], HIGH);digitalWrite(column[14], HIGH);digitalWrite(column[15], HIGH);digitalWrite(column[16], HIGH); 
                
        
      }
      
      //모든 Column의 LED OFF 모듈화
      void ColumnOFF()
      {
                //ALL LOW
                digitalWrite(column[1], LOW);digitalWrite(column[2], LOW);digitalWrite(column[3], LOW);digitalWrite(column[4], LOW);
                digitalWrite(column[5], LOW);digitalWrite(column[6], LOW);digitalWrite(column[7], LOW);digitalWrite(column[8], LOW);
                digitalWrite(column[9], LOW);digitalWrite(column[10], LOW);digitalWrite(column[11], LOW);digitalWrite(column[12], LOW);
                digitalWrite(column[13], LOW);digitalWrite(column[14], LOW);digitalWrite(column[15], LOW);digitalWrite(column[16], LOW); 
      
        
      }
        
      //XY방향의 Line ON OFF 모듈화
      void XYLine()
      {
                  //1~4번 ON OFF
                 digitalWrite(column[1], HIGH);  digitalWrite(column[2], HIGH);  digitalWrite(column[3], HIGH);  digitalWrite(column[4], HIGH);        
                  TimeDelay();
                 digitalWrite(column[1], LOW);  digitalWrite(column[2], LOW);  digitalWrite(column[3], LOW);  digitalWrite(column[4], LOW);
                 
                 //5~8번 ON OFF
                 digitalWrite(column[5], HIGH);  digitalWrite(column[6], HIGH);  digitalWrite(column[7], HIGH);  digitalWrite(column[8], HIGH);
                  TimeDelay();
                 digitalWrite(column[5], LOW);  digitalWrite(column[6], LOW);  digitalWrite(column[7], LOW);  digitalWrite(column[8], LOW);
                 
                 //9~12번 ON OFF
                 digitalWrite(column[9], HIGH);  digitalWrite(column[10], HIGH);  digitalWrite(column[11], HIGH);  digitalWrite(column[12], HIGH);
                  TimeDelay();
                 digitalWrite(column[9], LOW);  digitalWrite(column[10], LOW);  digitalWrite(column[11], LOW);  digitalWrite(column[12], LOW);
                
                 //13~16번 ON OFF
                 digitalWrite(column[13], HIGH);  digitalWrite(column[14], HIGH);  digitalWrite(column[15], HIGH);  digitalWrite(column[16], HIGH);
                  TimeDelay();
                 digitalWrite(column[13], LOW);  digitalWrite(column[14], LOW);  digitalWrite(column[15], LOW);  digitalWrite(column[16], LOW);
              
      }

         
      //모든 LED OFF
      void turnEverythingOff()
       {
               for(int i = 1; i<=16; i++)
               {
                         digitalWrite(column[i], LOW);
               }
               for(int i = 1; i<=4; i++)
               {
                         digitalWrite(layer[i], HIGH);
               }
       }
       
       //모든 LED ON
       void turnEverythingOn()
       {
               for(int i = 1; i<=16; i++)
               {
                         digitalWrite(column[i], HIGH);
               }
               for(int i = 1; i<=4; i++)
               {
                         digitalWrite(layer[i], LOW);
               }
       }
       
      
        /* -------------------------1개 LED ON OFF --------------------------*/
     
     
      //1층부터 시작해서 1층마다 LED 한개씩 ON OFF
      void OnOffEachLEDLayer()
      {
 
              turnEverythingOff();
              //Layer1~4 까지 ON OFF
              for(int i=1; i<=4; i++)
              {
             
                     //Column 1~16까지 ON OFF
                    for(int j=1;j<=16; j++)
                     {
                      
                           digitalWrite(column[j],HIGH);
                           digitalWrite(layer[i],LOW);
                           TimeDelay();
                           digitalWrite(column[j],LOW);
                           digitalWrite(layer[i],HIGH);
                            
                     }
            
              }
        
      }
      
      
      //Column 방향으로 1개씩 ON OFF
      void OnOffEachLEDColumn()
      {
       
              turnEverythingOff();
              
              //Column 1~16까지 ON OFF
              for(int i=1; i<=16; i++)
              {
             
                   //Layer 1~4 까지 ON OFF
                    for(int j=1;j<=4; j++)
                    {
                      
                           digitalWrite(column[i],HIGH);
                           digitalWrite(layer[j],LOW);
                           TimeDelay();
                           digitalWrite(column[i],LOW);
                           digitalWrite(layer[j],HIGH);
                            
                    }
            
              }
        
      }
      
      
      
     /* -------------------------Line별 LED ON OFF --------------------------*/
      
      // z라인 1줄씩 밝히기 모든 Layer를 HIGH 한 상태에서 원하는 D1~16를 차례로 HIGH -> LOW 
      void ColumnLineOnOFF()
      {
          
          //16번 반복
          for(i=1; i<=16; i++)
          {
           
             digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
             
             //Column[0] LOW, column[1] HIGH -> column[1] LOW, column[2] HIGH -> ,,,
             digitalWrite(column[i-1], LOW); digitalWrite(column[i], HIGH);
         
             digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
              TimeDelay();
            
          }
      }
      
      //XY 평면과 평행한 Line 으로 한줄씩 ON OFF
      void LayerLineOnOFF()
      {
             turnEverythingOff();    
             
             
             
             for(j=1;j<=4;j++)
             { 
             
                     //1층
                   if(j==1)
                   {
                          digitalWrite(layer[1], LOW); 
                          XYLine();                      //XY line ON OFF 모듈
                          digitalWrite(layer[1], HIGH); 
                          TimeDelay();
                   }   
                    //2층
                   if(j==2)
                   {
                          digitalWrite(layer[2], LOW); 
                          XYLine();                     //XY line ON OFF 모듈
                          digitalWrite(layer[2], HIGH);
                          TimeDelay();
                   }     
                    //3층
                   if(j==3)
                   {
                          digitalWrite(layer[3], LOW); 
                          XYLine();                     //XY line ON OFF 모듈
                          digitalWrite(layer[3], HIGH);
                          TimeDelay();
                   }   
                    //4층
                   if(j==4)
                   {
                          digitalWrite(layer[4], LOW); 
                          XYLine();                     //XY line ON OFF 모듈
                          digitalWrite(layer[4], HIGH);
                          TimeDelay();
                   }   
          
             }
        
      }
      
      
       /* -------------------------평면 LED ON OFF --------------------------*/
      
      
      //YZ평면으로 LED ON OFF
      void YZOnOFF()
      {
          
              
              for(i=1; i<=4; i++)
              {
                     //Column 1~4까지 ON OFF && Layer 1~4 까지 ON OFF
                    if(i==1)
                    {
                           digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                           digitalWrite(column[1], HIGH);digitalWrite(column[2], HIGH);digitalWrite(column[3], HIGH);digitalWrite(column[4], HIGH);
                         
                           digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                           TimeDelay();
                           digitalWrite(column[1], LOW);digitalWrite(column[2], LOW);digitalWrite(column[3], LOW);digitalWrite(column[4], LOW);
                    }else 
                     //Column 5~8까지 ON OFF && Layer 1~4 까지 ON OFF
                    if(i==2)
                    {
                      
                           digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                           digitalWrite(column[5], HIGH);digitalWrite(column[6], HIGH);digitalWrite(column[7], HIGH);digitalWrite(column[8], HIGH);
                       
                           digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                           TimeDelay();
                           digitalWrite(column[5], LOW);digitalWrite(column[6], LOW);digitalWrite(column[7], LOW);digitalWrite(column[8], LOW); 
                          
                    }else 
                     //Column 9~12까지 ON OFF && Layer 1~4 까지 ON OFF
                    if(i==3)
                    {
                      
                           digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                           digitalWrite(column[9], HIGH);digitalWrite(column[10], HIGH);digitalWrite(column[11], HIGH);digitalWrite(column[12], HIGH);
                      
                           digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                           TimeDelay();
                           digitalWrite(column[9], LOW);digitalWrite(column[10], LOW);digitalWrite(column[11], LOW);digitalWrite(column[12], LOW); 
                          
                          
                    }else 
                     //Column 13~16까지 ON OFF && Layer 1~4 까지 ON OFF
                    if(i==4)
                    {
                      
                      
                           digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                           digitalWrite(column[13], HIGH);digitalWrite(column[14], HIGH);digitalWrite(column[15], HIGH);digitalWrite(column[16], HIGH);
                      
                           digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                           TimeDelay();
                           digitalWrite(column[13], LOW);digitalWrite(column[14], LOW);digitalWrite(column[15], LOW);digitalWrite(column[16], LOW); 
                          
                      
                    } 
                  
              }
      }
      
      
      //XZ평면으로 LED ON OFF
      void XZOnOFF()
      {
          
              
              for(i=1; i<=4; i++)
              {
                        //Column 1,5,9,3 까지 ON OFF && Layer 1~4 까지 ON OFF
                      if(i==1)
                      {
                               digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                               digitalWrite(column[1], HIGH);digitalWrite(column[5], HIGH);digitalWrite(column[9], HIGH);digitalWrite(column[13], HIGH);
                             
                               digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                               TimeDelay();
                               digitalWrite(column[1], LOW);digitalWrite(column[5], LOW);digitalWrite(column[9], LOW);digitalWrite(column[13], LOW);
                      }else 
                      //Column 2,6,10,14 까지 ON OFF && Layer 1~4 까지 ON OFF
                      if(i==2)
                      {
                              
                               digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                               digitalWrite(column[2], HIGH);digitalWrite(column[6], HIGH);digitalWrite(column[10], HIGH);digitalWrite(column[14], HIGH);
                             
                               digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                               TimeDelay();
                               digitalWrite(column[2], LOW);digitalWrite(column[6], LOW);digitalWrite(column[10], LOW);digitalWrite(column[14], LOW); 
                        
                      }else 
                      //Column 3,7,11,15 까지 ON OFF && Layer 1~4 까지 ON OFF
                      if(i==3)
                      {
                              
                               digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                               digitalWrite(column[3], HIGH);digitalWrite(column[7], HIGH);digitalWrite(column[11], HIGH);digitalWrite(column[15], HIGH);
                            
                               digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                               TimeDelay();
                               digitalWrite(column[3], LOW);digitalWrite(column[7], LOW);digitalWrite(column[11], LOW);digitalWrite(column[15], LOW); 
                              
                              
                      }else 
                      //Column 4,8,12,16 까지 ON OFF && Layer 1~4 까지 ON OFF
                      if(i==4)
                      {
                              
                              
                               digitalWrite(layer[1], HIGH); digitalWrite(layer[2], HIGH); digitalWrite(layer[3], HIGH); digitalWrite(layer[4], HIGH); 
                               digitalWrite(column[4], HIGH);digitalWrite(column[8], HIGH);digitalWrite(column[12], HIGH);digitalWrite(column[16], HIGH);
                           
                               digitalWrite(layer[1], LOW); digitalWrite(layer[2], LOW); digitalWrite(layer[3], LOW);  digitalWrite(layer[4], LOW);
                               TimeDelay();
                               digitalWrite(column[4], LOW);digitalWrite(column[8], LOW);digitalWrite(column[12], LOW);digitalWrite(column[16], LOW); 
                              
                        
                      }
                
                
              }
      }
      
      //XY평면으로 LED ON OFF
      void XYOnOFF()
      {
              turnEverythingOff();
        
              for(i=1; i<=4; i++)
              {
               
                      //Layer 4 만 ON OFF && 모든 Column ON OFF
                      if(i==1)
                      {
                             digitalWrite(layer[4], LOW); 
                             ColumnON();
                             TimeDelay();
                             ColumnOFF();
                           
                             digitalWrite(layer[4], HIGH);
                        
                        
                      }else 
                       //Layer 3 만 ON OFF && 모든 Column ON OFF
                      if(i==2)
                      {
                             digitalWrite(layer[3], LOW); 
                             ColumnON();
                             TimeDelay();
                             ColumnOFF();
                         
                             digitalWrite(layer[3], HIGH);
                         
                        
                      }else 
                       //Layer 2 만 ON OFF && 모든 Column ON OFF
                      if(i==3)
                      {
                        
                            digitalWrite(layer[2], LOW); 
                             ColumnON();
                             TimeDelay();
                             ColumnOFF();
                           
                             digitalWrite(layer[2], HIGH);
                          
                        
                      }else 
                       //Layer 1 만 ON OFF && 모든 Column ON OFF
                      if(i==4)
                      {
                             digitalWrite(layer[1], LOW); 
                             ColumnON();
                             TimeDelay();
                             ColumnOFF();
                          
                             digitalWrite(layer[1], HIGH);
                              
                        
                      }
                
              }
      }
   
      void randomRain()
      {
              turnEverythingOff();
              int x = 100;
              for(int i = 0; i!=60; i+=2)
              {
                        int randomColumn = random(1,17);
                        digitalWrite(column[randomColumn], HIGH);
                        digitalWrite(layer[4], LOW);
                        delay(x+100);
                        digitalWrite(layer[4], HIGH);
                        digitalWrite(layer[3], LOW);
                        delay(x);
                        digitalWrite(layer[3], HIGH);
                        digitalWrite(layer[2], LOW);
                        delay(x);
                        digitalWrite(layer[2], HIGH);
                        digitalWrite(layer[1], LOW);
                        delay(x+100);
                        digitalWrite(layer[1], HIGH);
                        digitalWrite(column[randomColumn], LOW);
              }
      }

This snippet took 0.04 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).