New to Busy?

Snake Game on Arduino using Nokia 5110 LCD and push buttons

4 comments

pakganern
66
2 years agoUtopian8 min read

What Will I Learn?

hey Guys today in this tutorial we will learn how to make a simple game the popular snake game on the nokia 5110 lcd and push buttons to play the game, how to program arduino on desktop IDE and how to connect the components with a circuit diagram.

20180215_134529.jpg

Requirements

We need this couples of requirements in this tutorial

  • Nokia 5110 LCD display

20180215_125201.jpg

These displays were used in old Nokia 5110/3310 cell phones. It is a 84x48 pixel monochrome LCD display. These displays are small, but very readable and come with backlight. This display is made of 84x48 individual pixels, so you can use it for graphics, text or bitmaps. so less expensive than using oled display. I bought mine for just $5 when i was in college.

  • Buttons

20180215_125306.jpg

we will use the Momentary Push Button Switch a 12mm Square, These buttons gives a nice soft click every time it's pressed so you know that you've definitely activated it. Their almost instant return make them great for building your own homemade button pad. it also make a good reset switch or user feedback for a microcontroller circuit.

  • Buzzer

U5dthsbr6hptCvg63665GRJxsF74eQo_1680x8400.jpg

this electric buzzer can be driven by an oscillating electronic circuit or other audio signal source. A click, beep or ring can indicate that a button has been pressed most of this using to make alarm tune or alarm system that we will use in this tutorial.

  • Arduino Uno R3 board

20180215_102432.jpg

In this tutorial i am using the clone arduino r3 board, It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button. It contains everything needed to support the microcontroller components.

  • Jumper wires

20180214_140431.jpg

we will use jumpers to connect on a breadboard or female header connector, size and color to distinguish the different working signals. we will use the male to male jumper for 7 segment and the buttons to connect to arduino.

  • Breadboard

20180214_140331.jpg

we will use a Single Panel Breadboard it is a solderless device for temporary prototype with electronics and test circuit designs. both side of the breadboard is the power rail it is horizontal lining and the middle is the vertical rail for arduino components.

Difficulty

  • Intermediate

Tutorial Contents

  • SOFTWARE

we are going to use the arduino ide, to set the sketch for this, if you dont have make sure to download the Arduino IDE for your specific operating system. I’ll leave a link to where you can download this software: https://www.arduino.cc/en/Main/Software

  • Libraries

To run the program and to make sketching a code easily we need to download these couples of libraries.

Adafruit-GFX-Library; https://github.com/adafruit/Adafruit-GFX-Library

Adafruit-PCD8544-Nokia-5110-LCD-library; https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library/

  • Nokia 5110 CIRCUIT DIAGRAM

nokia.png

To drive the nokia display, we need 5 digital output pins. the LIGHT pin can be used to control (via on/off or PWM) the backlight, the GND and the LIGht pin should connected to GND on the arduino board, The display driver is a PCD8544 chip which is on the library weve recently downloaded, and it runs at 3.3V so you will need a 3V supply handy. Logic levels must be 3.3V to prevent damage so you must use some kind of resistance. or we can use resistor between the light pin and the GND pin on the arduino. the remaining pin on the LCD will be conneted to the specified digital pin out on the arduino board llisted below.

  • pin 7 - Serial clock out (SCLK)
  • pin 6 - Serial data out (DIN)
  • pin 5 - Data/Command select (D/C)
  • pin 4 - LCD chip select (CS)
  • pin 3 - LCD reset (RST)

Push Buttons CIRCUIT DIAGRAM

button.png

The push buttons has 4 set of legs but we will using 2 legs, both the right leg is connected to the GND pin on the arduino, the left one is connected to specified arduino pin depends on the use of it. we will use 5 buttons the 4 is then navigation buttons and the 1 is for the pause button.

  • button 1 LEFT - 8 pin on arduino
  • button 2 DOWN - pin 10
  • button 3 RIGHT - 11
  • button 4 UP - 12
  • button 5 PAUSE - 9

Buzzer Diagram

buzz.png

The buzzer has 2 leg the GND supposed to be the short leg of the buzzer and the long leg is connected to the Digital pin on the arduino. we will use these button as a ringtone everytime the snake eat the egg its beeping.

  • Combine all the components in one breadboard

