This is a code for a 3x3x3 LED cube. This cube is run off of 12 pins. To wire this cube, connect all of the anodes on a given level and connect all of the cathodes down vertically in one column. int anode [] ={5, 6, 7, 9, 10, 11, 12, 13, 8}; // sets an aray for lights to be used int anode2 [] ={8, 13, 12, 11, 10, 9, 7, 6, 5}; // opposeite aray int catone [] ={2, 2, 2, 2, 2, 2, 2, 2, 2}; // negative for bottom level int cattwo [] ={3, 3, 3, 3, 3, 3, 3, 3, 3}; // negative for middle level int catthree [] ={4, 4, 4, 4, 4, 4, 4, 4, 4}; // negative for top level int x; //sets variables int d; int c; void setup () { } void one(int wait) // bottom level light sequence // wait can be defined in the void loop { for(x=0; x<=8; x++) // variable for different numbers in // the aray corresponding to there place { pinMode(anode[x], OUTPUT); // sets as output pinMode(catone[x], OUTPUT); digitalWrite(anode[x], HIGH); // turns on positives digitalWrite(catone[x], LOW); // turns on negatives delay(wait); // if you don't know what delay is your a moron pinMode(anode[x], INPUT); //sets them back to inputs so the lights turn off pinMode(catone[x], INPUT); } } void two(int wait) { for(x=0; x<=8; x++) { pinMode(anode2[x], OUTPUT); pinMode(cattwo[x], OUTPUT); digitalWrite(anode2[x], HIGH); digitalWrite(cattwo[x], LOW); delay(wait); pinMode(anode2[x], INPUT); pinMode(cattwo[x], INPUT); } } void three(int wait) { for(x=0; x<=8; x++) { pinMode(anode[x], OUTPUT); pinMode(catthree[x], OUTPUT); digitalWrite(anode[x], HIGH); digitalWrite(catthree[x], LOW); delay(wait); pinMode(anode[x], INPUT); pinMode(catthree[x], INPUT); } } void column(int wait) // loops through columns { for(x=0; x<=8; x++) // loops through the aray { pinMode(anode[x], OUTPUT); for(d=1; d<=wait; d++) // delay on each column { for(c=2; c<=4; c++) // loops through lights on a column { pinMode(c, OUTPUT); digitalWrite(anode[x], HIGH); digitalWrite(c, LOW); pinMode(c, INPUT); delayMicroseconds(100); } } pinMode(anode[x], INPUT); } } void levelone (int wait) // turns entire level on { for(d=1; d<=wait; d++) { for(x=0; x<=8; x++) { pinMode(anode[x], OUTPUT); pinMode(catone[x], OUTPUT); digitalWrite(anode[x], HIGH); digitalWrite(catone[x], LOW); delayMicroseconds(100); // loops really fast so it looks like pinMode(anode[x], INPUT); // all the lights are one at the same time pinMode(catone[x], INPUT); } } } void leveltwo (int wait) { for(d=1; d<=wait; d++) { for(x=0; x<=8; x++) { pinMode(anode[x], OUTPUT); pinMode(cattwo[x], OUTPUT); digitalWrite(anode[x], HIGH); digitalWrite(cattwo[x], LOW); delayMicroseconds(100); pinMode(anode[x], INPUT); pinMode(cattwo[x], INPUT); } } } void levelthree (int wait) { for(d=1; d<=wait; d++) { for(x=0; x<=8; x++) { pinMode(anode[x], OUTPUT); pinMode(catthree[x], OUTPUT); digitalWrite(anode[x], HIGH); digitalWrite(catthree[x], LOW); delayMicroseconds(100); pinMode(anode[x], INPUT); pinMode(catthree[x], INPUT); } } } void randlight(int repeat) // number of lights turned on { randomSeed (millis()); // random lights for (int d=1; d<=repeat; d++) { x=random(0,9); c=random(2,5); pinMode(anode[x], OUTPUT); pinMode(c, OUTPUT); digitalWrite(anode[x], HIGH); digitalWrite(c, LOW); delay(100); pinMode(anode[x], INPUT); pinMode(c, INPUT); } } void loop () { one(50); two(50); three(50); two(50); one(50); column(500); levelthree(500); leveltwo(500); levelone(500); one(75); leveltwo(500); two(75); levelthree(500); three(75); randlight(100); } |