Overview of the Arduino breadboard snake game
To use this tutorials please make sure you already have platformio setup on your computer. See Learning Arduino GitHub Repository for more details.
The source code for this project is available on my GitHub Repository.
- Arduino Nano (adapt if using a different board)
- Fullsize breadboard
- 240×320 2.2″ TFT SPI display
- 7 x 1k Ohm resistor
- 6 x 2k Ohm resitor
- 30 breadboard jumper wires
- breadboard push-to-make button
The circuit basically consists of a Arduino Nano as the main controller. A 5v/3.3V level shifter for the SPI lines was made using a resistor voltage divider (I tried using a IC to do the level shifting but it aparently couldn’t keep up with the high SPI frequency). I only had one push button, but ideally you need 2 buttons to be able to turn left or right, instead of just circling in one direction.
Make sure when connecting your LCD what voltages it needs, and check the pin layout – it might not match exactly like my diagram.
Running the code
git pull https://github.com/trojanc/learn-arduino cd learn-arduino/snake platformio init platformio run
Source code notes
You might noticed that I did not use the Adafruit graphics library. I initially did use it, but it performed too slow for even a simple game like this. I then found the library PDQ_GFX_Libs which is forked from the original Adafruit libraries with a lot of performance improvements – they claim somewhere between 2 to 12 times faster. This library help take away the “choppyness” of screen refreshes.
I also took a few shortcuts to prevent whole screen updates. It only re-draws where something has changed. For example when the snake moves, we only have to draw over the background (or apple) where the head is at, and put back the background where the tail used to be. Doing this we only have to redraw two 10×10 pixel blocks for every movement.
- ADD ANOTHER BUTTON! You need at least 2 buttons to properly play
- Use hardware debouncing for button presses
- Find a IC for level shifting that works
- Maybe start putting together a reusable circuit in a nicer packaging so that someone can play this without the risk of letting a jumper wire pop out…