Introduction: Arduino Snake LED Game

In a previous project we created an interactive coffee table, which lit up leds under an object. In this project, we reuse that table to create a snake game from the 2D matrix of LEDs. If some parts of the project are unclear, it may be addressed in the previous project, so I recommend reviewing that for more information.

Supplies

1. ws2811 LED Bulbs - https://amzn.to/3uuYKrr

2. 5V Power Supply - https://amzn.to/2PGh6qq

3. Arduino I used the 2560 - https://amzn.to/39Kj7ci

4. Analog Joy Stick - https://amzn.to/2PDUzun

Step 1: Create the LED Matrix

This is the crux of the entire thing and the size of the matrix will not matter. We used a 8x12 led matrix, but you could make any size you prefer. To get started we took a 1/8 inch piece of paper composite board and drew a diagram out of where we wanted to drill a point for each bulb. We spaced each led about 3 inches apart and drilled a 1/2 inch hole to squeeze the bulb in. After stringing up the leds, we used hot glue to fix them in place.

You can put this matrix up anywhere but we decided to house the board and electronics into a coffee table to make it easier to handle, and more pleasant to view. You can use whatever you'd like.

Step 2: Create the Circuit

At a high level we have a matrix of ws2811 LEDs powered by an external 5V power supply. They were arranged in a serpentine pattern and form a matrix of leds 8 x 12. This game will work with any size matrix with minor adjustments to the code. We use an arduino to control the lights in the matrix. See the circuit diagram and material list for more information on the exact parts and wiring.

The only thing different about this setup from the previous project is we added a joy stick which will be used to direct the snake in the game. The analog stick is a basic analog stick that actually comes with an arduino starter pack, but you can also get it from Amazon. Refer to the circuit diagram for more information on how to wire up the circuit. Luckily the analog stick only requires 3 inputs for the ardunio, 2 analog inputs to read x and y direction of the stick, and a digital input to tell if the analog stick is pressed in. All of the wiring and power supply was hidden within the coffee table.

Step 3: Install the Code

You can find the code in the following repo: https://github.com/tmckay1/snake_game

Make sure to install any dependencies mentioned in the README, for now you will need the LinkedList Module

The code is pretty self explanatory and has a lot of comments, but I will explain a bit about it here. We have an object Point that we use to represent a point on the 2D matrix of leds. We initialize the location of the apple with a point and the snake with a point. The snake is a linked list of points. The point of the game is to move the snake to the apple and grow the snake. When the head touches the apple we generate a random point for the apple to spawn again and grow the linked list that represents the snake. If the head of the snake touches the tail or goes out of bounds, then you will have to start over. You can pause the game by clicking into the joystick. I have included a visual here of what it looks like when the game is paused (notice the change in color).4

The only adjustments you may have to make is the section of code that renders the direction to travel. Depending on the orientation of your joystick, it may be necessary to change the direction the snake moves, as it may be different than the orientation of our joysticks.

Step 4: Upload the Code and Enjoy!

After you have created the led matrix, the circuit, and modified the code to suit your specific setup, you can upload the code to the arduino. Upon uploading the game will start automatically with the snake in the bottom left corner as a red dot traveling to the right. An apple will randomly be generated and you can start moving the snake around with the analog stick. When the game resets it will again start in the bottom left corner except a flashing animation will trigger to indicate the game is over. See an image I attached as a reference