{"id":1774,"date":"2013-12-30T18:55:26","date_gmt":"2013-12-30T18:55:26","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=1774"},"modified":"2019-04-02T10:23:38","modified_gmt":"2019-04-02T10:23:38","slug":"teensy-arduino-memory-game","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/teensy-arduino-memory-game\/","title":{"rendered":"Teensy\/Arduino &#8211; Memory Game"},"content":{"rendered":"<p>This project is all about creating a simple game to test your memory.<\/p>\n<p>I&#8217;ll be using a Teensy 3.0 board. If you want to know more about this board please <a title=\"Getting Started with Teensy\" href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-teensy\/\">click here<\/a> to read a Getting Started Guide I&#8217;ve created a few days ago. This project is also 100% compatible with the Arduino.<!--more--><\/p>\n<h2>Watch the video below to see how it works<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/cDEmH0iguMw?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Parts Required<\/h2>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-1777\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/parts.jpg?resize=367%2C275&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"parts\" width=\"367\" height=\"275\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/parts.jpg?resize=1024%2C764&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/parts.jpg?resize=300%2C224&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/parts.jpg?w=2400&amp;quality=100&amp;strip=all&amp;ssl=1 2400w\" sizes=\"(max-width: 367px) 100vw, 367px\" \/><\/p>\n<ul>\n<li>1x <a href=\"http:\/\/www.pjrc.com\/teensy\/\" target=\"_blank\" rel=\"noopener noreferrer\">Teensy 3.0<\/a><\/li>\n<li>(or <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\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">8x 220 Ohm Resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">4x LED&#8217;s<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\" rel=\"noopener noreferrer\">4x Pushbuttons<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3-24v-piezo-buzzer\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x Buzzer<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x Breadboard<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper Cables<\/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>Schematics<\/h2>\n<ul>\n<li><strong>Teensy<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: center;\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics_memory_game_teensy1.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-1833\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics_memory_game_teensy1.jpg?resize=459%2C396&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"schematics_memory_game_teensy\" width=\"459\" height=\"396\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics_memory_game_teensy1.jpg?w=656&amp;quality=100&amp;strip=all&amp;ssl=1 656w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics_memory_game_teensy1.jpg?resize=300%2C258&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 459px) 100vw, 459px\" \/><\/a><\/p>\n<ul>\n<li><strong>Arduino<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: center;\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics-memory-game.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-1831\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics-memory-game.png?resize=407%2C339&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"[schematics] memory game\" width=\"407\" height=\"339\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics-memory-game.png?w=582&amp;quality=100&amp;strip=all&amp;ssl=1 582w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/schematics-memory-game.png?resize=300%2C249&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 407px) 100vw, 407px\" \/><\/a><\/p>\n<h2>Upload the code below<\/h2>\n<p>This code works with Teensy and with the Arduino.<\/p>\n<p><pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Memory Game with Arduino\n  Based on a project by Jeremy Wilson \n  Modified by Rui Santos\n  Visit: http:\/\/randomnerdtutorials.com\n*\/\n\n\/\/ Constants\nconst int button1 = 2;          \/\/ 1st button controls Blue LED \nconst int button2 = 3;          \/\/ 2nd button controls Yellow LED\nconst int button3 = 4;          \/\/ 3rd button controls Green LED\nconst int button4 = 5;          \/\/ 4th button controls Red LED\nconst int led1 = 7;             \/\/ Blue LED\nconst int led2 = 8;             \/\/ Yellow LED\nconst int led3 = 9;             \/\/ Green LED\nconst int led4 = 10;            \/\/ Red LED\nconst int buzzer = 12;          \/\/ Buzzer Output\nconst int tones[] = {1915, 1700, 1519, 1432, 2700}; \/\/ tones when you press the LED's - the last one is when you fail.\n\n\/\/ Variables\nint buttonState[] = {0,0,0,0};         \/\/ current state of the button\nint lastButtonState[] = {0,0,0,0};     \/\/ previous state of the button\nint buttonPushCounter[] = {0,0,0,0}; \n\n\nvoid playTone(int tone, int duration) {\n  for (long i = 0; i &lt; duration * 1000L; i += tone * 2) {\n    digitalWrite(buzzer, HIGH);\n    delayMicroseconds(tone);\n    digitalWrite(buzzer, LOW);\n    delayMicroseconds(tone);\n  }\n}\n\nvoid setup() {\n  \/\/ initialize inputs :\n  randomSeed(analogRead(0));\n  pinMode(button1, INPUT);\n  pinMode(button2, INPUT);\n  pinMode(button3, INPUT);\n  pinMode(button4, INPUT);\n  \/\/ initialize outputs:\n  pinMode(led1, OUTPUT);\n  pinMode(led2, OUTPUT);\n  pinMode(led3, OUTPUT);\n  pinMode(led4, OUTPUT);\n  pinMode(buzzer, OUTPUT);\n  \/\/ initialize serial communication for debugging:\n  \/\/Serial.begin(9600); \n}\nint game_on = 0;\nint wait = 0;\nint currentlevel = 1; \/\/ This is the level (also the number of button presses to pass to next level)\nlong rand_num = 0; \/\/initialize long variable for random number from 0-100.\nint rando = 0; \/\/initialize random integer for loopgame_on. Will be from 1-4 later.\nint butwait = 500; \/\/amount of time to wait for next button input (ghetto de-bounce)\nint ledtime = 500; \/\/amount of time each LED flashes for when button is pressed\nint n_levels = 10; \/\/number of levels until the game is won\nint pinandtone = 0; \/\/This integer is used when the sequence is displayed\nint right = 0; \/\/This variable must be 1 in order to go to the next level\nint speedfactor = 5; \/\/This is the final speed of the lights and sounds for the last level. This increases as more games are won\nint leddelay = 200; \/\/Initializing time for LED. This will decrease as the level increases\n\nvoid loop() {\n  \nint n_array[n_levels];\nint u_array[n_levels];\n\nint i;\n\/\/clears arrays both &quot;n_array&quot; and &quot;u_array&quot; and starts a new game\nif (game_on == 0){\nfor(i=0; i&lt;n_levels; i=i+1){\n        n_array[i]=0;\n        u_array[i]=0;\n        rand_num = random(1,200);\n        if (rand_num &lt;= 50)\n          rando=0;\n        else if (rand_num&gt;50 &amp;&amp; rand_num&lt;=100)\n          rando=1;\n        else if (rand_num&gt;100 &amp;&amp; rand_num&lt;=150)\n          rando=2;\n         else if (rand_num&lt;=200)\n          rando=3;\n        \/\/saves a random number in our n_array\n         n_array[i]=rando;\n      }\n game_on = 1; \n\n}\n\n\n\/\/shows the user the current sequence\nif (wait == 0){\n delay (200);\ni = 0;\nfor (i = 0; i &lt; currentlevel; i= i + 1){\n  leddelay = ledtime\/(1+(speedfactor\/n_levels)*(currentlevel - 1));\n      pinandtone = n_array[i];\n      digitalWrite(pinandtone+7, HIGH);\n      playTone(tones[pinandtone], leddelay);\n      digitalWrite(pinandtone+7, LOW);\n      delay(100\/speedfactor);\n    }\n    wait = 1;\n}\ni = 0;\nint buttonchange = 0;    \nint j = 0; \/\/ This is the current position in the sequence\nwhile (j &lt; currentlevel){    \n    while (buttonchange == 0){\n          for (i = 0; i &lt; 4; i = i + 1){ \n          buttonState[i] = digitalRead(i+2);\n          buttonchange = buttonchange + buttonState[i];\n        }\n    }\n     for (i = 0; i &lt; 4; i = i + 1){\n        if (buttonState[i] == HIGH) {\n            digitalWrite(i+7, HIGH);\n            playTone(tones[i], ledtime);\n            digitalWrite(i+7, LOW);\n            wait = 0;\n            u_array[j]=i; \n            buttonState[i] = LOW;\n            buttonchange = 0;\n         }\n       } \n        if (u_array[j] == n_array[j]){\n            j++;  \n            right = 1;\n            }\n        else{\n          \n            right = 0;\n            i = 4;\n            j = currentlevel;\n            wait = 0;\n        }\n}\n\nif (right == 0){\n  delay(300);\n  i = 0;\n  game_on = 0;\n  currentlevel = 1;\n  for (i = 0; i &lt; 4; i = i + 1){\n         digitalWrite(i+7, HIGH);\n      }\n          playTone(tones[4], ledtime);\n   for (i = 0; i &lt; 4; i = i + 1){\n         digitalWrite(i+7, LOW);   \n       }\n       delay (200);\n  for (i = 0; i &lt; 4; i = i + 1){\n         digitalWrite(i+7, HIGH);\n      }\n          playTone(tones[4], ledtime);\n   for (i = 0; i &lt; 4; i = i + 1){\n         digitalWrite(i+7, LOW);   \n       }\n       \n       delay(500);\n       game_on = 0;\n}\n\n\n\/\/if you insert the right sequence it levels up\nif (right == 1){\n    currentlevel++;\n    wait = 0;\n    }\n\/\/if you finish the game    \nif (currentlevel == n_levels){\n  delay(500);\n  \/\/ The following is the victory sound:\n  int notes[] = {2, 2, 2, 2, 0, 1, 2, 1, 2};\n  int note = 0;\n  int tempo[] = {200, 200, 200, 400, 400, 400, 200, 200, 600}; \n  int breaks[] = {100, 100, 100, 200, 200, 200, 300, 100, 200}; \n  for (i = 0; i &lt; 9; i = i + 1){\n  note = notes[i];\n    digitalWrite(note+7, HIGH);\n    playTone(tones[note], tempo[i]);\n    digitalWrite(note+7, LOW);\n    delay(breaks[i]);\n  }\n\/\/sets game_on to 0, so it restarts a new game\ngame_on = 0;\ncurrentlevel = 1;\nn_levels = n_levels + 2;\nspeedfactor = speedfactor + 1;\n    }\n \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\/Teensy_Arduino_Memory_Game.c\" target=\"_blank\">View raw code<\/a><\/p><br \/>\n<strong>Did you enjoy this project?<\/strong><\/p>\n<p>Please leave a comment.<\/p>\n<p>Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my\u00a0<a href=\"https:\/\/www.facebook.com\/RandomNerdTutorials?ref=hl\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Facebook Page.<\/a><\/p>\n<p><strong>P.S. <a title=\"Getting Started with Teensy\" href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-teensy\/\">Click here to read the Getting Started with Teensy<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This project is all about creating a simple game to test your memory. I&#8217;ll be using a Teensy 3.0 board. If you want to know more about this board please &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Teensy\/Arduino &#8211; Memory Game\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/teensy-arduino-memory-game\/#more-1774\" aria-label=\"Read more about Teensy\/Arduino &#8211; Memory Game\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":1778,"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":[303,267,2,264,196,197,10],"tags":[],"class_list":["post-1774","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-project","category-teensy","category-t-tutorials","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2013\/12\/thumbnailmemorygame.jpg?fit=616%2C402&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/1774","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=1774"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/1774\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/1778"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=1774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=1774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=1774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}