Go Down

Topic: My first Arduino project. 3x3x3 cube (Read 716 times) previous topic - next topic

RosDaddy

So I currently am going to school right now for electronics and I thought it would be fun to buy an ardunio and play with it. My first project is a 3x3x3 LED cube and it came out really great! The soldering was easy but the programming is a bit more of a pain. I hooked each column to pins 1-9 and hooked up each ground to A1,A2 and A3. Someone told me I should use transistors for my grounds but I don't really see a reason why. Right now if I want to turn a layer on I set the the correct cathode pin to low and the correct anode to high.
I would like to make a 5x5x5 cube but i need to become better with programming, and experiment with shift registers first.



Question 1- Should I be using transistors to switch the cathode layers to ground?

Questions 2- How do I write code that will repeat itself a specific number of times? Right now I write the code and then copy and paste it 10x to make it loop.

Questions 3- How can improve my code or my hardware?





http://youtu.be/KXg24e9ouBk

Code: [Select]
//3x3x3 Led Cube First Try!


int led1 = 1;
int led2 = 2;
int led3 = 3;
int led4 = 4;
int led5 = 5;
int led6 = 6;
int led7 = 7;
int led8 = 8;
int led9 = 9;

int ledA1 = A1;
int ledA2 = A2;
int ledA3 = A3;

// the setup routine runs once when you press reset:
void setup() {                
 // initialize the digital pin as an output.
pinMode(led1, OUTPUT);    
pinMode(led2, OUTPUT);    
pinMode(led3, OUTPUT);    
pinMode(led4, OUTPUT);    
pinMode(led5, OUTPUT);    
pinMode(led6, OUTPUT);    
pinMode(led7, OUTPUT);    
pinMode(led8, OUTPUT);    
pinMode(led9, OUTPUT);    
pinMode(ledA1, OUTPUT);    
pinMode(ledA2, OUTPUT);
pinMode(ledA3, OUTPUT);  
}

