Arduino Snake Game- 16x32 RGB LED Matrix
by maxnelson0 in Circuits > Arduino
1208 Views, 3 Favorites, 0 Comments
Arduino Snake Game- 16x32 RGB LED Matrix
Authors:
Max Nelson - MNelson3819@csum.edu
Luke Van Scherrenburg - LVanSherrenbur7875@csum.edu
Sean Nussdorfer - SNussdorfer4066@csum.edu
Acknowledgements:
California State University Maritime Academy - Engineering Technology Department
Professor Evan Chang-Siu (ET370)
Erin Cole (Makerspace)
The Project:
Snake game is a very simple game where the player controls a "snake" in a bounded area. The goal is to move the snake to the food while avoiding hitting the walls or the snake's tail. Each piece of food will grow the snake longer, and the game becomes more difficult. Our project uses a 16x32 RGB LED matrix as the display for the game and is driven by an Arduino Uno. The snake can be controlled by either the buttons or the joystick. See below for a YouTube video on our project!
YouTube video link: https://youtu.be/80b4hdcXWq8
Supplies
Project Functionality
This project uses some simple circuit components, along with an LED matrix, to create a snake game. There are a few custom 3D printed parts for this project that are optional, but when printed this project will produce a self contained snake game station. We have also designed a custom PCB that was milled out using a Bantam tools desktop PCB mill. This is an optional part, but if you have access to a PCB mill it is a very cool experience and produces something to be proud of.
Snake game is a basic 2D game. The snake is bounded on a flat plane, and the goal is to grow your snake. The snake grows when it "eats" a piece of food. The controls are very simple and can be seen in the figure above (apologies for the blurry image, computer resized it). There are two ways you can die in Snake game, the first being hitting the edges of the play area, and the second is by hitting the head of the snake against the body of the snake. The game gets harder each time the snake grows. There are many examples of this game available online for your enjoyment.
Circuit Diagram
This circuit diagram shows the physical connections required to make this project function. Note that we could not display the ribbon cable on the circuit diagram, but we have listed the connections needed to make the project function. The second image is of the ribbon cable with labeled connections for reference when assembling. Note the three grounds on the ribbon cable. Those grounds can be connected to the common ground on the breadboard, or to grounds on the Arduino. More on physical assembly can be found in the manufacturing/ assembly section. The connection to pin A5 is not connected to anything intentionally, but still has a short jumper in it.
If assembling this project on a breadboard: follow this circuit diagram for building your project.
If assembling this project with the 3D printed housing: see manufacturing/ assembly section for more information on wiring.
Verify Power Consumption
To assure that we would provide ample power to the device, we opted for the recommended power supply for our matrix: a 5V, 2A power supply. When the 16x32 matrix is fully powered with all LEDs on full brightness, it requires 2A of current. However, our project has the LEDs set to 40% brightness. Assuming that the current draw is linear, we expect the matrix to pull about 800mA. The Arduino pulls about 42mA. Overall, the current draw is much less than what the supply is able to provide.
State Machine Diagram
The states that are controlled are pretty much just going between three main states: the main menu, the game and the end screen. These states are all transitioned between by another state: the screen wipe. The transitions are all done by the user inputting something while the end screen is brought about by the player losing the game.
The Code
To make the snake game work on the Arduino while having enough processing power to drive the matrix as well requires quite a few libraries and tricks. The two .h files are custom made libraries. One of them adds a variable that holds an x and y byte to represent a position on a 2D-plane. The second one handles a lot of the game logic, this is done mostly to make the main code file much easier to read as well as being mostly controlling the state as well as controlling the matrix. The snake has a max length of 256, due to the limitations of the memory in the Arduino. To combat the low memory almost every variable is a byte instead of an int due to a byte using lower memory and staying within the range of numbers that was needed.
CAD Drawings
The button board files above are for manufacturing the custom circuit board for the buttons. These were designed using Eagle CAD and were manufactured using a Bantam Tools desktop PCB mill.
The files for the 3D printed housing are attached above and are available for download. Some modification may be required if the custom circuit board is not used, as the housing is designed with the custom circuit board in mind.
Manufacturing/ Assembly
Assembling this project is fairly straight forward if you do not choose to 3D print the housing. Without the 3D printed housing the project can be assembled on a breadboard, and the Arduino can be plugged into a computer to power it. Using the 3D printed housing requires a few additional steps. One of those steps is drilling some holes for the M3 mounting screws to pass through to the panel. The biggest one of note here is dealing with the ground connections from the ribbon cable. There are three ground connections from the ribbon cable, one from the joy stick, and one from the buttons, but there are not five grounds on the Arduino. We chose to solder together the three ground connections from the ribbon cable to make one singular connection to the Arduino. If you are using a breadboard you can skip this step.
Note in the first image in this section the screw driver is showing the ribbon cable going to the input channel on the LED panel. It is important to connect the ribbon cable here for proper operation.
The supplied power and ground cable for the LED matrix came very long, so we shortened the cable to make for easier wiring in the 3D printed housing. The connection from the cable to a power source was circular and had to be cut to fit into the barrel jack. This barrel jack is where the power supply plugs in and powers the Arduino, buttons, and joystick. Note in the last image that the supplied power cable was cut and jumpers were soldered in to provide power to the previously mentioned components.
If this project is being built on a breadboard we recommend powering the Arduino with a computer and sending 5V and ground to the breadboard, this will make the connections much simpler.
The cover image for this Instructable shows the project built in the 3D printed housing, with a custom circuit board for the buttons. To make this custom circuit board the Eagle CAD file is included, but a PCB mill is required. The one used for this project was a Bantam Tools desktop PCB milling machine. If you have access to one of these machines we highly recommend making the custom circuit board! If you do not have access to a PCB milling machine you can use a breadboard for the button connections.
When connecting the joystick in the 3D printed housing it may be necessary to shave off a bit of the print to make room for the connections. The image of the green housing with the arch shows where it was necessary to remove some material using a dremel.
Conclusion/ Take Aways
Overall, this project proved to be enjoyable, despite a couple twists and turns along the way. Through our combined efforts, we managed to implement our planned design features, in 3 identical units. Possible improvements that could be made to our design would be: better cable management (designed in the case itself), and optimizing the connections for our button PCB based on the configuration, and integrating a battery into the design. The design and troubleshooting process has garnered a newfound respect for the complexity of small device in our everyday lives, as well as respect for the R&D involved with such devices. With this, we realize that our process for designing and assembling the project could be improved upon (i.e., Starting earlier, leaving time to build multiple versions, dedicating more time to refining designs, having a backup plan). However, the experience gives us newfound confidence for working on future projects.