ATtiny85 Snake Game
Introduction: ATtiny85 Snake Game
When it comes to electronics, it's the free-form designed ones that always catch my eye. Seeing how all the wires and components are connected just make it so fascinating and nice to look at. I wanted to try building something in a similar style. Being aware my soldering skills are not as good as I'd like them to be, I decided to go for a bit easier but still kind of nice-looking option. The whole build was a process of trial and error that probably cost me a bit more time than I'd like to admit. It was also the first time I ever used a ATtiny (or anything besides an Arduino Uno), which posed many challenges. But I ended up learning a lot throughout the making, so hopefully you'll do too!
Supplies
* 1 x 128x64 I²C OLED Display (SSD1306 based)
* 1 x CR2032 3V Battery
* 1 x CR2032 Battery Holder
* 1 x ATtiny85
* 1 x 8 Pin DIP IC Socket
* 4 x Push Button
* 3 x 100k Ohm Resistor
* 2 x 10k Ohm Resistor
* 1 x 0.1uF Ceramic Capacitor
* 1 x Slide Switch
* 0.6 mm Enameled Copper Wire
* Arduino Uno, breadboard and some jumper wires (for programming your ATtiny)
* 2 x 8x11 cm Plywood Piece
* 4 x 1.6 cm Bolts
* Drill, Sanding Paper, Saw
* Soldering tools
Step 1: Programming Your ATtiny85
I was using a chip without a development board, so I used my Arduino Uno as an ISP (In-System-Programmer). To keep this part of the tutorial short, I recommend you follow this article on how to do this. For this project, I was using the internal 8MHz oscillator. Please note that to make the ATtiny run at 8 MHz, you need to burn the bootloader first.
The code consists of two parts, first is the "very minimalistic" library I wrote for the display (oled85.h and oled85.cpp). Both of these files have to be in the same folder as the file snakeGame.ino (which you'll be uploading). 769
This library depends on the TinyWireM library developed by Adafruit, so you need to have it in your libraries (if you don't have it already, add it in Sketch->Include Library->Add Zip Library).
Then there is the snakeGame.ino file which contains the sketch you are going to upload to your ATtiny. Open it in your IDE, compile and upload. It will probably take a while. I tried throwing in some comments to make it at least a bit readable in case you'd like to try playing around with it a bit.
NOTE: by default the display assumes a I2C address of
0x3C, in case your display uses a different one, open the oled85.h file and change the #define I2C_ADDR statement.
You can download everything from my Github.
Step 2: Testing It Out on a Breadboard
You can skip this part, but I think it's good practice to test everything before moving on to the next step. It'll be harder to identify what's not working correctly when it's all soldered together. One issue that I encountered when building this on a breadboard was that the display had trouble rendering some frames correctly (probably due to the wonky connections). Everything worked like a charm when I soldered it together though.
Here's a bit of explanation how the connections work:
THE DISPLAY
Using the I2C protocol already set restrictions on what pins can be used here, the connections are as follows:
* SDA to PB0 (physical pin 5)
* SCL to PB2 (physical pin 7)
* VCC to +3V
* GND to GND
THE BUTTONS
The trickiest part of the circuit - the buttons. Since the ATtiny85 comes with a limited amount of digital pins that could be reliably used for this purpose (and two were already occupied by the display), the solution was to make use of the analog input instead. The buttons are connected in pairs to two of the analog pins - A2 (PB4/physical pin 3)reads input from UP and DOWN buttons and A3 (PB3/physical pin 2) reads input from LEFT and RIGHT. They are separated by resistors that form a voltage divider. Comparing the analog readings on each of the pins to the two thresholds (threshold1 and threshold2 in the sketch - can be calculated from the resistor values in the voltage divider) is used to determine which button is pressed. 991