Using the Arduino Uno R4 and a GY-521 (MPU-6050) to Play Snake
by pdp12 in Circuits > Arduino
33 Views, 0 Favorites, 0 Comments
Using the Arduino Uno R4 and a GY-521 (MPU-6050) to Play Snake
I had an Arduino Uno R4 Wifi lying around not earning its keep so I decided to see what I could do with it. I came across pierfra71's project "Arduino UNO R4 Simple Game" and figured that I could expand on that to play the classic Snake game. My next step was to create a requirements document for the game to upload to Claude to create the sketch.
This is the key step. If you want to use any of the AI programs to write code for you be sure to do the upfront work to write out, in as much detail as you can, the exact behavior you want your application to exhibit. Take it from me, if you start out loosey-goosey with a broad request expecting to fine-tune it as you go, you will enter revision hell. While you may get some appropriate new behavior in your app you're just as likely to see existing behavior that was working now broken.
Supplies
Arduino Uno R4
GY-521
Arduino IDE
4 Dupont wires either female to male or with the female ends clipped off
Optional: 3D Printed puck to hold the GY-521
Downloads
Program the Arduino Uno R4
To program the Arduino Uno R4 you need to have the Arduino IDE running on your computer and upload the ArduinoUnoR4Game.ino file. If your IDE does not already have the R4 boards installed, make sure to install the R4 boards using the IDE board manager.
From the Arduino support site (https://support.arduino.cc/hc/en-us/articles/360016119519-Add-boards-to-Arduino-IDE):
Add a board with the Boards Manager
Official Arduino cores can be conveniently installed with the Boards Manager tool.
- In the menu bar, select Tools > Board > Boards Manager… ,or click on the
button in the sidebar.
- Either search for the package name (e.g. “megaAVR”), or the board (e.g. “Uno”, “MKR1000”, or “Portenta”), by typing in the search field.
- Find the package that includes your board.
- Click Install (the latest version is selected by default).
- Wait for the installation to complete.
- You can now select boards from the installed packages in the Tools > Board menu:
At this point select the version of the R4 that you have. In my case it's the Arduino Uno R4 Wifi.
Now you can plug in your board and select it from the "Tools/Board/Arduino Uno R4 Boards" menu.
Then select the port that your board is connected to. It should auto select the port that's connected. If you don't see a board already selected look for a port that end with your board name.
In my case the port is "dev/cu.usbmodemF0F5BD56EEC42 (Arduino UNO R4 WiFi)"
BTW, it can be quite frustrating if your port doen't show up on the list. I had to change cables and also turn the connector upside down before it registered my port.
At this point you should be able to compile the game sketch (ArduinoUnoR4Game.ino) without any errors. Once you've verified you can compile the sketch upload it to your R4.
Connect the GY-521 (MPU-6050)
This part is pretty simple and may or may not require soldering. If your GY-521 (aka MPU-6050) has header pins you can use female to male Dupont wires to connect the two boards. In my case I cut the Dupont wires to solder them to existing wires that were already on the GY-521 from another project.
Either way, here's the pin connections:
UNO R4 --> GY-521
A5 --> SCL
A4 --> SDA
3.3V --> Vcc
GND --> GND
Controlling the Snake
Once you've connected the two boards and loaded the sketch you're good to go. At the start of the game you get a 5 sec countdown timer followed by the word Go!. The snake starts off with 8 dots and adds a dot every 2 secs. Your challenge is to move the snake without letting it touch itself. If it does, the game ends.
If you have the GY-521 oriented correctly (component side down, Vcc pin in front) you can change the direction of the snake by tilting the GY-521 forward, back, left, or right. You don't need an enclosure for it but if you like you can print out the puck.stl file and install it. That file is from a fun project from Tipam called "Pico Arcade Mini" - https://www.instructables.com/Pico-Arcade-Mini/.
Adjusting Game Parameters
While this is a pretty simple game there are a few parameters you can change, listed under "Game Constants" in the sketch, to make the game easier or harder.
const int MAX_SNAKE_LENGTH = 64;
const int INITIAL_SNAKE_LENGTH = 8;
const int MOVE_DELAY = 300; // milliseconds between moves
const int GROWTH_INTERVAL = 2000; // grow every 2 seconds
Enjoy!