20180215_133347.jpg

  • Connect the arduino uno board to your computer using the Type B usb cable included in the package. open the arduino Desktop IDE locate the TOOLS verify the type of board you are using PORT should be on the COM# and the board should be on the arduino/genuino uno if youre using the same board as mine.

com.png

  • Programming a code

Include the libraries the weve recenlty downloaded, the EEPROM.h is already on the arduino library.

#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

Define the pin output of the nokia LCD

Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); 
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST) 

Define the push buttons pin output

#define LEFT 8
#define DOWN 10
#define RIGHT 11
#define UP 12
#define PAUSE 9
#define MAX_WIDTH 84
#define MAX_HEIGHT 48
#define speakerPin 2

Direction of the snake that is currenlt moving

boolean dl=false,dr=false,du=false,dd=false;
int x[200],y[200],i,slength,tempx=10,tempy=10,xx,yy;
unsigned int high;
uint8_t bh,bl;
int xegg,yegg;
int freq,tb;
int l,r,u,d,p;
unsigned long time=280,beeptime=50;
int score=0,flag=0;

setup() method is ran once at the just after the Arduino is powered up and the loop() method is ran continuously afterwards.
setup() is where you want to do any initialisation steps, and in loop() you want to run the code you want to run over and over again.

#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); 
#define LEFT 8
#define DOWN 10
#define RIGHT 11
#define UP 12
#define PAUSE 9
#define MAX_WIDTH 84   
#define MAX_HEIGHT 48
#define speakerPin 2
boolean dl=false,dr=false,du=false,dd=false; 
int x[200],y[200],i,slength,tempx=10,tempy=10,xx,yy;
unsigned int high;
uint8_t bh,bl;
int xegg,yegg;
int freq,tb;
int l,r,u,d,p;
unsigned long time=280,beeptime=50;
int score=0,flag=0; 
void setup()
{
  Serial.begin(9600);      
  display.begin();
  display.clearDisplay();
  pinMode(LEFT,INPUT);    
  pinMode(RIGHT,INPUT);
  pinMode(UP,INPUT);
  pinMode(DOWN,INPUT);
  pinMode(PAUSE,INPUT);  
  pinMode(speakerPin,OUTPUT);  
  digitalWrite(LEFT,HIGH);  
  digitalWrite(RIGHT,HIGH);
  digitalWrite(UP,HIGH);
  digitalWrite(DOWN,HIGH);
  digitalWrite(PAUSE,HIGH);
  display.setContrast(25);
  slength=8;             
  xegg=(display.width())/2;  
  yegg=(display.height())/2;  
  display.setTextSize(2);      
  display.setTextColor(BLACK);
  display.setCursor(10,20);
  display.print("SNAKE");
  display.setCursor(10,50);
  display.print("utopian");
  display.display();
  delay(4000);
  display.clearDisplay();  
  for(i=0;i<=slength;i++)  
  {
    x[i]=25-3*i;
    y[i]=10;   
  }  
  for(i=0;i<slength;i++)   
     {
     display.drawCircle(x[i],y[i],1,BLACK);
     }
     display.display();
dr=true;
}
void loop()   
{        
   movesnake();      
} 
void movesnake()
{
  l=digitalRead(LEFT);   
  d=digitalRead(DOWN);
  r=digitalRead(RIGHT);
  u=digitalRead(UP);
  p=digitalRead(PAUSE);  
if(flag==0)
{
direct();            
}
  if(millis()%time==0)   
{             
     if(flag==0)                                
     {
       if(dr==true){tempx=x[0]+3;tempy=y[0];}   
       if(dl==true){tempx=x[0]-3;tempy=y[0];}    
       if(du==true){tempy=y[0]-3;tempx=x[0];}
       if(dd==true){tempy=y[0]+3;tempx=x[0];}
     }   
   flag=0;  
     checkgame();                       
      checkegg();   
 if(tempx<=0){tempx=84+tempx;}    
 if(tempx>=84){tempx=tempx-84;}
 if(tempy<=0){tempy=48+tempy;}
 if(tempy>=48){tempy=tempy-48;}
 for(i=0;i<=slength;i++)      
  {
   xx=x[i];
   yy=y[i]; 
   x[i]=tempx;
   y[i]=tempy;
   tempx=xx;
   tempy=yy;
  }  
drawsnake();        
}
}
void checkgame()      
{
  for(i=1;i<slength;i++)            
  {
    if(x[i]==x[0] && y[i]==y[0])
    {      
      bh=EEPROM.read(1);
      bl=EEPROM.read(0);
      high=(((0xff00+bh)<<8) + bl);      
      if(score>high)
      {
        high=score;
        bh=(high >> 8);
        bl=high & 0xff;       
        EEPROM.write(1,bh);
        EEPROM.write(0,bl);
      }
      display.clearDisplay();
      display.setTextColor(BLACK);       
      display.setTextSize(1);
      display.setCursor(20,12);
      display.print("Game Over");
      display.setCursor(15,30);
      display.print("Score: ");
      display.print(score);
      display.setCursor(15,40);
      display.print("High: ");
      display.print(high);     
      display.display();
      beep(20,5000);            
      display.clearDisplay();      
      slength=8;        
      score=0;
      time=280;      
      redraw();          
    }
  }
}
void checkegg()   
{
  if(x[0]==xegg or x[0]==(xegg+1) or x[0]==(xegg+2) or x[0]==(xegg-1))  
  {
    if(y[0]==yegg or y[0]==(yegg+1) or y[0]==(yegg+2) or y[0]==(yegg-1))
    {
      score+=1;                
      slength+=1;
      if(time>=90)
      {time-=20;}      
      display.fillRect(xegg,yegg,3,3,WHITE);          
      display.display();            
      beep(35,beeptime);          
      xegg=random(1,80);            
      yegg=random(1,40);
    }
  }
}        
void direct()             
{
  if(l==LOW and dr==false)      
  {
    dl=true;du=false;dd=false;
    tempx=x[0]-3;          
    tempy=y[0];
    flag=1;                    
  }  
  else if(r==LOW and dl==false)
  {
    dr=true;du=false;dd=false;
    tempx=x[0]+3;
    tempy=y[0];
    flag=1;
  }  
  else if(u==LOW and dd==false)
  {
    du=true;dl=false;dr=false;
    tempy=y[0]-3;
    tempx=x[0];
    flag=1;
  }  
  else if(d==LOW and du==false)
  {
    dd=true;dl=false;dr=false;
    tempy=y[0]+3;
    tempx=x[0];
    flag=1;
  } 
  else if(p==LOW)
  {
  display.clearDisplay();  
  display.setTextColor(BLACK);
  for(i=5;i>0;i--)
    {
     display.setCursor(25,10); 
     display.setTextSize(1);
     display.print("PAUSED");
     display.setCursor(40,30);
     display.print(i);
     display.display();
     delay(1000);
     display.clearDisplay();
    } 
    redraw();    
  } 
}
void drawsnake()  
{
  display.fillRect(xegg,yegg,3,3,BLACK);  
  display.drawCircle(x[0],y[0],1,BLACK);  
  display.drawCircle(x[slength],y[slength],1,WHITE);
  display.display(); 
}
void redraw()
{
  display.fillRect(xegg,yegg,3,3,BLACK);
  for(i=0;i<slength;i++)
     {
     display.drawCircle(x[i],y[i],1,BLACK);
     }
     display.display();
}               
void beep (int freq,long tb)  
{ 
    int x;   
    long delayAmount = (long)(500/freq);   
    long loopTime = (long)(tb/(delayAmount*2)); 
    for (x=0;x<loopTime;x++)     // One iteration produces sound for one cycle
    {    
        digitalWrite(speakerPin,HIGH);
        delay(delayAmount);
        digitalWrite(speakerPin,LOW); 
        delay(delayAmount);
    }        
    delay(2);   
}

20180215_134720.gif

I hope you enjoy this actitvity if want to learn how arduino works, and how to make a sketch, then maybe this site http://educ8s.tv/ might help you, and Abhinav Fauzdar since im always following their project for this stuff, all images on this blog are supposedly mine. thank you for stopping by..

please check my previous tutorials;

LED Matrix red 8x8 Tutorial

Ultrasonic Sensor HC SR04 distance meter with a Nokia 5110 LCD display

IIC LCD Scrolling Text with Arduino tutorial

Light sensor LDR Monitor with a LCD Nokia 5110 Tutorial

Rotary Encoder with Nokia 5110 display Tutorial

Real time clock DS1302 module and Nokia 5110 DIY lcd tutorial

How to connect nokia 5110 LCD to arduino



Posted on Utopian.io - Rewarding Open Source Contributors

jeric ederon

Comments

Sort byBest