Arduino LED Matrix Stacking Game

by 786969 in Circuits > Arduino

13 Views, 0 Favorites, 0 Comments

Arduino LED Matrix Stacking Game

IMG_2160.JPG

A four-block-wide platform moves back and forth horizontally across the screen. When the player presses the push button, the moving block stops and is placed on top of the previous layer. Any portion of the block that does not overlap with the layer below is cut off, making the next block smaller. After every successful placement, the speed of the moving block increases, making the game progressively more difficult.


Sources:

  1. Reddit
  2. GitHub
  3. Arduino Forum

Supplies

  1. Arduino
  2. 8×8 LED Matrix
  3. Two 74HC595 shift registers (if not already integrated with the LED matrix)
  4. Push button
  5. 8 330 Ω resistors
  6. Breadboard

Wiring the LED Matrix

IMG_2071.jpg
IMG_2072.jpg
wiring diagram.png
IMG_2096.JPG

Wire it exactly like the image/diagram to the shift registers.

Connect the Arduino pins to the corresponding matrix pins:

Arduino pin 8 connects to Latch, pin 11 connects to Data, Arduino pin 12 connects to Clock, 5V connects to VCC, and GND connects to GND. Keep the matrix rotated so that the 788BS label is on the bottom, since that's the orientation that will work properly.

Add the Button

IMG_2159.JPG

Use a simple pushbutton and place it with two legs on one side of the breadboard's bridge and the other two legs on the other side of the bridge. Wire the push button to Arduino pin 2 and to GND, no resistor is needed because we use the Arduino's internal resistor, with the code: pinMode(2, INPUT_PULLUP);

Test the Button

Upload the code and open the serial monitor at 9600 baud, and press the button, monitor should print "Pressed":


const int buttonPin = 2;


void setup()

{

pinMode(buttonPin, INPUT_PULLUP);

Serial.begin(9600);

}


void loop()

{

if (digitalRead(buttonPin) == LOW)

{

Serial.println("Pressed");

delay(200);

}

}

Test the Matrix

Upload the code:


const int latchPin = 8;//Pin connected to ST_CP of 74HC595

const int clockPin = 12;//Pin connected to SH_CP of 74HC595

const int dataPin = 11; //Pin connected to DS of 74HC595

int data[] = {

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",0*/

0xFF,0xC1,0xBE,0xBE,0xBE,0xC1,0xFF,0xFF,/*"0",1*/

0xFF,0xDF,0xDF,0x80,0xFF,0xFF,0xFF,0xFF,/*"1",2*/

0xFF,0xDC,0xBC,0xBA,0xB6,0xCE,0xFF,0xFF,/*"2",3*/

0xFF,0xDD,0xBE,0xB6,0xB6,0xC9,0xFF,0xFF,/*"3",4*/

0xFB,0xF3,0xEB,0xDB,0x80,0xFB,0xFF,0xFF,/*"4",5*/

0xFF,0x8D,0xAE,0xAE,0xAE,0xB1,0xFF,0xFF,/*"5",6*/

0xFF,0xC1,0x96,0xB6,0xB6,0xD9,0xFF,0xFF,/*"6",7*/

0xFF,0xBF,0xBC,0xB3,0xAF,0x9F,0xFF,0xFF,/*"7",8*/

0xFF,0xC9,0xB6,0xB6,0xB6,0xC9,0xFF,0xFF,/*"8",9*/

0xFF,0xCD,0xB6,0xB6,0xB4,0xC1,0xFF,0xFF,/*"9",10*/

0xFC,0xF3,0xCB,0x9B,0xEB,0xF3,0xFC,0xFF,/*"A",11*/

0xFF,0x80,0xB6,0xB6,0xB6,0xB6,0xC9,0xFF,/*"B",12*/

0xFF,0xE3,0xDD,0xBE,0xBE,0xBE,0xBE,0xDD,/*"C",13*/

0xFF,0x80,0xBE,0xBE,0xBE,0xBE,0xDD,0xE3,/*"D",14*/

0xFF,0x80,0xB6,0xB6,0xB6,0xB6,0xBE,0xFF,/*"E",15*/

0xFF,0x80,0xB7,0xB7,0xB7,0xB7,0xBF,0xFF,/*"F",16*/

0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,/*" ",17*/

};

void setup ()

{

//set pins to output

pinMode(latchPin,OUTPUT);

pinMode(clockPin,OUTPUT);

pinMode(dataPin,OUTPUT);

}

void loop()

{


for(int n = 0; n < 136; n++)

{

for(int t = 0;t < 10;t ++)//Show repeated 10 times

{

int dat = 0x01;

for(int num = n; num < 8+n; num++)

{


shiftOut(dataPin,clockPin,MSBFIRST,~data[num]);//control ROW of dot matrix

shiftOut(dataPin,clockPin,MSBFIRST,~dat); //control COL of dot matrix

//return the latch pin high to signal chip that it

//no longer needs to listen for information

digitalWrite(latchPin,HIGH); //pull the latchPin to save the data

//delay(1); //wait for a microsecond

digitalWrite(latchPin,LOW); //ground latchPin and hold low for as long as you are transmitting

dat = dat<<1;

delay(1); //wait for a microsecond

}

}

}

}



After uploading, the matrix should display the characters 0 1 2 3 4 5 6 7 8 9 A B C D E F one by one, if it works then it is wired correctly, if not then there is a wiring or orientation issue, look for wrong wiring or rotate the matrix if wiring is correct.

Create the Display Buffer

Instead of controlling individual LEDs directly, the game stores the current state of the display in memory using an array. This array represents the eight rows of the LED matrix. byte screen[8]; By updating the array and refreshing the display continuously, animations and game mechanics can be created without constant flickering making it unusable.

Program the Moving Block

The game begins with a four-block platform moving back and forth across the bottom of the matrix. As the block reaches either side of the display, its direction reverses. This moving block forms the foundation of the tower.

Stop the Block With the Button

When the player presses the pushbutton, the moving block stops and is placed. A new row is then created one level higher, where another moving block begins traveling across the display.

Detect Overlap

Only the portion of the new block that overlaps with the previous row remains. Any section that extends beyond the block below is removed.

Increase the Speed

After each successful stack, the movement speed increases slightly, making the game progressively more challenging.

Detect Game Over

If a row fails to overlap with the previous row, no blocks remain. When this happens, the game ends and the player loses.

Restart the Game

After a game over, pressing the button resets the game, allowing the player to begin a new game.

Enjoy the Finished Game

This completed project makes an addicting game using an Arduino, two 74HC595 shift registers, a pushbutton, and an 8×8 LED matrix. As the player builds higher, the blocks become smaller and the speed increases, requiring increasingly precise timing to achieve the highest score possible.

Downloads