How to make a snake game on arduino

in #dblog5 years ago

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. 

<p dir="auto"> <img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8N1XsXmbXq61eRHKCQJYyVxJgos2BnyW8kdJbW6?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8N1XsXmbXq61eRHKCQJYyVxJgos2BnyW8kdJbW6?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8N1XsXmbXq61eRHKCQJYyVxJgos2BnyW8kdJbW6?format=match&mode=fit&width=640 2x" /> <h4>Required Components <p dir="auto">We need this couples of requirements in this tutorial <ul> <li>Nokia 5110 LCD display <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgGUCnZhy6ePW6ZbwzSgHc4tjwyspbVm4YnBgG6?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgGUCnZhy6ePW6ZbwzSgHc4tjwyspbVm4YnBgG6?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgGUCnZhy6ePW6ZbwzSgHc4tjwyspbVm4YnBgG6?format=match&mode=fit&width=640 2x" /> <p dir="auto">These displays were used in old Nokia 5110/3310 cell phones. It is an 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. <ul> <li>Buttons <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgGc3SrF3hesubLWEzRNAbXnJXG7UGZU9eyTSMk?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgGc3SrF3hesubLWEzRNAbXnJXG7UGZU9eyTSMk?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgGc3SrF3hesubLWEzRNAbXnJXG7UGZU9eyTSMk?format=match&mode=fit&width=640 2x" /> <p dir="auto">I use the Momentary Push Button Switch a 12mm Square, These buttons give a nice soft click every time it's pressed so you know that you've definitely activated it. Their almost instant return makes them great for building your own homemade button pad. it also makes a good reset switch or user feedback for a microcontroller circuit. <ul> <li>Buzzer <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5fiyP84a1kVxCa4rBzyNdaW529RtMZ6udtXnC1L?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5fiyP84a1kVxCa4rBzyNdaW529RtMZ6udtXnC1L?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5fiyP84a1kVxCa4rBzyNdaW529RtMZ6udtXnC1L?format=match&mode=fit&width=640 2x" /> <p dir="auto">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 user to make alarm tune or alarm system that we will use in this tutorial. <ul> <li>Arduino Uno R3 board <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6Fn8w3Lq7nshgHBxKzQiP4Vmo8UBgBLr5uyetuHjt?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6Fn8w3Lq7nshgHBxKzQiP4Vmo8UBgBLr5uyetuHjt?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6Fn8w3Lq7nshgHBxKzQiP4Vmo8UBgBLr5uyetuHjt?format=match&mode=fit&width=640 2x" /> <p dir="auto"> In this tutorial I am using the clone <a href="https://store.arduino.cc/usa/arduino-uno-rev3" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">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. <ul> <li>Jumper wires <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgrfcoBqCVqHZRJQYjJo42twojV5f58hBH5LybU?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgrfcoBqCVqHZRJQYjJo42twojV5f58hBH5LybU?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgrfcoBqCVqHZRJQYjJo42twojV5f58hBH5LybU?format=match&mode=fit&width=640 2x" /> <p dir="auto">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. <ul> <li>Breadboard <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgsD1DThPvGzGM1QtaSR2b2vHkMT7Y5YYCB7UXY?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgsD1DThPvGzGM1QtaSR2b2vHkMT7Y5YYCB7UXY?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpgsD1DThPvGzGM1QtaSR2b2vHkMT7Y5YYCB7UXY?format=match&mode=fit&width=640 2x" /> <p dir="auto">Use a Single Panel Breadboard it is a solderless device for a 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. <h4>Difficulty <ul> <li>Intermediate <h4>Tutorial Contents <ul> <li>SOFTWARE <p dir="auto">we are going to use the Arduino ide, to set the sketch for this, if you don't have to make sure to download the Arduino IDE for your specific operating system. I’ll leave a link to where you can download this software: <a href="https://www.arduino.cc/en/Main/Software" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://www.arduino.cc/en/Main/Software <ul> <li>Libraries <p dir="auto">To run the program and to make sketching a code easily we need to download these couples of libraries. Adafruit-GFX-Library; <a href="https://github.com/adafruit/Adafruit-GFX-Library" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://github.com/adafruit/Adafruit-GFX-LibraryAdafruit-PCD8544-Nokia-5110-LCD-library; <a href="https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library/ <ul> <li>Nokia 5110 CIRCUIT DIAGRAM <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpjqvyFQf2V4Q79EHqnPf1v3TqkH52FFkFQX52VC?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpjqvyFQf2V4Q79EHqnPf1v3TqkH52FFkFQX52VC?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FpjqvyFQf2V4Q79EHqnPf1v3TqkH52FFkFQX52VC?format=match&mode=fit&width=640 2x" /> <p dir="auto"> 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 be connected to GND on the Arduino board, The display driver is a <a href="https://www.sparkfun.com/datasheets/LCD/Monochrome/Nokia5110.pdf" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">PCD8544 chip which is on the library we've 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 a resistor between the light pin and the GND pin on the Arduino. the remaining pin on the LCD will be connected to the specified digital pinout on the Arduino board listed below. <ul> <li>pin 7 - Serial clock out (SCLK) <li>pin 6 - Serial data out (DIN) <li>pin 5 - Data/Command select (D/C) <li>pin 4 - LCD chip select (CS) <li>pin 3 - LCD reset (RST) <p dir="auto"><strong>Push Buttons CIRCUIT DIAGRAM  <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FsLAuKco395oyWUUig7natX5g8i2Tow27HNauEUe?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FsLAuKco395oyWUUig7natX5g8i2Tow27HNauEUe?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6FsLAuKco395oyWUUig7natX5g8i2Tow27HNauEUe?format=match&mode=fit&width=640 2x" /> <p dir="auto"> The pushbuttons has 4 sets of legs but we will use 2 legs, both the right leg is connected to the GND pin on the Arduino, the left one is connected to the specified Arduino pin depends on the use of it. we will use 5 buttons the 4 is the navigation buttons and the 1 is for the pause button. <ul> <li>button 1 LEFT - 8 pin on Arduino <li>button 2 DOWN - pin 10 <li>button 3 RIGHT - 11 <li>button 4 UP - 12 <li>button 5 PAUSE - 9 <p dir="auto"><strong>Buzzer Diagram  <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5gK2k3HsNE2wgnaByfgQ5ud1p8tsfczUad5mmSn?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5gK2k3HsNE2wgnaByfgQ5ud1p8tsfczUad5mmSn?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5gK2k3HsNE2wgnaByfgQ5ud1p8tsfczUad5mmSn?format=match&mode=fit&width=640 2x" /> <p dir="auto"> The buzzer has 2 legs 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 every time the snake eat the egg it's beeping. <ul> <li>Combine all the components in one breadboard <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8MRt69Vvhkpjc3dUQkgMUeUNeDGXuXX6yWLmqE6?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8MRt69Vvhkpjc3dUQkgMUeUNeDGXuXX6yWLmqE6?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8MRt69Vvhkpjc3dUQkgMUeUNeDGXuXX6yWLmqE6?format=match&mode=fit&width=640 2x" /> <p dir="auto"> 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 you're using the same board as mine.  <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5gv31Mmr3xzZ8WmAhkTLk9XWb86iZ7qAUXG48v2?format=match&mode=fit&width=640" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5gv31Mmr3xzZ8WmAhkTLk9XWb86iZ7qAUXG48v2?format=match&mode=fit&width=640 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T5gv31Mmr3xzZ8WmAhkTLk9XWb86iZ7qAUXG48v2?format=match&mode=fit&width=640 2x" /> <p dir="auto">  <ul> <li>Programming a code <p dir="auto">Include the libraries we've recently downloaded, the EEPROM.h is already on the Arduino library. <pre><code>#include <EEPROM.h><br /> <br /> #include <Adafruit_GFX.h><br /> <br /> #include <Adafruit_PCD8544.h> <p dir="auto">Define the pin output of the Nokia LCD <pre><code>Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3); <br /> <br /> // pin 7 - Serial clock out (SCLK)<br /> <br /> // pin 6 - Serial data out (DIN)<br /> <br /> // pin 5 - Data/Command select (D/C)<br /> <br /> // pin 4 - LCD chip select (CS)<br /> <br /> // pin 3 - LCD reset (RST)  <p dir="auto">Define the pushbuttons pin output <pre><code>#define LEFT 8<br /> <br /> #define DOWN 10<br /> <br /> #define RIGHT 11<br /> <br /> #define UP 12<br /> <br /> #define PAUSE 9<br /> <br /> #define MAX_WIDTH 84<br /> <br /> #define MAX_HEIGHT 48<br /> <br /> #define speakerPin 2 <p dir="auto">Direction of the snake that is currently moving <pre><code>boolean dl=false,dr=false,du=false,dd=false;<br /> <br /> int x[200],y[200],i,slength,tempx=10,tempy=10,xx,yy;<br /> <br /> unsigned int high;<br /> <br /> uint8_t bh,bl;<br /> <br /> int xegg,yegg;<br /> <br /> int freq,tb;<br /> <br /> int l,r,u,d,p;<br /> <br /> unsigned long time=280,beeptime=50;<br /> <br /> int score=0,flag=0; <p dir="auto">setup() method is run once at the just after the Arduino is powered up and the loop() method is ran continuously afterward.<br /> setup() is where you want to do any initialization steps, and in loop() you want to run the code you want to run over and over again.  <p dir="auto">Download code <a href="https://www.sendspace.com/file/q3bdyl" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">HERE <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8Lq1YPJtwn6aKU15MX9tVJkEhLd3NCzwb54tQ5T?format=match&mode=fit" srcset="https://images.hive.blog/768x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8Lq1YPJtwn6aKU15MX9tVJkEhLd3NCzwb54tQ5T?format=match&mode=fit 1x, https://images.hive.blog/1536x0/https://steemitimages.com/p/3W72119s5BjWMGm4Xa2MvD5AT2bJsSA8F9WeC71v1s1fKfGkK9mMKuc3LcvF4KigbWg9UsrpEPGGL6T8Lq1YPJtwn6aKU15MX9tVJkEhLd3NCzwb54tQ5T?format=match&mode=fit 2x" /> <p dir="auto"> I hope you enjoy this activity if you want to learn how Arduino works, and how to make a sketch, then maybe this site <a href="http://educ8s.tv/" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">http://educ8s.tv/ might help you, and Abhinav Fauzdar since I'm always following their project for this stuff, all images on this blog are supposedly mine that I posted a year ago you can find the original post from this <a href="https://steemit.com/utopian-io/@pakganern/snake-game-on-arduino-using-nokia-5110-lcd-and-push-buttons" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">link. thank you for stopping by.