This project involves creating a snake game using an Arduino Uno, joystick, and 8×8 LED matrix display. The player controls the snake’s movement with the joystick while the LED matrix shows the game board. The project requires basic knowledge of Arduino programming and electronics.
components required
To create a snake game on a 8×8 LED matrix using Arduino Uno and a joystick, you will need the following components:
You will also need a computer with the Arduino IDE installed to program the microcontroller board.
8×8 dot matrix display module pinout
Pinout of matrix display module.
- Pin 1: VCC (power supply voltage)
- Pin 2: GND (ground)
- Pin 3: DIN (data input)
- Pin 4: CS (chip select)
- Pin 5: CLK (clock)
xy joystick module pinout
The pinout for a Dual Axis XY Joystick Module typically includes five pins, arranged in a row or a column. Here is a common pinout:
Pin 1: GND (ground)
Pin 2: +5V (power supply voltage)
Pin 3: VRx (analog output for X-axis)
Pin 4: VRy (analog output for Y-axis)
Pin 5: SW (digital output for joystick button)
Note that the exact numbering and labeling of the pins may vary depending on the specific module. Some modules may have additional pins for features such as LED indicators or additional buttons.
circuit diagram
Here we are using Arduino Uno, joystick, buzzer and jumper wire .write now we connect 5 pin of joy stick with Arduino Uno 5v with Arduino Uno 5 volt and gnd with GND and VRX with A1 and VRY with A0. Now we connect 8×8 matrix led with Din with pin12, CS with 11, CLk with pin 10. Here we connect pin 8 with buzzer.
ARDUINO CODE
#include “LedControl.h” // Pins const int SW_pin = 2; const int X_pin = 0; const int Y_pin = 1; const int DIN = 12; const int CS = 11; const int CLK = 10; const int BUZZER = 8; // Variables const int screenWidth = 8; const int screenHeight = 8; int snakeX, snakeY, foodX, foodY, score = 0, snakeSize = 1; char direction; int tailX[100], tailY[100]; bool isGameOver = false; LedControl lc = LedControl(DIN, CS, CLK, 1); void setup() { setupPins(); setupLedBoard(); setupSnakePosition(); setupFoodPosition(); } void setupSnakePosition() { snakeX = 4; snakeY = 4; } void setupFoodPosition() { foodX = rand() % screenWidth; foodY = rand() % screenHeight; } void setupLedBoard() { lc.shutdown(0, false); lc.setIntensity(0, 1); lc.clearDisplay(0); } void setupPins() { pinMode(SW_pin, INPUT); digitalWrite(SW_pin, HIGH); } void loop() { if (isGameOver) { playGameOverSong(); showGameOverScreen(); } else { startGame(); } } void playGameOverSong() { tone(BUZZER, 1000, 1000); delay(100); tone(BUZZER, 2000, 1000); delay(100); tone(BUZZER, 3000, 1000); delay(100); tone(BUZZER, 4000, 1000); delay(100); tone(BUZZER, 5000, 2000); } void playFoodEatenSong() { tone(BUZZER, 500, 100); } void startGame() { manageGameOver(); setJoystickDirection(); changeSnakeDirection(); manageSnakeOutOfBounds(); manageEatenFood(); manageSnakeTailCoordinates(); drawSnake(); delay(300); } void manageGameOver() { for (int i = 1; i < snakeSize; i++) { if (tailX[i] == snakeX && tailY[i] == snakeY) { isGameOver = true; } } } void manageSnakeOutOfBounds() { if (snakeX >= screenWidth) { snakeX = 0; } else if (snakeX < 0) { snakeX = screenWidth – 1; } if (snakeY >= screenHeight) { snakeY = 0; } else if (snakeY < 0) { snakeY = screenHeight – 1; } } void manageSnakeTailCoordinates() { int previousX, previousY, prevX, prevY; previousX = tailX[0]; previousY = tailY[0]; tailX[0] = snakeX; tailY[0] = snakeY; for (int i = 1; i < snakeSize; i++) { prevX = tailX[i]; prevY = tailY[i]; tailX[i] = previousX; tailY[i] = previousY; previousX = prevX; previousY = prevY; } } void manageEatenFood() { if (snakeX == foodX && snakeY == foodY) { playFoodEatenSong(); score++; snakeSize++; setupFoodPosition(); } } void setJoystickDirection() { if (analogRead(X_pin) > 1000) { direction = ‘u’; } else if (analogRead(X_pin) < 100) { direction = ‘d’; } else if (analogRead(Y_pin) > 1000) { direction = ‘l’; } else if (analogRead(Y_pin) < 100) { direction = ‘r’; } } void changeSnakeDirection() { switch (direction) { case ‘l’: snakeX–; break; case ‘r’: snakeX++; break; case ‘u’: snakeY–; break; case ‘d’: snakeY++; break; } } void showGameOverScreen() { for (int i = 0; i < screenHeight; i++) { for (int j = 0; j < screenWidth; j++) { showLed(j, i); delay(50); } } resetVariables(); } void resetVariables() { setupSnakePosition(); setupFoodPosition(); direction = ‘ ‘; isGameOver = false; score = 0; snakeSize = 1; } void showLed(int row, int column) { lc.setLed(0, row, column, true); } void hideLed(int row, int column) { lc.setLed(0, row, column, false); } void drawSnake() { for (int i = 0; i < screenHeight; i++) { for (int j = 0; j < screenWidth; j++) { if (i == snakeY && j == snakeX) { showLed(snakeX, snakeY); } else if (i == foodY && j == foodX) { showLed(foodX, foodY); } else { bool isShown = false; for (int k = 0; k < snakeSize; k++) { if (tailX[k] == j && tailY[k] == i) { showLed(j, i); isShown = true; } } if (!isShown) { hideLed(j, i); } } } } } |
errors during interfacing 8×8 matrix led display –here what you should do ?
Here I face issues when I interface with 8×8 matrix led display with Arduino losing wire .so you just keep fit tight so it will not move.