Pages: [1]   Go Down
Author Topic: 3 x 3 x 3 LED Cube code  (Read 1466 times)
0 Members and 1 Guest are viewing this topic.
Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 513
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have recently resurrected my 3 x 3 x 3 LED cube to write some code based on Hassan Ali's code at -

http://www.abrushfx.com/Arduino/ledcube3.html

The posted code
Code:
//Code based on ABrushFX Cube code at
//http://www.abrushfx.com/Arduino/ledcube3.html

//Array for three LED levels
int ledRow[] ={ 11, 12, 13 };
// Array for nine LED's in each level
int ledCol[]= { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int timer= 500;

void setup() // Sets up all pins as Outputs

  for( int i =  0; i < 3; i++)
 
  {
  pinMode( ledRow[i],OUTPUT);
  }

    { 
      for(int j =  0; j < 9; j++)
     
      {
      pinMode( ledCol[j],OUTPUT);
      }
    }
}
// Sets an LED HIGH based on Row and Column position
void onLED(int ledRow, int ledCol){
 
  digitalWrite(ledRow, HIGH);
  digitalWrite(ledCol, HIGH);
 
}

// Sets an LED LOW based on Row and Column position
void offLED(int ledRow, int ledCol)
{
  digitalWrite(ledRow, LOW);
  digitalWrite(ledCol, LOW);
}

void topRowPaneFigure8()
    {
      for(int y = 0; y < 3; y++)   // light LED's 1 - 3 on/off on top level
       
      {
        onLED(ledRow[0],ledCol[y]);         // 7 8 9
        delay(timer);                       // 4 5 6
        offLED(ledRow[0],ledCol[y]);        // 1 2 3
      }
       for(int y = 5; y > 2; y--)  // light LED's 6 - 4 on/off on top level
      {
        onLED(ledRow[0],ledCol[y]);
        delay(timer);
        offLED(ledRow[0],ledCol[y]);
      }
       for(int y = 6; y < 9; y++)  // light LED's 7 - 9 on/off on top level
      {
        onLED(ledRow[0],ledCol[y]);
        delay(timer);
        offLED(ledRow[0],ledCol[y]);
      }
      for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on top level
      {
        onLED(ledRow[0],ledCol[y]);
        delay(timer);
        offLED(ledRow[0],ledCol[y]);
      }
 
  }
 

void centreRowPaneFigure8()
    {
      for(int y = 0; y < 3; y++)    // light LED's 1 - 3 on/off on centre level
       
      {
        onLED(ledRow[1],ledCol[y]);           // 7 8 9
        delay(timer);                         // 4 5 6
        offLED(ledRow[1],ledCol[y]);          // 1 2 3
      }
       for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on centre level
      {
        onLED(ledRow[1],ledCol[y]);
        delay(timer);
        offLED(ledRow[1],ledCol[y]);
      }
       for(int y = 6; y < 9; y++)   // light LED's 7 - 9 on/off on centre level
      {
        onLED(ledRow[1],ledCol[y]);
        delay(timer);
        offLED(ledRow[1],ledCol[y]);
      }
      for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on centre level
      {
        onLED(ledRow[1],ledCol[y]);
        delay(timer);
        offLED(ledRow[1],ledCol[y]);
      }
 
  }
 
  void bottomRowPaneFigure8()
    {
      for(int y = 0; y < 3; y++)   // light LED's 1 - 3 on/off on bottom level
       
      {
        onLED(ledRow[2],ledCol[y]);          // 7 8 9
        delay(timer);                        // 4 5 6
        offLED(ledRow[2],ledCol[y]);         // 1 2 3
      }
       for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on bottom level
      {
        onLED(ledRow[2],ledCol[y]);
        delay(timer);
        offLED(ledRow[2],ledCol[y]);
      }
       for(int y = 6; y < 9; y++)   // light LED's 7 - 9 on/off on bottom level
      {
        onLED(ledRow[2],ledCol[y]);
        delay(timer);
        offLED(ledRow[2],ledCol[y]);
      }
      for(int y = 5; y > 2; y--)    // light LED's 6 - 4 on/off on bottom level
      {
        onLED(ledRow[2],ledCol[y]); 
        delay(timer);
        offLED(ledRow[2],ledCol[y]);
      }
 
  }
 
 