// the loop routine runs over and over again forever:
void loop() {

 
 digitalWrite(ledA1, LOW);    
 digitalWrite(ledA2, LOW);    
 digitalWrite(ledA3, LOW);  
 
 digitalWrite(led5, HIGH);
 digitalWrite(led9, HIGH);
 delay(50);
 digitalWrite(led9, LOW);
 
 
 digitalWrite(led8, HIGH);
  delay(50);
 digitalWrite(led8, LOW);

 
 digitalWrite(led7, HIGH);
  delay(50);
 digitalWrite(led7, LOW);
 
 
 digitalWrite(led4, HIGH);
  delay(50);
 digitalWrite(led4, LOW);
 
 
 digitalWrite(led1, HIGH);
  delay(50);
 digitalWrite(led1, LOW);
 
 
 digitalWrite(led2, HIGH);
  delay(50);
 digitalWrite(led2, LOW);
 
 
 digitalWrite(led3, HIGH);
  delay(50);
 digitalWrite(led3, LOW);
 
 
 digitalWrite(led6, HIGH);
  delay(50);
 digitalWrite(led6, LOW);
 
   
 digitalWrite(ledA1, LOW);    
 digitalWrite(ledA2, LOW);    
 digitalWrite(ledA3, LOW);  
 
 digitalWrite(led5, HIGH);
 digitalWrite(led9, HIGH);
 delay(50);
 digitalWrite(led9, LOW);
 
 
 digitalWrite(led8, HIGH);
  delay(50);
 digitalWrite(led8, LOW);

 
 digitalWrite(led7, HIGH);
  delay(50);
 digitalWrite(led7, LOW);
 
 
 digitalWrite(led4, HIGH);
  delay(50);
 digitalWrite(led4, LOW);
 
 
 digitalWrite(led1, HIGH);
  delay(50);
 digitalWrite(led1, LOW);
 
 
 digitalWrite(led2, HIGH);
  delay(50);
 digitalWrite(led2, LOW);
 
 
 digitalWrite(led3, HIGH);
  delay(50);
 digitalWrite(led3, LOW);
 
 
 digitalWrite(led6, HIGH);
  delay(50);
 digitalWrite(led6, LOW);
 
   
 digitalWrite(ledA1, LOW);    
 digitalWrite(ledA2, LOW);    
 digitalWrite(ledA3, LOW);  
 
 digitalWrite(ledA2, HIGH);    // turn the LED off by making the voltage LOW
 digitalWrite(ledA3, HIGH);    // turn the LED off by making the voltage LOW

 
 
 
 digitalWrite(led1, HIGH);   // Down Up sweep NO OFF
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 digitalWrite(led4, HIGH);
 digitalWrite(led5, HIGH);
 digitalWrite(led6, HIGH);
 digitalWrite(led7, HIGH);
 digitalWrite(led8, HIGH);
 digitalWrite(led9, HIGH);
 
 digitalWrite(ledA1, LOW);    
 delay(75);  
digitalWrite(ledA1, HIGH);
 delay(1);  
 
 digitalWrite(ledA2, LOW);    
 delay(75);  
digitalWrite(ledA2, HIGH);
 delay(1);  
 
 digitalWrite(ledA3, LOW);    
 delay(75);  
digitalWrite(ledA3, HIGH);
 delay(1);  

digitalWrite(ledA2, LOW);    
 delay(75);  
digitalWrite(ledA2, HIGH);
 delay(1);  
 
 digitalWrite(ledA2, HIGH);    // turn the LED off by making the voltage LOW
 digitalWrite(ledA3, HIGH);    // turn the LED off by making the voltage LOW

 
 
 digitalWrite(led1, HIGH);   // Down Up sweep NO OFF
 digitalWrite(led2, HIGH);
 digitalWrite(led3, HIGH);
 digitalWrite(led4, HIGH);
 digitalWrite(led5, HIGH);
 digitalWrite(led6, HIGH);
 digitalWrite(led7, HIGH);
 digitalWrite(led8, HIGH);
 digitalWrite(led9, HIGH);
 
 digitalWrite(ledA1, LOW);    
 delay(75);  
digitalWrite(ledA1, HIGH);
 delay(1);  
 
 digitalWrite(ledA2, LOW);    
 delay(75);  
digitalWrite(ledA2, HIGH);
 delay(1);  
 
 digitalWrite(ledA3, LOW);    
 delay(75);  
digitalWrite(ledA3, HIGH);
 delay(1);  

digitalWrite(ledA2, LOW);    
 delay(75);  
digitalWrite(ledA2, HIGH);
 delay(1);  
 
 digitalWrite(ledA2, HIGH);    // turn the LED off by making the voltage LOW
 digitalWrite(ledA3, HIGH);    // turn the LED off by making the voltage LOW

 digitalWrite(ledA3, HIGH);    
 digitalWrite(ledA1, HIGH);
 digitalWrite(led1, LOW);   // Down Up sweep NO OFF
 digitalWrite(led2, LOW);
 digitalWrite(led3, LOW);
 digitalWrite(led4, LOW);
 digitalWrite(led5, LOW);
 digitalWrite(led6, LOW);
 digitalWrite(led7, LOW);
 digitalWrite(led8, LOW);
 digitalWrite(led9, LOW);
  delay(1);
 
 
 
 digitalWrite(led1, HIGH);   // Left Right Sweep

 digitalWrite(led4, HIGH);
 
 digitalWrite(led7, HIGH);

 digitalWrite(ledA1, LOW);    
 digitalWrite(ledA2, LOW);    
 digitalWrite(ledA3, LOW);    
 delay(75);  
 digitalWrite(led1, LOW);  
 digitalWrite(led4,  LOW);
 digitalWrite(led7,  LOW);
 delay(1);
 
digitalWrite(led2, HIGH);  
digitalWrite(led5, HIGH);
digitalWrite(led8, HIGH);
delay(75);
digitalWrite(led2, LOW);  
digitalWrite(led5, LOW);
digitalWrite(led8, LOW);
 delay(1);

digitalWrite(led3, HIGH);  
digitalWrite(led6, HIGH);
digitalWrite(led9, HIGH);
delay(75);
digitalWrite(led3, LOW);  
digitalWrite(led6, LOW);
digitalWrite(led9, LOW);
 delay(1);

digitalWrite(led2, HIGH);  
digitalWrite(led5, HIGH);
digitalWrite(led8, HIGH);
delay(75);
digitalWrite(led2, LOW);  
digitalWrite(led5, LOW);
digitalWrite(led8, LOW);
 delay(1);


}


I had to shorten the code a ton to fit on this post

CrossRoads

How much current do you have going thru each LED? <4mA? Then you can get by without a transistor.
>4? Use a transistor so you don't damage the analog pins.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pedro147

#2
Feb 19, 2013, 08:54 am Last Edit: Feb 19, 2013, 08:56 am by Pedro147 Reason: 1
Great job RosDaddy, ten points.

I am definitely no coding expert, and all that I have learned has been with the help of the members of this great forum. Also studying and trying to understand, then implement / modify any code that you can find is of enormous help.  I would suggest looking at arrays and for loops initially.

Have a look at this code and see what you can do. There are much better cube code examples on this forum and the net generally but if you can see how this works you will be making an improvement  in your coding prowess  8)
Code: [Select]
/* Code by Pedro147 inspired by this -

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

*/

int ledRow[] = { 11, 12, 13 };
int ledCol[]= { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int timer= 10;




void setup()
{  
 for( int i =  0; i < 3; i++)

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

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

   {
     pinMode( ledCol[j],OUTPUT);
   }
 }
}


void onLED(int ledRow, int ledCol){

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

}


void offLED(int ledRow, int ledCol)
{
 digitalWrite(ledRow, LOW);
 digitalWrite(ledCol, LOW);
}


void allOff()
{
 for(int i = 3; i--;)
 {
   for(int j = 9; j--;)
   {
     offLED(ledRow[i],ledCol[j]);
   }
 }
}


void oneToNine_ColsOn()
{
 for(int j = 9; j--;)

 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}


void everySecond_ColsOn1()
{

 for(int j = 0; j < 9; j+=2)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}


void everySecond_ColsOn_2()
{

 for(int j = 1; j < 9; j+=2)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}
void lhWall_fToR()

{

 for(int j = 0; j < 9; j+=3)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}

void rhWall_fToR()

{

 for(int j = 2; j < 9; j+=3)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}


void frontWall_LtoR()
{
 for(int j = 0; j < 3; j++)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}

void rearWall_LtoR()
{
 for(int j = 6; j <9; j++)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(10);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}


void centreWall_LtoR()
{
 for(int j = 3; j < 6; j++)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(timer);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}

void centreWall_fToR()

{

 for(int j = 1; j < 9; j+=3)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(timer);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}

void diagWall_LtoR()
{
 for(int j = 0; j < 9; j+=4)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(timer);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}

void diagWall_RtoL()
{
 for(int j = 2; j < 8; j+=2)
 {
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(timer);
     digitalWrite(ledCol[j], HIGH);
   }
 }
}

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 frontTorear_flash1()
{
 for(int j = 0; j < 9; j+=3)
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(20);
     digitalWrite(ledCol[j], HIGH);
   }
}


void frontTorear_flash2()
{
 for(int j = 1; j < 9; j+=3)
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(20);
     digitalWrite(ledCol[j], HIGH);
   }
}


void frontTorear_flash3()
{
 for(int j = 2; j < 9; j+=3)
   for(int i = 3; i--;)
   {
     digitalWrite(ledRow[i], HIGH);
     delay(20);
     digitalWrite(ledCol[j], HIGH);
   }
}

void randomColumnsOnOff()
{
 int colmax = 9;
 int stCol  = random(0,colmax);

 {
   digitalWrite(ledRow[0], HIGH);
   digitalWrite(ledCol[stCol], HIGH);
   digitalWrite(ledRow[1], HIGH);
   digitalWrite(ledCol[stCol], HIGH);
   digitalWrite(ledRow[2], HIGH);
   digitalWrite(ledCol[stCol], HIGH);
   delay(timer);
   allOff();



 }
}
void loop()
{
 oneToNine_ColsOn();
 delay(10);
 allOff();
 delay(10);
 everySecond_ColsOn1();
 delay(10);
 allOff();
 delay(10);
 everySecond_ColsOn_2();
 delay(10);

 oneToNine_ColsOn();
 delay(10);
 allOff();
 delay(10);
 everySecond_ColsOn1();
 delay(10);
 allOff();
 delay(10);
 everySecond_ColsOn_2();
 delay(10);


 lhWall_fToR();
 delay(100);
 allOff();
 centreWall_fToR();
 delay(100);
 allOff();
 rhWall_fToR();
 delay(100);
 allOff();

 frontWall_LtoR();
 delay(100);
 allOff();
 centreWall_LtoR();
 delay(100);
 allOff();
 rearWall_LtoR();
 delay(100);
 allOff();

 rowPaneFigure8();

 centreWall_LtoR();
 allOff();
 diagWall_LtoR();
 allOff();
 centreWall_fToR();
 allOff();
 diagWall_RtoL();
 allOff();
 frontTorear_flash1();
 delay (timer);
 allOff();
 frontTorear_flash2();
 delay (timer);
 allOff();
 frontTorear_flash3();
 delay (timer);
 allOff();
 randomColumnsOnOff();
 randomColumnsOnOff();
 randomColumnsOnOff();
}







I assume that your cube is wired so that the nine LED's on any one level are led1 to led9, and the three levels are A1, A2 and A3. If so and you want to try the attached code, just change your cube to Arduino wiring to -

Led1 to led9   Arduino pins 2 to 10. And leave led1 led2 and led3 as they are connected to Arduino pins 11, 12 and 13. Upload the attached code and have a look and see what it is doing. As I said I am certainly no expert but hey it's all a fun learning process.

Regarding the use of transistors I would definitely listen to Crossroads advice. He is one of the best hardware guru's on the forum.

Have fun Pedro.  8)


Go Up