กลับไปหน้ารวมไฟล์
arduino-uno-snake-game-22-tft-display-e6e0d1.md

Codedocumentationandfiles(download)canbefoundonGitHub

If you have any questions just write a comment in English or German.

Explanations

-!- Explanations in the code are in German -!-

It is possible to use another display with the same resolution. It may be necessary to adjust the pins and the method of accessing the display.

For the movement of the snake and the start of the game a 5 way joystick is used.

The display is the 2.2 inch Tft display from Adafruit. The connection to the Arduino is established via SPI.


🛠️ เจาะลึกเบื้องหลังการทำงาน (Deep Dive / Technical Analysis)

A 16x2 LCD is just sending bytes of letters. A massive 2.2" TFT Color Screen requires the Arduino to physically draw every single microscopic pixel independently. The Snake Game on 2.2" TFT Display requires programmers to execute the most demanding SPI logic ever created for the Uno, managing thousands of X/Y spatial Cartesian loops, redrawing rapidly growing green byte-arrays before the player's joystick action expires!

The Adafruit_ILI9341 Engine

There are $320 \times 240$ pixels on this screen, totally $76,800$ individual dots!

  1. The Uno cannot store the image in RAM because it only holds $2,000$ dots total!
  2. You cannot use display.display() like an OLED. You must execute direct, absolute geometry painting!
  3. Erasing the Tail: As the massive Snake array moves, plotting a new green head is easy tft.fillRect(headX, headY, 10, 10, ILI9341_GREEN);.
  4. But the tail never disappears! You must explicitly tell the SPI bus to draw a black square exactly over the last index of the snake body array!
tft.fillRect(tailX[last], tailY[last], 10, 10, ILI9341_BLACK); // Erase the ghost image physics!

High-Speed Joypad Interrupts

Because pushing 76,000 pixels over 5 SPI wires takes nearly 150 milliseconds for a full screen clear, standard digitalRead() inputs become extremely sloppy and unresponsive.

  • The 2-Axis Joystick uses analog geometry analogRead(A0).
  • The loop() utilizes custom timing constraints:
if (analogRead(JoyY) > 800 && direction != DOWN) {
   direction = UP; // Execute immediately to prevent sluggish steering!
}
  • A massive Apple (ILI9341_RED) randomly mathematically jumps onto the screen. It validates against snakeX[] \ snakeY[] arrays, ensuring it does not magically spawn physically inside the snake's body!

SPI Color Console Parts

  • Arduino Uno/Mega (Mega is wildly preferred. A Mega runs Snake flawlessly, allowing massive array expansion. An Uno will run out of RAM around length 80, instantly randomly crashing the entire game in a memory overflow!).
  • 2.2" or 2.8" SPI TFT Display (ILI9341 Controller Chip).
  • Dual Axis Potentiometer Joystick or a standard 4-Button D-Pad configuration.
  • (Note: Ensure logic-level shifting! Most TFT screens require exactly 3.3V on their SPI pins, not 5V!)

ข้อมูล Frontmatter ดั้งเดิม

title: "Arduino Uno - Snake Game - 2.2\" TFT Display"
description: "Snake Game with Arduino Uno. The snake is controlled with a joystick. A 2.2\" inch TFT display from Adafruit is used as the display."
author: "black-shadow"
category: ""
tags:
  - "game"
  - "arduino uno"
  - "display"
  - "snake"
  - "joystick"
views: 6476
likes: 1
price: 2450
difficulty: "Intermediate"
components:
  - "1x Joystick"
  - "1x Breadboard (generic)"
  - "1x Soldering iron (generic)"
  - "1x Arduino UNO"
  - "1x 2.2\" TFT Display"
  - "1x Jumper wires (generic)"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles:
  - "https://github.com/Black-Shadow29/Arduino-Snake-Game-with-2.2-TFT-Screen.git"
  - "https://projects.arduinocontent.cc/1c3a7d6e-4b2a-4d47-a797-982003c822c9.h"
  - "https://github.com/Black-Shadow29/Arduino-Snake-Game-with-2.2-TFT-Screen.git"
  - "https://projects.arduinocontent.cc/1c3a7d6e-4b2a-4d47-a797-982003c822c9.h"
  - "https://projects.arduinocontent.cc/8768f7c9-0cd9-4fd9-9537-86da6f542e04.cpp"
documentationLinks: []
passwordHash: "894c1ef73ade204d4daea4a46349267231f6e578395f9a1a202efa406e19f932"
encryptedPayload: "U2FsdGVkX1/CxpWL54VALeCtbsEtq+nFVlXPOPt3pxLxarqDQ7FBIlk3/DSwiBcG2oKwatYaE1YpruFUw0nHpsC0OKvWU42ZV5+2TDzq+Dc="
seoDescription: "Build a Snake Game with Arduino Uno, Joystick control, and a 2.2\\\\\\\" TFT display from Adafruit. A classic DIY project for electronics enthusiasts."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/arduino-uno-snake-game-22-tft-display-e6e0d1_cover.jpg"
lang: "en"