Hi,
So I've been working on a project to force myself to learn to code better. I built a multiplexed 8x8 LED matrix and have been trying to program an extremely primitive Tetris game on it. My last completely successful upload was a small-scale simulation wherein rows of LEDs fall and stack until they reach the top, and "GAME OVER" scrolls across.
Because I have no idea where/how to debug an Arduino program line-by-line (will accept advice here as well), so I transcribed my code into java and used Netbeans to debug it. Once I fixed everything I needed to get it to a checkpoint (just enough to stack random pieces down the middle until game over), I tried to upload it. Unfortunately, the matrix remains blank. I didn't change anything in the shift register or digital write functions, so I've pretty much hit a wall. Any suggestions?
int Latch = 11;
int Data = 12;
int Clock = 10;
int OE = 13;
int C1 = 8;
int C2 = 9;
int C3 = 2;
int C4 = 3;
int C5 = 4;
int C6 = 5;
int C7 = 6;
int C8 = 7;
int CPins[] = {
C1, C2, C3, C4, C5, C6, C7, C8};
int shiftCount = 0;
int letter = 0;
int newRow = 1;
int contGame = 1;
int pointer[2] = {
4,0};
int maxPiece = 0;
int posInd = 0;
int id = 0;
int count = 0;
//Set clear board starting point and letter templates
int allRows[11][11]={ ... };
int tempRows[11][11]={ ... };
int letterG[8][8] = {
{0,0,1,1,0,0,0,0 }
,{0,1,0,0,1,0,0,0 }
,{1,0,0,0,0,0,0,0 }
,{1,0,0,0,0,0,0,0 }
,{1,0,0,1,1,0,0,0 }
,{1,0,0,0,1,0,0,0 }
,{0,1,0,0,1,0,0,0 }
,{0,0,1,1,1,0,0,0 }
};
*Instantiate the rest of the letters*
//Create pieces in all positions
int pieceT[4][4][4] = {{
{1,1,1,0}
,{0,1,0,0}
,{0,0,0,0}
,{0,0,0,0}}
,{
{0,1,0,0}
,{1,1,0,0}
,{0,1,0,0}
,{0,0,0,0}}
,{
{0,1,0,0}
,{1,1,1,0}
,{0,0,0,0}
,{0,0,0,0}}
,{
{1,0,0,0}
,{1,1,0,0}
,{1,0,0,0}
,{0,0,0,0}
}
};
*Instantiate the other 6 Tetris pieces*
//How many shift register?
#define number_of_74hc595s 1
//Pins on this shift register
#define numOfRegisterPins number_of_74hc595s * 8
int val = 0; //val will be used to store the row
int row = 0; //of the input pin
int oldVal = 0;
boolean registers[numOfRegisterPins];
void setup() {
pinMode(Data, OUTPUT); //tell Arduino LED is an output
pinMode(Latch, OUTPUT);
pinMode(Clock, OUTPUT);
pinMode(OE, OUTPUT);
pinMode(C1, OUTPUT);
pinMode(C2, OUTPUT);
pinMode(C3, OUTPUT);
pinMode(C4, OUTPUT);
pinMode(C5, OUTPUT);
pinMode(C6, OUTPUT);
pinMode(C7, OUTPUT);
pinMode(C8, OUTPUT);
pinMode(14, INPUT);
pinMode(15, INPUT);
pinMode(16, INPUT);
//reset all register pins
clearRegisters();
writeRegisters();
Serial.begin(9600);
}
//Sets a proxy
void setTemp(){
for(int i = 0; i<=10; i++){
for(int j = 0; j<=10; j++){
tempRows[i][j]=allRows[i][j];
}
}
}
//Set all register pins to LOW
void clearRegisters(){
for(int i = numOfRegisterPins - 1; i>= 0; i--){
registers[i] = LOW;
}
}
boolean collisionDetection(){
for(int i = 0; i<=10; i++){
for(int j = 0; j<=10; j++){
if(tempRows[i][j]>maxPiece){
maxPiece = tempRows[i][j];
}
}
}
if(maxPiece>=2){
return true;
}else{
return false;
}
}
//Checks to see if piece has finished falling
/*void endCheck(){
for(int i = 3; i<=10; i++){
for(int j = 3; j<=10; j++){
if(tempRows[i][j]>maxPiece){
maxPiece = tempRows[i][j];
}
}
}
if(maxPiece>=2){
newRow = 1;
}else{
newRow = 0;
}
}*/
//Sets active piece randomly
void newPiece(){
posInd = 0;
id = abs(rand()%7);
for(int i = 0; i<=3; i++){
for(int j = 0; j<=3; j++){
for(int k = 0; k<=3; k++){
switch(id){
case 0:
activePiece[i][j][k] = pieceT[i][j][k];
break;
case 1:
activePiece[i][j][k] = pieceL[i][j][k];
break;
case 2:
activePiece[i][j][k] = pieceBackL[i][j][k];
break;
case 3:
activePiece[i][j][k] = pieceZ[i][j][k];
break;
case 4:
activePiece[i][j][k] = pieceBackZ[i][j][k];
break;
case 5:
activePiece[i][j][k] = pieceI[i][j][k];
break;
case 6:
activePiece[i][j][k] = pieceSq[i][j][k];
break;
}
}
}
}
}
//Makes active piece fall
void fallingPiece(){
if(newRow==1){
newRow = 0;
setTemp();
newPiece();
pointer[0] = 0;
pointer[1] = 4;
for(int i = 0; i<=3; i++){
for(int j = 0; j<=3; j++){
//put new piece at top
tempRows[pointer[0]+i][pointer[1]+j]=tempRows[pointer[0]+i][pointer[1]+j]+activePiece[posInd][i][j];
}
}
if(collisionDetection()==false){
if((allRows[10][0]+allRows[10][1]+allRows[10][2]+allRows[10][3]+allRows[10][4]+allRows[10][5]+allRows[10][6]+allRows[10][7])<
(tempRows[10][0]+tempRows[10][1]+tempRows[10][2]+tempRows[10][3]+tempRows[10][4]+tempRows[10][5]+tempRows[10][6]+tempRows[10][7])){
newRow=1;
}
for(int i = 0; i<=10; i++){
for(int j = 0; j<=10; j++){
allRows[i][j] = tempRows[i][j];
}
}
}else{
contGame = 0;
}
}else{
pointer[0]++;
for(int i = 0; i<=3; i++){
for(int j = 0; j<=3; j++){
//Put new piece a row down
if(pointer[0]+i<=10){
tempRows[pointer[0]+i][pointer[1]+j]=tempRows[pointer[0]+i][pointer[1]+j]+activePiece[posInd][i][j];
}
//Remove new piece from row above
if(pointer[0]<=10){
tempRows[(pointer[0]-1)+i][pointer[1]+j]=tempRows[(pointer[0]-1)+i][pointer[1]+j]-activePiece[posInd][i][j];
}
}
}
if(collisionDetection()==false){
maxPiece = 0;
for(int i = 0; i<=10; i++){
for(int j = 0; j<=10; j++){
allRows[i][j] = tempRows[i][j];
}
}
}else{
newRow = 1;
}
}
}
//When game ends, scrolls "GAME OVER"
void gameOver(){
int temp[8];
for(int i = 0; i<8; i++){
for(int j = 0; j<8; j++){
temp[j]=allRows[i+3][j];
}
for(int j = 0; j<7; j++){
allRows[i+3][j]=temp[(j+1)%8];
}
switch(letter){
case 0:
allRows[i+3][7] = letterG[i][shiftCount];
break;
case 1:
allRows[i+3][7] = letterA[i][shiftCount];
break;
case 2:
allRows[i+3][7] = letterM[i][shiftCount];
break;
case 3:
allRows[i+3][7] = letterE[i][shiftCount];
break;
case 4:
allRows[i+3][7] = 0;
break;
case 5:
allRows[i+3][7] = letterO[i][shiftCount];
break;
case 6:
allRows[i+3][7] = letterV[i][shiftCount];
break;
case 7:
allRows[i+3][7] = letterE[i][shiftCount];
break;
case 8:
allRows[i+3][7] = letterR[i][shiftCount];
break;
case 9:
allRows[i+3][7] = 0;
break;
}
}
shiftCount = (shiftCount + 1) % 8;
if(shiftCount==0){
letter=(letter+1)%10;
}
}
//Set and display registers
//Only call AFTER all values are set how you would like (slow otherwise)
void writeRegisters(){
digitalWrite(OE, LOW);
digitalWrite(Latch, LOW);
for(int i = numOfRegisterPins - 1; i>= 0; i--){
digitalWrite(Clock, LOW);
int val = registers[i];
digitalWrite(Data, val);
digitalWrite(Clock, HIGH);
}
digitalWrite(Latch, HIGH);
digitalWrite(OE, HIGH);
}
void clearColumns(){
for(int i = 0; i < 8; i++){
digitalWrite(CPins[i], LOW);
}
}
//set an individual pin HIGH or LOW
void setRegisterPin(int index, int value){
registers[index] = value;
}
void loop() {
for(int i=0;i<8;i++){
clearRegisters();
writeRegisters();
for(int j=0;j<8;j++){
setRegisterPin(j,allRows[i+3][j]);
}
clearColumns();
writeRegisters();
digitalWrite(CPins[i], HIGH);
}
if(count == 25){
if(contGame == 1){
//endCheck();
fallingPiece();
}else{
clearRegisters();
writeRegisters();
gameOver();
}
count=0;
}
}
count++;
}