{"id":24473,"date":"2016-07-27T14:34:58","date_gmt":"2016-07-27T14:34:58","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=24473"},"modified":"2019-04-02T10:23:32","modified_gmt":"2019-04-02T10:23:32","slug":"guide-for-8x8-dot-matrix-max7219-with-arduino-pong-game","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/guide-for-8x8-dot-matrix-max7219-with-arduino-pong-game\/","title":{"rendered":"Guide for 8&#215;8 Dot Matrix MAX7219 with Arduino + Pong Game"},"content":{"rendered":"<p>The dot matrix that we\u2019re going to use in this\u00a0guide is a 8&#215;8 matrix which means that it has 8 columns and 8 rows, so it contains a total of 64 LEDs.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter  wp-image-24518\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=700%2C397&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"397\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?w=850&amp;quality=100&amp;strip=all&amp;ssl=1 850w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=150%2C85&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=300%2C170&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=768%2C436&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=80%2C45&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=220%2C125&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=176%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 176w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=265%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 265w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=420%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 420w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?resize=732%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 732w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The MAX7219 chip makes it easier to control the dot matrix, by just using 3 digital pins\u00a0of the Arduino board.<\/p>\n<p>I think the best option is\u00a0to buy the dot matrix with the MAX7219 chip as a module, it will simplify the wiring. You can check the <a href=\"https:\/\/makeradvisor.com\/tools\/8x8-dot-matrix-max7219\/\" target=\"_blank\" rel=\"noopener noreferrer\">dot matrix at Maker Advisor<\/a> and find the best price.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-24496\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=900%2C367&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"dotmatrixphoto\" width=\"900\" height=\"367\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?w=900&amp;quality=100&amp;strip=all&amp;ssl=1 900w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=150%2C61&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=300%2C122&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=768%2C313&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=80%2C33&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=220%2C90&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=245%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 245w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=280%2C114&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=510%2C208&amp;quality=100&amp;strip=all&amp;ssl=1 510w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/dotmatrixphoto.jpg?resize=750%2C306&amp;quality=100&amp;strip=all&amp;ssl=1 750w\" sizes=\"(max-width: 900px) 100vw, 900px\" \/><\/p>\n<p>You can control more than one matrix at a time. For that you just need to connect them to each other, as they have pins in both sides to extend the dot matrix.<\/p>\n<h3>Parts required<\/h3>\n<p>For this guide you\u2019ll need:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/8x8-dot-matrix-max7219\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x 8&#215;8 Dot Matrix with\u00a0MAX7219\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/potentiometer-assortment-kit\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x\u00a01k ohm Potentiometer<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Pin Wiring<\/h2>\n<p>You only need to connect 5 pins from the dot matrix to your Arduino board. The wiring is pretty straightforward:<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"283\"><strong>Dot matrix pin<\/strong><\/td>\n<td width=\"283\"><strong>Wiring to Arduino Uno<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"283\">GND<\/td>\n<td width=\"283\">GND<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">VCC<\/td>\n<td width=\"283\">5V<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">DIN<\/td>\n<td width=\"283\">Digital pin<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">CS<\/td>\n<td width=\"283\">Digital pin<\/td>\n<\/tr>\n<tr>\n<td width=\"283\">CLK<\/td>\n<td width=\"283\">Digital pin<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>How to control the dot matrix with Arduino<\/h2>\n<p>For making it easier to control the dot matrix, you need to download and install in your Arduino IDE the LedControl library. To install the library follow these steps:<\/p>\n<ol>\n<li><a href=\"https:\/\/github.com\/wayoda\/LedControl\/archive\/master.zip\" target=\"_blank\" rel=\"noopener noreferrer\">Click here to download the LedControl library<\/a>. You should have a .zip folder in your Downloads<\/li>\n<li>Unzip the <em>.zip<\/em> folder and you should get <strong>LedControl-master <\/strong>folder<\/li>\n<li>Rename your folder from\u00a0<del><strong>LedControl-master<\/strong><\/del>\u00a0to<strong> <strong>LedControl<\/strong><\/strong><\/li>\n<li>Move the <strong>LedControl\u00a0<\/strong>folder to your Arduino IDE installation\u00a0<strong>libraries\u00a0<\/strong>folder<\/li>\n<li>Finally, re-open your Arduino IDE<\/li>\n<\/ol>\n<h3>Using the\u00a0LedControl library functions<\/h3>\n<p>The easiest way to display something on the dot matrix is by using the functions <em>setLed()<\/em>,<em> setRow()<\/em> or<em> setColumn()<\/em>. These functions allow you to control one single led, one row or one column at a time.<\/p>\n<p>Here\u2019s the parameters\u00a0for each function:<\/p>\n<p><strong><em>setLed(addr, row, col, state)<\/em><\/strong><\/p>\n<ul>\n<li><strong>addr<\/strong> is the address of your matrix, for example, if you have just 1 matrix, the int addr will be zero.<\/li>\n<li><strong>row <\/strong>is\u00a0the row where the led is located<\/li>\n<li><strong>col<\/strong> is\u00a0the column where the led is located<\/li>\n<li><strong>state<\/strong>\n<ul>\n<li>It\u2019s true or 1 if you want to turn the led on<\/li>\n<li>It\u2019s false or 0 if you want to switch it off<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong><em>setRow(addr, row, value)<\/em><\/strong><\/p>\n<p><strong><em>setCol(addr, column, value)<\/em><\/strong><\/p>\n<h3>Index<\/h3>\n<p>As previously stated, this matrix has 8 columns and 8 rows. Each one is indexed from 0 to 7. Here\u2019s a figure for better understanding:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-24488\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=471%2C487&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"index\" width=\"471\" height=\"487\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?w=625&amp;quality=100&amp;strip=all&amp;ssl=1 625w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=145%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 145w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=291%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 291w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=78%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 78w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=213%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 213w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=97%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 97w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=231%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 231w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=402%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 402w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=472%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 472w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/index.jpg?resize=577%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 577w\" sizes=\"(max-width: 471px) 100vw, 471px\" \/><\/p>\n<p>If you want to display something in the matrix, you just need to know\u00a0if\u00a0in a determined row or column, the LEDs\u00a0that are on or off.<\/p>\n<p>For example, if you want to display a happy face, here\u2019s what you need to do:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-24489\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=567%2C454&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"faces\" width=\"567\" height=\"454\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?w=811&amp;quality=100&amp;strip=all&amp;ssl=1 811w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=150%2C120&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=300%2C241&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=768%2C616&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=80%2C64&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=220%2C177&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=125%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 125w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=187%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 187w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=296%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 296w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=517%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 517w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=607%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 607w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/faces.jpg?resize=741%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 741w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/p>\n<h2>Code<\/h2>\n<p>Here\u2019s a simple sketch\u00a0that displays three types of faces: a sad face, a neutral face and a happy face. Upload the following code to your board:<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n Created by Rui Santos\n \n All the resources for this project:\n http:\/\/randomnerdtutorials.com\/\n*\/\n\n#include &quot;LedControl.h&quot;\n#include &quot;binary.h&quot;\n\n\/*\n DIN connects to pin 12\n CLK connects to pin 11\n CS connects to pin 10 \n*\/\nLedControl lc=LedControl(12,11,10,1);\n\n\/\/ delay time between faces\nunsigned long delaytime=1000;\n\n\/\/ happy face\nbyte hf[8]= {B00111100,B01000010,B10100101,B10000001,B10100101,B10011001,B01000010,B00111100};\n\/\/ neutral face\nbyte nf[8]={B00111100, B01000010,B10100101,B10000001,B10111101,B10000001,B01000010,B00111100};\n\/\/ sad face\nbyte sf[8]= {B00111100,B01000010,B10100101,B10000001,B10011001,B10100101,B01000010,B00111100};\n\nvoid setup() {\n  lc.shutdown(0,false);\n  \/\/ Set brightness to a medium value\n  lc.setIntensity(0,8);\n  \/\/ Clear the display\n  lc.clearDisplay(0);  \n}\n\nvoid drawFaces(){\n  \/\/ Display sad face\n  lc.setRow(0,0,sf[0]);\n  lc.setRow(0,1,sf[1]);\n  lc.setRow(0,2,sf[2]);\n  lc.setRow(0,3,sf[3]);\n  lc.setRow(0,4,sf[4]);\n  lc.setRow(0,5,sf[5]);\n  lc.setRow(0,6,sf[6]);\n  lc.setRow(0,7,sf[7]);\n  delay(delaytime);\n  \n  \/\/ Display neutral face\n  lc.setRow(0,0,nf[0]);\n  lc.setRow(0,1,nf[1]);\n  lc.setRow(0,2,nf[2]);\n  lc.setRow(0,3,nf[3]);\n  lc.setRow(0,4,nf[4]);\n  lc.setRow(0,5,nf[5]);\n  lc.setRow(0,6,nf[6]);\n  lc.setRow(0,7,nf[7]);\n  delay(delaytime);\n  \n  \/\/ Display happy face\n  lc.setRow(0,0,hf[0]);\n  lc.setRow(0,1,hf[1]);\n  lc.setRow(0,2,hf[2]);\n  lc.setRow(0,3,hf[3]);\n  lc.setRow(0,4,hf[4]);\n  lc.setRow(0,5,hf[5]);\n  lc.setRow(0,6,hf[6]);\n  lc.setRow(0,7,hf[7]);\n  delay(delaytime);\n}\n\nvoid loop(){\n  drawFaces();\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/dot_matrix_faces.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<p>In the end, you\u2019ll have something like this:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-24490 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/gif_face_final.gif?resize=480%2C270&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"gif_face_final\" width=\"480\" height=\"270\" \/><\/p>\n<h2>Pong Game<\/h2>\n<p>The pong game that you&#8217;re about to try\u00a0was created by <a href=\"http:\/\/www.itopen.it\/arduino-pong-with-8x8-led-matrix-and-max7219\/\" target=\"_blank\" rel=\"noopener noreferrer\">Alessandro Pasotti<\/a>.<\/p>\n<p>For the pong game, you just need to add a 1k ohm potentiometer to the previous schematic. Assemble the new circuit\u00a0as shown below:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-24491\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=700%2C544&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"pongping_bb\" width=\"700\" height=\"544\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=150%2C117&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=300%2C233&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=80%2C62&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=220%2C171&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=129%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 129w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=193%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 193w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=306%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 306w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=534%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 534w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/pongping_bb.jpg?resize=627%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 627w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>Code<\/h2>\n<p>Then, upload the following code to your Arduino board:<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*  \n *   Play pong on an 8x8 matrix - project from itopen.it\n *\/\n \n#include &quot;LedControl.h&quot;\n#include &quot;Timer.h&quot;\n \n#define POTPIN A5 \/\/ Potentiometer\n#define PADSIZE 3\n#define BALL_DELAY 200\n#define GAME_DELAY 10\n#define BOUNCE_VERTICAL 1\n#define BOUNCE_HORIZONTAL -1\n#define NEW_GAME_ANIMATION_SPEED 50\n#define HIT_NONE 0\n#define HIT_CENTER 1\n#define HIT_LEFT 2\n#define HIT_RIGHT 3\n \n\/\/#define DEBUG 1\n \nbyte sad[] = {\nB00000000,\nB01000100,\nB00010000,\nB00010000,\nB00000000,\nB00111000,\nB01000100,\nB00000000\n};\n \nbyte smile[] = {\nB00000000,\nB01000100,\nB00010000,\nB00010000,\nB00010000,\nB01000100,\nB00111000,\nB00000000\n};\n \nTimer timer;\n \nLedControl lc = LedControl(12,11,10,1);\n \nbyte direction; \/\/ Wind rose, 0 is north\nint xball;\nint yball;\nint yball_prev;\nbyte xpad;\nint ball_timer;\n \nvoid setSprite(byte *sprite){\n    for(int r = 0; r &lt; 8; r++){\n        lc.setRow(0, r, sprite[r]);\n    }\n}\n \nvoid newGame() {\n    lc.clearDisplay(0);\n    \/\/ initial position\n    xball = random(1, 7);\n    yball = 1;\n    direction = random(3, 6); \/\/ Go south\n    for(int r = 0; r &lt; 8; r++){\n        for(int c = 0; c &lt; 8; c++){\n            lc.setLed(0, r, c, HIGH);\n            delay(NEW_GAME_ANIMATION_SPEED);\n        }\n    }\n    setSprite(smile);\n    delay(1500);\n    lc.clearDisplay(0);\n}\n \nvoid setPad() {\n    xpad = map(analogRead(POTPIN), 0, 1020, 8 - PADSIZE, 0);\n}\n \nvoid debug(const char* desc){\n#ifdef DEBUG\n    Serial.print(desc);\n    Serial.print(&quot; XY: &quot;);\n    Serial.print(xball);\n    Serial.print(&quot;, &quot;);\n    Serial.print(yball);\n    Serial.print(&quot; XPAD: &quot;);\n    Serial.print(xpad);\n    Serial.print(&quot; DIR: &quot;);\n    Serial.println(direction);\n#endif\n}\n \nint checkBounce() {\n    if(!xball || !yball || xball == 7 || yball == 6){\n        int bounce = (yball == 0 || yball == 6) ? BOUNCE_HORIZONTAL : BOUNCE_VERTICAL;\n#ifdef DEBUG\n        debug(bounce == BOUNCE_HORIZONTAL ? &quot;HORIZONTAL&quot; : &quot;VERTICAL&quot;);\n#endif\n        return bounce;\n    }\n    return 0;\n}\n \nint getHit() {\n    if(yball != 6 || xball &lt; xpad || xball &gt; xpad + PADSIZE){\n        return HIT_NONE;\n    }\n    if(xball == xpad + PADSIZE \/ 2){\n        return HIT_CENTER;\n    }\n    return xball &lt; xpad + PADSIZE \/ 2 ? HIT_LEFT : HIT_RIGHT;\n}\n \nbool checkLoose() {\n    return yball == 6 &amp;&amp; getHit() == HIT_NONE;\n}\n \nvoid moveBall() {\n    debug(&quot;MOVE&quot;);\n    int bounce = checkBounce();\n    if(bounce) {\n        switch(direction){\n            case 0:\n                direction = 4;\n            break;\n            case 1:\n                direction = (bounce == BOUNCE_VERTICAL) ? 7 : 3;\n            break;\n            case 2:\n                direction = 6;\n            break;\n            case 6:\n                direction = 2;\n            break;\n            case 7:\n                direction = (bounce == BOUNCE_VERTICAL) ? 1 : 5;\n            break;\n            case 5:\n                direction = (bounce == BOUNCE_VERTICAL) ? 3 : 7;\n            break;\n            case 3:\n                direction = (bounce == BOUNCE_VERTICAL) ? 5 : 1;\n            break;\n            case 4:\n                direction = 0;\n            break;\n        }\n        debug(&quot;-&gt;&quot;);\n    }\n \n    \/\/ Check hit: modify direction is left or right\n    switch(getHit()){\n        case HIT_LEFT:\n            if(direction == 0){\n                direction =  7;\n            } else if (direction == 1){\n                direction = 0;\n            }\n        break;\n        case HIT_RIGHT:\n            if(direction == 0){\n                direction = 1;\n            } else if(direction == 7){\n                direction = 0;\n            }\n        break;\n    }\n \n    \/\/ Check orthogonal directions and borders ...\n    if((direction == 0 &amp;&amp; xball == 0) || (direction == 4 &amp;&amp; xball == 7)){\n        direction++;\n    }\n    if(direction == 0 &amp;&amp; xball == 7){\n        direction = 7;\n    }\n    if(direction == 4 &amp;&amp; xball == 0){\n        direction = 3;\n    }\n    if(direction == 2 &amp;&amp; yball == 0){\n        direction = 3;\n    }\n    if(direction == 2 &amp;&amp; yball == 6){\n        direction = 1;\n    }\n    if(direction == 6 &amp;&amp; yball == 0){\n        direction = 5;\n    }\n    if(direction == 6 &amp;&amp; yball == 6){\n        direction = 7;\n    }\n    \n    \/\/ &quot;Corner&quot; case\n    if(xball == 0 &amp;&amp; yball == 0){\n        direction = 3;\n    }\n    if(xball == 0 &amp;&amp; yball == 6){\n        direction = 1;\n    }\n    if(xball == 7 &amp;&amp; yball == 6){\n        direction = 7;\n    }\n    if(xball == 7 &amp;&amp; yball == 0){\n        direction = 5;\n    }\n \n    yball_prev = yball;\n    if(2 &lt; direction &amp;&amp; direction &lt; 6) {\n        yball++;\n    } else if(direction != 6 &amp;&amp; direction != 2) {\n        yball--;\n    }\n    if(0 &lt; direction &amp;&amp; direction &lt; 4) {\n        xball++;\n    } else if(direction != 0 &amp;&amp; direction != 4) {\n        xball--;\n    }\n    xball = max(0, min(7, xball));\n    yball = max(0, min(6, yball));\n    debug(&quot;AFTER MOVE&quot;);\n}\n \nvoid gameOver() {\n    setSprite(sad);\n    delay(1500);\n    lc.clearDisplay(0);\n}\n \nvoid drawGame() {\n    if(yball_prev != yball){\n        lc.setRow(0, yball_prev, 0);\n    }\n    lc.setRow(0, yball, byte(1 &lt;&lt; (xball)));\n    byte padmap = byte(0xFF &gt;&gt; (8 - PADSIZE) &lt;&lt; xpad) ;\n#ifdef DEBUG\n    \/\/Serial.println(padmap, BIN);\n#endif\n    lc.setRow(0, 7, padmap);\n}\n \nvoid setup() {\n  \/\/ The MAX72XX is in power-saving mode on startup,\n  \/\/ we have to do a wakeup call\n  pinMode(POTPIN, INPUT);\n \n  lc.shutdown(0,false);\n  \/\/ Set the brightness to a medium values\n  lc.setIntensity(0, 8);\n  \/\/ and clear the display\n  lc.clearDisplay(0);\n  randomSeed(analogRead(0));\n#ifdef DEBUG\n  Serial.begin(9600);\n  Serial.println(&quot;Pong&quot;);\n#endif\n  newGame();\n  ball_timer = timer.every(BALL_DELAY, moveBall);\n}\n \nvoid loop() {\n    timer.update();\n    \/\/ Move pad\n    setPad();\n#ifdef DEBUG\n    Serial.println(xpad);\n#endif\n    \/\/ Update screen\n    drawGame();\n    if(checkLoose()) {\n        debug(&quot;LOOSE&quot;);\n        gameOver();\n        newGame();\n    }\n    delay(GAME_DELAY);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/dot_matrix_pong_game.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Demonstration<\/h2>\n<p>Here&#8217;s the final demonstration of me playing the pong game. Have fun!<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-24493\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/gif_game_f.gif?resize=480%2C270&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"gif_game_f\" width=\"480\" height=\"270\" \/><\/p>\n<h2>Wrapping up<\/h2>\n<p>Have you ever used the dot matrix in your Arduino Projects?<\/p>\n<p>Let me know by leaving a comment below.<\/p>\n<p>If you like this post probably you might like my next ones (<a href=\"https:\/\/randomnerdtutorials.com\/download\" target=\"_blank\" rel=\"noopener noreferrer\">click here to subscribe my blog<\/a>). Thanks for reading,<\/p>\n<p>Rui<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The dot matrix that we\u2019re going to use in this\u00a0guide is a 8&#215;8 matrix which means that it has 8 columns and 8 rows, so it contains a total of &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Guide for 8&#215;8 Dot Matrix MAX7219 with Arduino + Pong Game\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/guide-for-8x8-dot-matrix-max7219-with-arduino-pong-game\/#more-24473\" aria-label=\"Read more about Guide for 8&#215;8 Dot Matrix MAX7219 with Arduino + Pong Game\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":24518,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[267,2,302,269,264,10],"tags":[],"class_list":["post-24473","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-project","category-arduino","category-0-guide","category-guide-project","category-project","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/07\/featured-image-1.jpg?fit=850%2C482&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/24473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=24473"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/24473\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/24518"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=24473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=24473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=24473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}