void loop()
{
  topRowPaneFigure8();
  centreRowPaneFigure8();
  bottomRowPaneFigure8();
}


 
includes three functions  (one for each level of the cube) that light the LED's in a figure eight configuration as detailed in the short attached txt document. The code operates as required but in the interest of building on my limited code writing skills  smiley-cool I am wondering how to consolidate the three functions

topRowPaneFigure8
centreRowPaneFigure8
bottomRowPaneFigure8

into one function. Where I am having trouble is the fact that the only difference between the three functions is the (ledRow[0] section in

onLED(ledRow[0],ledCol[y]);         
delay(timer);                       
offLED(ledRow[0],ledCol[y]);

which is 0, 1 and 2 in the three functions corresponding to the three levels. I assume that the solution is to use a for loop, but as the " ledRow[ ] "appears unchanged four times in each function, I am somewhat perplexed. I hope that I have explained myself adequately and thanks for reading Pedro.

* Cube doc.txt (0.39 KB - downloaded 13 times.)
Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8665
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void rowPaneFigure8(int row)
    {
    // Check for range errors
    if (row < 0 || row > 2)
        return;

      for(int y = 0; y < 3; y++)    // light LED's 1 - 3 on/off on centre level
      {
        onLED(ledRow[row],ledCol[y]);           // 7 8 9
        delay(timer);                                   // 4 5 6
        offLED(ledRow[row],ledCol[y]);          // 1 2 3
      }
       for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on centre level
      {
        onLED(ledRow[1],ledCol[y]);
        delay(timer);
        offLED(ledRow[1],ledCol[y]);
      }
       for(int y = 6; y < 9; y++)   // light LED's 7 - 9 on/off on centre level
      {
        onLED(ledRow[row],ledCol[y]);
        delay(timer);
        offLED(ledRow[row],ledCol[y]);
      }
      for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on centre level
      {
        onLED(ledRow[row],ledCol[y]);
        delay(timer);
        offLED(ledRow[row],ledCol[y]);
      }
  }
 
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 513
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your reply johnwasser. Do I correctly assume that you mean for me to replace my three existing functions

topRowPaneFigure8
centreRowPaneFigure8
bottomRowPaneFigure8

with the  modified rowPaneFigure8 function you have provided ?

If so I get the following errors

sketch_nov17a.cpp: In function 'void loop()':
sketch_nov17a:39: error: too few arguments to function 'void allRowPaneFigure8(int)'
sketch_nov17a:72: error: at this point in file

Am I possibly making some mistake, here is the code  I am now trying to compile and upload  thanks Pedro.

Code:
//Code based on ABrushFX Cube code at
//http://www.abrushfx.com/Arduino/ledcube3.html

//Array for three LED levels
int ledRow[] ={ 11, 12, 13 };
// Array for nine LED's in each level
int ledCol[]= { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int timer= 500;

void setup() // Sets up all pins as Outputs

  for( int i =  0; i < 3; i++)

  {
    pinMode( ledRow[i],OUTPUT);
  }

  { 
    for(int j =  0; j < 9; j++)

    {
      pinMode( ledCol[j],OUTPUT);
    }
  }
}
// Sets an LED HIGH based on Row and Column position
void onLED(int ledRow, int ledCol){

  digitalWrite(ledRow, HIGH);
  digitalWrite(ledCol, HIGH);

}

// Sets an LED LOW based on Row and Column position
void offLED(int ledRow, int ledCol)
{
  digitalWrite(ledRow, LOW);
  digitalWrite(ledCol, LOW);
}

void rowPaneFigure8(int row)
    {
    // Check for range errors
    if (row < 0 || row > 2)
        return;

      for(int y = 0; y < 3; y++)    // light LED's 1 - 3 on/off on centre level
      {
        onLED(ledRow[row],ledCol[y]);           // 7 8 9
        delay(timer);                                   // 4 5 6
        offLED(ledRow[row],ledCol[y]);          // 1 2 3
      }
       for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on centre level
      {
        onLED(ledRow[1],ledCol[y]);
        delay(timer);
        offLED(ledRow[1],ledCol[y]);
      }
       for(int y = 6; y < 9; y++)   // light LED's 7 - 9 on/off on centre level
      {
        onLED(ledRow[row],ledCol[y]);
        delay(timer);
        offLED(ledRow[row],ledCol[y]);
      }
      for(int y = 5; y > 2; y--)   // light LED's 6 - 4 on/off on centre level
      {
        onLED(ledRow[row],ledCol[y]);
        delay(timer);
        offLED(ledRow[row],ledCol[y]);
      }
  }

void loop()
{
rowPaneFigure8();
}


 
Logged


Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8665
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Replace
Code:
topRowPaneFigure8();
centreRowPaneFigure8();
bottomRowPaneFigure8();
with
Code:
rowPaneFigure8(0);
rowPaneFigure8(1);
rowPaneFigure8(2);
or
Code:
for (int i=0; i<3; i++)
    rowPaneFigure8(i);
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Canberra Australia
Offline Offline
God Member
*****
Karma: 11
Posts: 513
Hardcore Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks johnwasser.
It took me a little while to work out how to implement your suggestions, but in the end I just reduced the three functions to one and incremented the

Code:
onLED(ledRow[i],ledCol[j]);
delay(timer);                      
offLED(ledRow[i],ledCol[j]);

with the for loop

Code:
for (int i = 0; i < 3; i++)

so I ended up with

Code:
   
    //Code based on ABrushFX Cube code at
//http://www.abrushfx.com/Arduino/ledcube3.html

//Array for three LED levels
int ledRow[] ={ 11, 12, 13 };
// Array for nine LED's in each level
int ledCol[]= { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int timer= 500;

void setup() // Sets up all pins as Outputs

  for( int i =  0; i < 3; i++)
 
  {
  pinMode( ledRow[i],OUTPUT);
  }

    { 
      for(int j =  0; j < 9; j++)
     
      {
      pinMode( ledCol[j],OUTPUT);
      }
    }
}
// Sets an LED HIGH based on Row and Column position
void onLED(int ledRow, int ledCol){
 
  digitalWrite(ledRow, HIGH);
  digitalWrite(ledCol, HIGH);
 
}

// Sets an LED LOW based on Row and Column position
void offLED(int ledRow, int ledCol)
{
  digitalWrite(ledRow, LOW);
  digitalWrite(ledCol, LOW);
}

void rowPaneFigure8()
  {
    for (int i = 0; i < 3; i++)
 
    {
      for(int j = 0; j < 3; j++)   // light LED's 1 - 3 on/off on top level
       
      {
        onLED(ledRow[i],ledCol[j]);         // 7 8 9
        delay(timer);                       // 4 5 6
        offLED(ledRow[i],ledCol[j]);        // 1 2 3
      }
       for(int j = 5; j > 2; j--)  // light LED's 6 - 4 on/off on top level
      {
        onLED(ledRow[i],ledCol[j]);
        delay(timer);
        offLED(ledRow[i],ledCol[j]);
      }
       for(int j = 6; j < 9; j++)  // light LED's 7 - 9 on/off on top level
      {
        onLED(ledRow[i],ledCol[j]);
        delay(timer);
        offLED(ledRow[i],ledCol[j]);
      }
      for(int j = 5; j > 2; j--)   // light LED's 6 - 4 on/off on top level
      {
        onLED(ledRow[i],ledCol[j]);
        delay(timer);
        offLED(ledRow[i],ledCol[j]);
      }
 
  }
 
  }
 
void loop()
{
  rowPaneFigure8();
}



This saved 550 bytes, so thanks for your help Pedro.
« Last Edit: November 17, 2012, 06:18:47 pm by Pedro147 » Logged


Pages: [1]   Go Up
Jump to: