Arduino 3x3x3 Cube Code
Posted: Tue Oct 09, 2012 6:57 am
I just made a 3x3x3 cube code for the Arduino from scratch. It does not have full notes, though it has some. The animations are written in octal.
This code assumes that the layers are cathodes and the columns are anodes.
To hook it up, you have to do is hook pins 2, 3, and 4 to the layers, starting with the bottom one.
It should look something like:
4 Top
3 Middle
2 Bottom
Now the column pins are pins 5-13. Hook them up left to right starting from the back, then to the middle, then front.
It should look something like this:
5 6 7 back
8 9 10 middle
11 12 13 front
Don't forget whatever resistors needed, then it should be all hooked up.
Here is how to program it:
1. Find the animation1[][5] variable.
2. Write the desired code in octal (convert desired binary number to octal)
The code looks something like this: {0123, 0123, 0123, 1999} note: the 8 and 9 are INVALID characters since it is written in octal!
The 0s (zeros) in front of the first three numbers need to stay there, it tells the Arduino that it is an octal number.
The first number, the first "0123" in the example, is the bottom layer. The characters in it go from back to front. So, the 1 is the back column group, 2 is the middle column group, 3 the front column group.
The second number, also "0123", is the middle row. Same thing goes for the column groups.
The third number is obviously the top row
The last number though (the fourth one) is the delay. Since it is 1999, it will stay on that animation step for 1.999 seconds).
3. The code does NOT have an automatic end, so you will have to insert a manual one. instead of the original code in the animation (such as "{0123, 0123, 0123, 1999},") you will need to insert "{END}"
Good luck guys, I would love to hear the complaints and comments.
This code assumes that the layers are cathodes and the columns are anodes.
To hook it up, you have to do is hook pins 2, 3, and 4 to the layers, starting with the bottom one.
It should look something like:
4 Top
3 Middle
2 Bottom
Now the column pins are pins 5-13. Hook them up left to right starting from the back, then to the middle, then front.
It should look something like this:
5 6 7 back
8 9 10 middle
11 12 13 front
Don't forget whatever resistors needed, then it should be all hooked up.
Here is how to program it:
1. Find the animation1[][5] variable.
2. Write the desired code in octal (convert desired binary number to octal)
The code looks something like this: {0123, 0123, 0123, 1999} note: the 8 and 9 are INVALID characters since it is written in octal!
The 0s (zeros) in front of the first three numbers need to stay there, it tells the Arduino that it is an octal number.
The first number, the first "0123" in the example, is the bottom layer. The characters in it go from back to front. So, the 1 is the back column group, 2 is the middle column group, 3 the front column group.
The second number, also "0123", is the middle row. Same thing goes for the column groups.
The third number is obviously the top row
The last number though (the fourth one) is the delay. Since it is 1999, it will stay on that animation step for 1.999 seconds).
3. The code does NOT have an automatic end, so you will have to insert a manual one. instead of the original code in the animation (such as "{0123, 0123, 0123, 1999},") you will need to insert "{END}"
Good luck guys, I would love to hear the complaints and comments.
Code: Select all
//a 3x3x3 LED cube for the arduino, direct connecting the pins.
//Originally made by Joshua Little 10/8/12
//Open source
byte cubePin[13] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; /*The pins going to the cube. The first three are the 3 rows, and the other 9 are the columns.
The rows go from buttom to top, and the columns go from left to right, back to front
Default: {2, 3, 4, 5 ,6 ,7 ,8 ,9 10, 11, 12, 13}
Rows: 4 top Comumns: 5 6 7 farthest away from the front
3 middle 8 9 10 middle
2 bottom 11 12 13 the closest LEDs to you */
unsigned int END = 63013; //Tells the animation to loop back to the begining. The value is a random value that will not conflict with the 3 digit octal values in the animations
//the animations to be played written in octal. It REQUIRES the 0 in front of the number. The last number is the delay.
//the first number after the initial 0 is the 3 back columns, second is 3 middle ones, and third is 3 front ones
//the first 0xxx number is the bottom row, second middle row, third top row
//so, if I wanted a the bottom row to look like this: 0 1 0 I would convert the top column set to octal: 2
// 1 0 1 Then convert the middle column set: 5
// 0 1 0 Bottom: 2
// So the first number in the set would be: 0252.
//If you wanted all 3 rows to be the same and the delay to be 250ms (0.25sec), then you would put {0252, 0252, 0252, 250}
int animation1[][5] = {
{0700, 0070, 0007, 250},
{0000, 0777, 0000, 250},
{0007, 0070, 0700, 250},
{0070, 0070, 0070, 250},
{END} //tells the animation to return to the first step
};
unsigned long time; //millis()
unsigned long timer; //the amount of time past since the last animation step
int timerCount = 0; // the amount of animations steps past
int rowData[2][4] = {{0, 0, 0}}; //temporarily stores the animation step values to display them on the cube.
void setup(){
for(int x=0; x < 14; x++){ //sets the cube pins as outputs
pinMode(cubePin[x], OUTPUT);
}
}
void loop(){
time = millis(); //time is the amount of time the Arduino has been on
cubeDisplayUpdate(); //computes what animation step should be played
cubeDisplay(); //displays the chosen animation step
}
void cubeDisplay(){
for(int y = 0; y < 3; y++){ //start with the bottom row then go to the middle, then top
digitalWrite(cubePin[y], LOW); //turns on the current row !!!if the rows are anodes, then this should be HIGH. If they are cathodes, set to LOW.
for(int x = 0; x < 9; x++){
if(bitRead(rowData[0][y], (8-x))==1){ //turns on the LEDs in the current row
digitalWrite(cubePin[(3+x)], HIGH); //3+x is the first !!!if the columns are anodes, then this should be HIGH. If they are cathodes, set to LOW.
}
}
for(int x = 0; x < 9; x++){ //turns off the all the column LEDs
digitalWrite(cubePin[3+x], LOW); // !!!if the columns are anodes, then this should be LOW. If they are cathodes, set to HIGH.
}
digitalWrite(cubePin[y], HIGH); //turns off the current row !!!if the rows are anodes, then this should be LOW. If they are cathodes, set to HIGH.
}
}
void cubeDisplayUpdate(){
if(time >= (timer + animation1[timerCount][3])){ //switches the animation step once the animation delay time has been reached
timerCount++; //timerCount decides which animation step to display
timer = time; //updates timer to the amount of time the arduino has been on. This allows the arduino to tell the amount of time since the steps were changed.
}
if(animation1[timerCount][0]==END){ //if {END} is found in the animation code, then go back to the first animation step
timerCount = 0;
}
for(int x = 0; x < 3; x++){
rowData[0][x] = animation1[timerCount][x]; //updates the current animation step to the rowData string
}
}