Go Down

### Topic: 3 x 3 x 3 LED Cube code (Read 3143 times)previous topic - next topic

#### Pedro147

##### Nov 17, 2012, 02:35 am
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: [Select]
` //Code based on ABrushFX Cube code at//http://www.abrushfx.com/Arduino/ledcube3.html//Array for three LED levelsint ledRow[] ={ 11, 12, 13 };// Array for nine LED's in each levelint 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 positionvoid onLED(int ledRow, int ledCol){    digitalWrite(ledRow, HIGH);   digitalWrite(ledCol, HIGH);   }// Sets an LED LOW based on Row and Column positionvoid 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  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.
http://www.pedroduino.com

#### johnwasser

#1
##### Nov 17, 2012, 03:39 am
Code: [Select]
`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]);      }  }`

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

#### Pedro147

#2
##### Nov 17, 2012, 06:50 am
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: [Select]
`//Code based on ABrushFX Cube code at//http://www.abrushfx.com/Arduino/ledcube3.html//Array for three LED levelsint ledRow[] ={ 11, 12, 13 };// Array for nine LED's in each levelint 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 positionvoid onLED(int ledRow, int ledCol){  digitalWrite(ledRow, HIGH);   digitalWrite(ledCol, HIGH); }// Sets an LED LOW based on Row and Column positionvoid 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();}  `
http://www.pedroduino.com

#### johnwasser

#3
##### Nov 17, 2012, 03:14 pm
Replace
Code: [Select]
`topRowPaneFigure8();centreRowPaneFigure8();bottomRowPaneFigure8();`
with
Code: [Select]
`rowPaneFigure8(0);rowPaneFigure8(1);rowPaneFigure8(2);`
or
Code: [Select]
`for (int i=0; i<3; i++)    rowPaneFigure8(i);`
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

#### Pedro147

#4
##### Nov 18, 2012, 12:15 amLast Edit: Nov 18, 2012, 12:18 am by Pedro147 Reason: 1
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: [Select]
`onLED(ledRow[i],ledCol[j]);delay(timer);                       offLED(ledRow[i],ledCol[j]);`

with the for loop

Code: [Select]
`for (int i = 0; i < 3; i++)`

so I ended up with

Code: [Select]
`        //Code based on ABrushFX Cube code at//http://www.abrushfx.com/Arduino/ledcube3.html//Array for three LED levelsint ledRow[] ={ 11, 12, 13 };// Array for nine LED's in each levelint 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 positionvoid onLED(int ledRow, int ledCol){    digitalWrite(ledRow, HIGH);   digitalWrite(ledCol, HIGH);   }// Sets an LED LOW based on Row and Column positionvoid 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.
http://www.pedroduino.com

Go Up

Please enter a valid email to subscribe