Snake Game on 8x8 Matrix using Arduino - Internet of Things

Snake Game on 8x8 Matrix using Arduino

Arduino Snake Game Project
Snake Game has been very popular since the beginning of the Mobile phones. Initially it was come in Black and white cell phones, and soon became very famous. Then with the advancement of the Cellphones, this game has also changed a lot, and now many graphical and colourful versions of this game are available.
Snake game has also become very popular DIY project for electronics Hobbyist and Students. So today we are going to demonstrate, Arduino Snake Game, with all its basic functionalities, while keeping it simple at the same time.

 Components Used:
·         Arduino UNO
·         8x8 LED Dot Matrix Display
·         Shift Register 74HC595
·         16x2 LCD
·         POT 1K
·         Push Buttons
·         Connecting wires
·         Bread Board
·         Power Supply

Working Explanation:
This is little complicated game to build. But in this tutorial, we have made it simple for you. To make this project, we have used an 8x8 red colour Dot matrix display for displaying the snake and its food dot, a LCD for displaying the points or score, 5 push buttons for giving directions and start the game and finally an Arduino UNO for controlling the whole the process. Pin diagram of 8x8 LED Dot Matrix Display with its original image has been given below:

  

When we power up the circuit, first we show a welcome message and then a Press Start To Playhint on the LCD. After this, LCD shows the score as zero and dot matrix display shows two dots as snake and a single dot as food.
Now user need to press the middle button to start the game and snake start moving in upward direction by default. Then user needs to give direction to snake by pressing the Direction keysaround the middle button. Here we have used five keys (push buttons) namely Left key, Right key, Up key, Down key and Start key. Whenever the snake reaches to the food dot or eats the food, score increases by 5 points each time and the Snake length is increased by one dot (LED) each time, also snake speed become faster than before. And whenever snake would strike at any wall or reach at the end of LED matrix, then it would end the game (“Game Over”). Then user needs to start game again by pressing start key.

Circuit Explanation:
 Circuit of this Snake Game Project is little complex. Here we have connected dot matrix display by using Shift Register 74HC595. Here two shift registers are used, one for driving the columns and second for driving the rows. Control pins of both the registers, Column shift register and row shift register (SH, ST), are directly connected to Arduinos pin number 14 and 16 respectively. And DS pin of column shift register and row shift register are directly connected to pin number 15 and 17 of Arduino. Start button for start the game is connected at pin number 3, left direction button at pin 4, right direction button at pin 6, up direction button at pin 2 and down direction button at pin 5. A LCD is also connected in our hardware to show score. RS and EN pins are directly connected at pin 13 and 12. RW pin is directly ground. And data pins d4-d7 are connected at pin 11, 10, 9, 8 of Arduino. Rest of connection are shown in the circuit diagram.


Programming Explanation:
To write this Arduino snake game code, first of all we include header files and define pins for LCD. And then define some pins for direction buttons and data pin for shift registers.
#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

#define ds_col 15
#define sh_col 16
#define st_col 14
#define ds_row 17

#define start 3
#define up 2
#define down 5
#define left 4
#define right 6
Then we initialize all the things that we have used in the program. In the setup function we initialize LCD, giving direction to input output pins, pull-up the bits and showing welcome message on LCD.
void setup()
{  
    lcd.begin(16,2);
    pinMode(ds_col, OUTPUT);
    pinMode(sh_col, OUTPUT);
    pinMode(st_col, OUTPUT);
    pinMode(ds_row, OUTPUT);
    pinMode(start, INPUT);
    ... ....
    .... ....
And then we start game in loop function.
void show_snake(int temp)
{
 for(int n=0;n<temp;n++)
 {
   int r,c;
  for(int k=0;k<21;k++)
  {
  ... ....
  .... ....
Here we have used the below function for reading input direction from the push button.
void read_button()
{
 if(!digitalRead(left))
 {
   move_r=0;
   move_c!=-1 ? move_c=-1 : move_c=1;
   while(!digitalRead(left));
   ... ....
   .... ....

       Demo & Code


Snake Game on 8x8 Matrix using Arduino Snake Game on 8x8 Matrix using Arduino Reviewed by XXX on สิงหาคม 27, 2560 Rating: 5

ไม่มีความคิดเห็น